A data type specifies the kind of value a variable can store. Java provides primitive data types for values such as numbers, characters, and Boolean values. It also supports reference types, such as strings, arrays, and objects.
A data type defines the kind of data a variable can represent and the operations that can be performed on it.
int age = 20;
double price = 99.50;
char grade = 'A';
boolean isPassed = true;
System.out.println(age);
System.out.println(price);
System.out.println(grade);
System.out.println(isPassed);
Output:
20
99.5
A
true
Each variable uses a data type suitable for its value.
Java data types are broadly divided into two categories:
| Category | Description | Examples |
|---|---|---|
| Primitive | Built-in types that represent basic values. | int, char, boolean |
| Reference | Types that refer to objects or arrays. | String, arrays, classes, interfaces |
Java has eight primitive data types. Reference types include classes, interfaces, arrays, and enumerations.
Java provides four primitive integer types for storing whole numbers.
| Type | Size | Range |
|---|---|---|
byte |
8 bits | -128 to 127 |
short |
16 bits | -32,768 to 32,767 |
int |
32 bits | -2³¹ to 2³¹ - 1 |
long |
64 bits | -2⁶³ to 2⁶³ - 1 |
byte smallNumber = 100;
short year = 2026;
int salary = 30000;
long population = 8000000000L;
System.out.println(smallNumber);
System.out.println(year);
System.out.println(salary);
System.out.println(population);
Use the L suffix for a long literal when
the value requires it. Using uppercase L
is recommended for readability.
Floating-point types store numbers with fractional parts.
| Type | Size | Typical Precision |
|---|---|---|
float |
32 bits | About 6–7 decimal digits |
double |
64 bits | About 15–16 decimal digits |
float temperature = 36.5f;
double price = 1250.75;
System.out.println(temperature);
System.out.println(price);
Output:
36.5
1250.75
A floating-point literal is a double by
default. Add f or F to use
a float literal.
BigDecimal.
The char data type represents a single
UTF-16 code unit and occupies 16 bits.
char grade = 'A';
char symbol = '#';
char digit = '7';
System.out.println(grade);
System.out.println(symbol);
System.out.println(digit);
Output:
A
#
7
Character literals use single quotation marks.
A char is not the same as a
String.
char letter = 'A';
String name = "A";
The boolean data type represents a logical
value: true or false.
boolean isStudent = true;
boolean isMarried = false;
System.out.println(isStudent);
System.out.println(isMarried);
Output:
true
false
Boolean variables are commonly used in conditions, loops, and decision-making statements.
int age = 20;
boolean isAdult = age >= 18;
System.out.println(isAdult);
Output:
true
A String represents a sequence of characters.
It is a class in Java, not a primitive data type.
String name = "Rahul";
String city = "Aurangabad";
String message = "Welcome to Java";
System.out.println(name);
System.out.println(city);
System.out.println(message);
Output:
Rahul
Aurangabad
Welcome to Java
String literals use double quotation marks. Strings are immutable, which means their contents cannot be changed after a String object is created.
An array stores a fixed number of values of a specified component type. An array variable is a reference type.
int[] marks = {80, 85, 90};
System.out.println(marks[0]);
System.out.println(marks[1]);
System.out.println(marks[2]);
Output:
80
85
90
Array indexes begin at zero. Therefore,
marks[0] accesses the first element.
Classes define the structure and behavior of objects. A variable of a class type can refer to an object created from that class.
class Student {
String name;
}
public class Main {
public static void main(String[] args) {
Student s1 = new Student();
s1.name = "Rahul";
System.out.println(s1.name);
}
}
Output:
Rahul
The variable s1 holds a reference to
a Student object.
Instance variables and static fields receive default values if no explicit value is assigned.
| Type | Default Value |
|---|---|
| Integer types | 0 |
float |
0.0f |
double |
0.0 |
char |
'\u0000' |
boolean |
false |
| Reference types | null |
public class Example {
int number;
boolean active;
String name;
public static void main(String[] args) {
Example e = new Example();
System.out.println(e.number);
System.out.println(e.active);
System.out.println(e.name);
}
}
Output:
0
false
null
Java sometimes converts a value from one numeric type to another automatically. This is called widening primitive conversion.
int number = 100;
double result = number;
System.out.println(result);
Output:
100.0
A narrowing conversion may require an explicit cast.
double price = 99.75;
int wholePrice = (int) price;
System.out.println(wholePrice);
Output:
99
Casting from double to int
discards the fractional part; it does not round
the number to the nearest integer.
Choose a data type based on the kind of information your program needs to store.
| Information | Suggested Type | Example |
|---|---|---|
| Student's age | int |
int age = 20; |
| Product price | double for a basic example |
double price = 99.50; |
| Grade character | char |
char grade = 'A'; |
| Account active status | boolean |
boolean active = true; |
| Student name | String |
String name = "Rahul"; |
For real financial applications, use an appropriate
decimal representation, such as BigDecimal,
when exact decimal arithmetic is required.
Create a program that stores a student's details using different data types.
public class StudentDetails {
public static void main(String[] args) {
String name = "Rahul";
int age = 20;
double marks = 85.5;
char grade = 'A';
boolean passed = true;
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Marks: " + marks);
System.out.println("Grade: " + grade);
System.out.println("Passed: " + passed);
}
}
Output:
Name: Rahul
Age: 20
Marks: 85.5
Grade: A
Passed: true
Key Points:
String, arrays, and class types are reference types.1. Which data type stores true or false values?
2. Which of the following is a reference type?
3. What is the output of this code?
double price = 99.75;
int amount = (int) price;
System.out.println(amount);