Java has 8 primitive data types:
Type | Size | Description | Example |
---|---|---|---|
byte |
1 byte | Whole number (small range) | byte a = 100; |
short |
2 bytes | Whole number | short b = 500; |
int |
4 bytes | Whole number (default) | int x = 1000; |
long |
8 bytes | Very large whole number | long l = 900000000L; |
float |
4 bytes | Decimal number (less precise) | float f = 3.14f; |
double |
8 bytes | Decimal number (more precise) | double d = 3.14159; |
char |
2 bytes | A single character | char c = 'A'; |
boolean |
1 bit | true or false | boolean b = true; |
These store references to objects:
1
2
String name = "John";
int[] scores = {90, 85, 78};
You must declare a variable before using it:
1
2
3
// <dataType> <variableName> [assign = <value>];
int age; // Declaration
age = 25; // Initialization
You can also do both at once:
1
int age = 25;
<variableName>
studentScore
.Code Demo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class DataTypeDemo {
public static void main(String[] args) {
// Primitive types
int age = 30;
double salary = 45000.50;
char grade = 'A';
boolean isEmployed = true;
// Reference types
String name = "Alice";
int[] scores = {80, 85, 90};
// Output
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Salary: $" + salary);
System.out.println("Grade: " + grade);
System.out.println("Is Employed: " + isEmployed);
System.out.println("Scores: " + scores[0] + ", " + scores[1] + ", " + scores[2]);
}
}
Output:
1
2
3
4
5
6
Name: Alice
Age: 30
Salary: $45000.5
Grade: A
Is Employed: true
Scores: 80, 85, 90