- In Java, every variable must have a data type.
- A data type tells the compiler what kind of value a variable can store and how much memory it needs.
What is a Data Type?
- A data type is a classification that specifies the type of value a variable can hold.
Types of Data Types in Java:
1)Primitive Data Types
2)Non-Primitive (Reference) Data Types
Primitive Data Types:
- Primitive data types store simple values directly in memory.
- They are predefined by Java and are not objects.
- Java has 8 primitive data types.
1)byte:
- Used to store small whole numbers.
- Size: 1 byte (8 bits)
- Range: -128 to 127.
byte age = 25;
2)short:
- stores slightly larger whole numbers than byte.
- Size: 2 bytes
- Range: -32,768 to 32,767
short year = 2025;
3)int (Most Common Integer Type):
- used to store whole numbers.
- Size: 4 bytes
- Range: -2,147,483,648 to 2,147,483,647
int marks = 95;
4)long:
- stores very large whole numbers.
- Size: 8 bytes
- Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
long population = 8000000000L;
5)float:
- stores decimal numbers with single precision.
- Size: 4 bytes
- Precision: 6–7 decimal digits
float temperature = 36.5f;
6)double (Most Common Decimal Type):
- stores decimal numbers with double precision.
- Size: 8 bytes
- Precision: 15–16 decimal digits
double salary = 25000.75;
7)char:
- stores a single character.
- Size: 2 bytes
- Range: 0 to 65,535 (Unicode values)
char grade = 'A';
8)boolean:
- stores true or false values.
boolean isPassed = true;
Non-Primitive (Reference) Data Types:
- These store memory addresses instead of actual values.
1)String:
- Used to store text.
- Immutable (cannot be changed after creation).
String name = "Java";
2)Arrays:
- Used to store multiple values of the same type.
- Fixed size once created.
int[] numbers = {1, 2, 3};
System.out.println(numbers[0]); // 1

Top comments (0)