Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Overview of Primitive Types | Primitive Data Types
Java Data Types

bookOverview of Primitive Types

Java has eight primitive data types: byte, short, int, long, float, double, char, and boolean. These types represent raw values (not objects) and are stored directly in memory, each with a specific size and value range.

  • byte: 8-bit signed integer, range -128 to 127; used for memory-efficient arrays;
  • short: 16-bit signed integer, range -32,768 to 32,767; used in memory-constrained environments;
  • int: 32-bit signed integer, range -2,147,483,648 to 2,147,483,647; default for integers;
  • long: 64-bit signed integer, range -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807; for large integers;
  • float: 32-bit floating-point, approximate range ~1.4E-45 to ~3.4E+38; memory-efficient decimals;
  • double: 64-bit floating-point, approximate range ~4.9E-324 to ~1.8E+308; default for decimals;
  • char: 16-bit Unicode character, range '\u0000' to '\uffff'; stores characters;
  • boolean: represents true or false; used for logical values.

Default values: numeric types default to zero, char to '\u0000', and boolean to false.

To help you compare these types, here is a table summarizing their key properties:

TypeSize (bits)Value RangeDefault ValueTypical Use Case
byte8-128 to 1270Memory-efficient integer arrays
short16-32,768 to 32,7670Legacy code, memory-constrained env.;
int32-2,147,483,648 to 2,147,483,6470General-purpose integer values
long64-9,223,372,036,854,775,808 to 9,223,372,036,854,775,8070Large integer values
float32~1.4E-45 to ~3.4E+38 (approximate)0.0fMemory-efficient decimal numbers
double64~4.9E-324 to ~1.8E+308 (approximate)0.0dDefault for decimal numbers
char16'\u0000' (0) to '\uffff' (65,535)'\u0000'Storing characters
boolean1 (virtual)true or falsefalseLogical values

Note: The boolean type does not have a precisely defined size in memory; its size is determined by the Java Virtual Machine implementation.

1. Which primitive data type should you use to store a value representing whether a light is on or off?

2. What is the default value of a char field in a Java class?

question mark

Which primitive data type should you use to store a value representing whether a light is on or off?

Select the correct answer

question mark

What is the default value of a char field in a Java class?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 1

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Awesome!

Completion rate improved to 7.14

bookOverview of Primitive Types

Sveip for å vise menyen

Java has eight primitive data types: byte, short, int, long, float, double, char, and boolean. These types represent raw values (not objects) and are stored directly in memory, each with a specific size and value range.

  • byte: 8-bit signed integer, range -128 to 127; used for memory-efficient arrays;
  • short: 16-bit signed integer, range -32,768 to 32,767; used in memory-constrained environments;
  • int: 32-bit signed integer, range -2,147,483,648 to 2,147,483,647; default for integers;
  • long: 64-bit signed integer, range -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807; for large integers;
  • float: 32-bit floating-point, approximate range ~1.4E-45 to ~3.4E+38; memory-efficient decimals;
  • double: 64-bit floating-point, approximate range ~4.9E-324 to ~1.8E+308; default for decimals;
  • char: 16-bit Unicode character, range '\u0000' to '\uffff'; stores characters;
  • boolean: represents true or false; used for logical values.

Default values: numeric types default to zero, char to '\u0000', and boolean to false.

To help you compare these types, here is a table summarizing their key properties:

TypeSize (bits)Value RangeDefault ValueTypical Use Case
byte8-128 to 1270Memory-efficient integer arrays
short16-32,768 to 32,7670Legacy code, memory-constrained env.;
int32-2,147,483,648 to 2,147,483,6470General-purpose integer values
long64-9,223,372,036,854,775,808 to 9,223,372,036,854,775,8070Large integer values
float32~1.4E-45 to ~3.4E+38 (approximate)0.0fMemory-efficient decimal numbers
double64~4.9E-324 to ~1.8E+308 (approximate)0.0dDefault for decimal numbers
char16'\u0000' (0) to '\uffff' (65,535)'\u0000'Storing characters
boolean1 (virtual)true or falsefalseLogical values

Note: The boolean type does not have a precisely defined size in memory; its size is determined by the Java Virtual Machine implementation.

1. Which primitive data type should you use to store a value representing whether a light is on or off?

2. What is the default value of a char field in a Java class?

question mark

Which primitive data type should you use to store a value representing whether a light is on or off?

Select the correct answer

question mark

What is the default value of a char field in a Java class?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 1
some-alt