Java is a strongly-typed language, which means every variable must have a specific data type. Java's data types can be divided into two categories:
Primitive data types are the most basic data types in Java. They store simple values like numbers, characters, and booleans.
| Data Type | Size | Default Value | Description | Example |
|---|---|---|---|---|
| byte | 1 byte | 0 | Stores small integers from -128 to 127 | byte a = 10; |
| short | 2 bytes | 0 | Stores integers from -32,768 to 32,767 | short b = 1000; |
| int | 4 bytes | 0 | Stores integers from -2^31 to 2^31-1 | int c = 50000; |
| long | 8 bytes | 0L | Stores large integers from -2^63 to 2^63-1 | long d = 100000L; |
| float | 4 bytes | 0.0f | Stores floating-point numbers (single precision) | float e = 5.75f; |
| double | 8 bytes | 0.0d | Stores double-precision floating-point numbers | double f = 19.99d; |
| char | 2 bytes | '\u0000' | Stores single 16-bit Unicode characters | char g = 'A'; |
| boolean | 1 bit | false | Stores true or false | boolean h = true; |
public class PrimitiveExample { public static void main(String[] args) { byte byteVar = 120; short shortVar = 1000; int intVar = 123456; long longVar = 1000000000L; float floatVar = 12.34f; double doubleVar = 1234.56; char charVar = 'J'; boolean boolVar = true; System.out.println("Byte value: " + byteVar); System.out.println("Short value: " + shortVar); System.out.println("Int value: " + intVar); System.out.println("Long value: " + longVar); System.out.println("Float value: " + floatVar); System.out.println("Double value: " + doubleVar); System.out.println("Char value: " + charVar); System.out.println("Boolean value: " + boolVar); }}Byte value: 120
Short value: 1000
Int value: 123456
Long value: 1000000000
Float value: 12.34
Double value: 1234.56
Char value: J
Boolean value: trueReference data types refer to objects and arrays, not primitive values. They store references (addresses) to the actual objects in memory.
Examples:
xxxxxxxxxxpublic class ReferenceExample { public static void main(String[] args) { String name = "Java Programming"; // String is a reference type int[] numbers = {1, 2, 3, 4, 5}; // Array is a reference type System.out.println("Name: " + name); System.out.println("First number in the array: " + numbers[0]); }}Name: Java Programming
First number in the array: 1