What are Data Types?
Data types specify what kind of data a variable can hold and how much memory it occupies. Java is a statically-typed language — every variable must have a declared type.
int age = 25; // integer number
double salary = 75000.50; // decimal number
char grade = 'A'; // single character
boolean active = true; // true or false
String name = "Arjun"; // text (reference type)Java data types are divided into two categories:
- Primitive Types — Built-in, store actual values
- Reference Types — Store memory addresses (objects, arrays, strings)
Primitive Data Types
Java has 8 primitive data types:
| Type | Size | Range | Default | Example |
|---|---|---|---|---|
byte | 1 byte | -128 to 127 | 0 | byte b = 100; |
short | 2 bytes | -32,768 to 32,767 | 0 | short s = 30000; |
int | 4 bytes | -2¹⁻ to 2³¹-1 (~2.1 billion) | 0 | int i = 100000; |
long | 8 bytes | -2⁶³ to 2⁶³-1 | 0L | long l = 99999999L; |
float | 4 bytes | ~7 decimal digits | 0.0f | float f = 3.14f; |
double | 8 bytes | ~15 decimal digits | 0.0d | double d = 3.14159; |
char | 2 bytes | 0 to 65,535 (Unicode) | '\u0000' | char c = 'A'; |
boolean | 1 bit* | true or false | false | boolean b = true; |
Integer Types
public class IntegerTypes {
public static void main(String[] args) {
byte smallNum = 127; // -128 to 127
short mediumNum = 32000; // -32,768 to 32,767
int regularNum = 2_000_000; // Underscores for readability
long bigNum = 9_999_999_999L; // Note the 'L' suffix
System.out.println("byte: " + smallNum);
System.out.println("short: " + mediumNum);
System.out.println("int: " + regularNum);
System.out.println("long: " + bigNum);
}
}Output:
byte: 127
short: 32000
int: 2000000
long: 9999999999Tip: Use underscores (_) in numeric literals for readability: 1_000_000 instead of 1000000.
Floating-Point Types
public class FloatTypes {
public static void main(String[] args) {
float price = 99.99f; // Must end with 'f'
double pi = 3.141592653589793; // More precise
System.out.println("float: " + price);
System.out.println("double: " + pi);
// Scientific notation
double distance = 1.496e8; // 1.496 × 10⁸
System.out.println("Distance to Sun: " + distance + " km");
}
}Output:
float: 99.99
double: 3.141592653589793
Distance to Sun: 1.496E8 kmWhen to use float vs double?
Use double by default — it's more precise. Use float only when memory is a concern (e.g., large arrays in game development).
Character Type
The char type stores a single Unicode character (2 bytes):
public class CharType {
public static void main(String[] args) {
char letter = 'A';
char digit = '7';
char symbol = '@';
char unicode = '\u0041'; // Unicode for 'A'
char hindi = 'अ'; // Hindi character
System.out.println(letter); // A
System.out.println(unicode); // A
System.out.println(hindi); // अ
// char is actually stored as a number (Unicode value)
System.out.println((int) letter); // 65
}
}Boolean Type
The boolean type holds only true or false:
public class BooleanType {
public static void main(String[] args) {
boolean isJavaFun = true;
boolean isRaining = false;
boolean isAdult = (18 >= 18); // true
System.out.println("Java is fun: " + isJavaFun);
System.out.println("Is adult: " + isAdult);
}
}Reference Data Types
Reference types store the memory address (reference) of an object rather than the actual value.
String
String is the most commonly used reference type:
String greeting = "Hello, World!";
String name = new String("Arjun"); // Also valid, but not preferred
System.out.println(greeting.length()); // 13
System.out.println(greeting.toUpperCase()); // HELLO, WORLD!
System.out.println(greeting.charAt(0)); // HArrays
Arrays store multiple values of the same type:
int[] numbers = {10, 20, 30, 40, 50};
String[] names = {"Arjun", "Priya", "Rahul"};
System.out.println(numbers[0]); // 10
System.out.println(names.length); // 3Objects
Any instance of a class is a reference type:
Scanner scanner = new Scanner(System.in);Primitive vs Reference Types
| Feature | Primitive | Reference |
|---|---|---|
| Stores | Actual value | Memory address |
| Memory | Stack | Heap |
| Default | 0, 0.0, false, '\u0000' | null |
| Methods | None | Has methods |
| Examples | int, double, boolean | String, Array, Object |
Type Casting
Converting one data type to another.
Widening Casting (Automatic)
Smaller type → Larger type. Done automatically by Java:
byte → short → int → long → float → doubleint myInt = 100;
long myLong = myInt; // Automatic: int → long
double myDouble = myLong; // Automatic: long → double
System.out.println(myInt); // 100
System.out.println(myLong); // 100
System.out.println(myDouble); // 100.0Narrowing Casting (Manual)
Larger type → Smaller type. Must be done explicitly:
double myDouble = 9.78;
int myInt = (int) myDouble; // Manual: double → int
System.out.println(myDouble); // 9.78
System.out.println(myInt); // 9 (decimal part lost)Warning: Narrowing casting can cause data loss (truncation, overflow).
int largeNum = 130;
byte smallNum = (byte) largeNum;
System.out.println(smallNum); // -126 (overflow!)Wrapper Classes
Every primitive type has a corresponding wrapper class (object version):
| Primitive | Wrapper Class |
|---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
// Autoboxing: primitive → wrapper (automatic)
int num = 42;
Integer wrapped = num;
// Unboxing: wrapper → primitive (automatic)
int unwrapped = wrapped;
// Useful methods
int parsed = Integer.parseInt("123"); // String → int
String str = Integer.toString(123); // int → String
int max = Integer.MAX_VALUE; // 2147483647Choosing the Right Data Type
| Use Case | Recommended Type |
|---|---|
| Age, count, loop counter | int |
| Large numbers (timestamps, IDs) | long |
| Money, precise decimals | double (or BigDecimal for finance) |
| Yes/No flags | boolean |
| Single character | char |
| Text | String |
| Memory-sensitive large arrays | byte or short |
Practical Example
public class StudentProfile {
public static void main(String[] args) {
// Using different data types
String name = "Arjun Kumar";
int age = 22;
double gpa = 8.75;
char bloodGroup = 'O';
boolean isGraduated = false;
long aadhaarNumber = 1234_5678_9012L;
byte semester = 6;
System.out.println("=== Student Profile ===");
System.out.printf("Name: %s%n", name);
System.out.printf("Age: %d years%n", age);
System.out.printf("GPA: %.2f/10%n", gpa);
System.out.printf("Blood Group: %c+%n", bloodGroup);
System.out.printf("Graduated: %b%n", isGraduated);
System.out.printf("Aadhaar: %d%n", aadhaarNumber);
System.out.printf("Semester: %d%n", semester);
}
}Output:
=== Student Profile ===
Name: Arjun Kumar
Age: 22 years
GPA: 8.75/10
Blood Group: O+
Graduated: false
Aadhaar: 123456789012
Semester: 6Summary
- Java has 8 primitive types:
byte,short,int,long,float,double,char,boolean - Reference types include
String, arrays, and objects — they store memory addresses - Widening casting (small → large) happens automatically; narrowing (large → small) requires explicit cast
- Every primitive has a wrapper class (
int→Integer) for object-oriented use - Use
intfor most integers,doublefor decimals,Stringfor text,booleanfor flags - Java is statically typed — declare the type before using a variable