Clean β’ Professional
Understanding data types is one of the most important fundamentals in Core Java.
Every variable you create in Java has a specific data type that defines what kind of value it can store and how much memory it needs.
Data types tell the Java compiler what kind of data a variable will hold β for example, numbers, characters, or true/false values.
Java is a strongly typed and statically typed language, meaning every variable must have a defined data type before it is used.
Variable vs Data Type:
A variable is the name of a memory location, and a data type defines the kind of value that variable can hold.
Java provides two main categories of data types:

| Category | Description |
|---|---|
| 1. Primitive Data Types | Built-in types defined by Java (8 types) |
| 2. Non-Primitive (Reference) Data Types | User-defined or library types like String, Arrays, and Classes |
There are 8 primitive data types in Java β the basic building blocks for data manipulation.

| Data Type | Size | Range | Default Value | Example | Description |
|---|---|---|---|---|---|
| byte | 1 byte | -128 to 127 | 0 | byte b = 100; | For small integers |
| short | 2 bytes | -32,768 to 32,767 | 0 | short s = 20000; | For medium-sized integers |
| int | 4 bytes | -2Β³ΒΉ to 2Β³ΒΉ-1 | 0 | int age = 25; | Common integer type |
| long | 8 bytes | -2βΆΒ³ to 2βΆΒ³-1 | 0L | long population = 9000000000L; | For very large numbers |
| float | 4 bytes | ~6β7 decimal digits | 0.0f | float price = 19.99f; | Decimal (low precision) |
| double | 8 bytes | ~15 decimal digits | 0.0d | double pi = 3.14159; | Decimal (high precision) |
| char | 2 bytes | '\u0000' to '\uffff' | '\u0000' | char grade = 'A'; | For single characters |
| boolean | 1 bit | true / false | false | boolean isActive = true; | True or false values |
Non-primitive data types are created by the programmer or provided by Java libraries.
-Data-Types%20_20251106_101959.png&w=3840&q=75)
They can hold multiple values or complex objects.
| Type | Example | Description |
|---|---|---|
| String | String name = "Java"; | Collection of characters |
| Array | int[] marks = {90, 80, 70}; | Multiple values of same type |
| Class / Object | Student s = new Student(); | User-defined data structure |
| Interface | Runnable r = new Task(); | Contract for implementation |
Wrapper classes allow primitives to be used as objects β useful for collections (like ArrayList) and generics.
| Primitive | Wrapper Class |
|---|---|
| byte | Byte |
| short | Short |
| int | Integer |
| long | Long |
| float | Float |
| double | Double |
| char | Character |
| boolean | Boolean |
Example:
Integer num = 100; // Autoboxing
int n = num; // Unboxing
Type Casting means converting one data type into another.
Automatically done by Java β from smaller β larger type.
int num = 10;
double result = num; // int β double
System.out.println(result); // 10.0
Manually done by programmer β from larger β smaller type.
double value = 9.78;
int number = (int) value; // double β int
System.out.println(number); // 9
Invalid Casting Example:
int x = 10;
boolean flag = (boolean) x; // Error β incompatible types
When performing operations with mixed data types, Java promotes smaller types to larger ones to prevent data loss.
Promotion order:
byte β short β int β long β float β double
Example:
byte a = 10;
byte b = 20;
int result = a + b; // promoted to int
System.out.println(result);
Another Example:
int a = 5;
float b = 6.5f;
double result = a + b;
System.out.println(result); // Output: 11.5
int a = 5;
Integer obj = a; // Autoboxing
int b = obj; // Unboxing
public class CoreJavaDataTypes {
public static void main(String[] args) {
int age = 25;
double salary = age * 1000.50;
float tax = (float) (salary * 0.05); // Explicit casting
String name = "John";
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Salary: " + salary);
System.out.println("Tax: " + tax);
}
}
Output:
Name: John
Age: 25
Salary: 25012.5
Tax: 1250.625Β