Data Types in Java
In this lesson: All Java data types — byte, short, int, long, float, double, char, boolean — sizes, ranges and examples. In Hindi & English.
What are data types
A data type tells Java what kind of value a variable holds — a whole number, a decimal, a character, true/false, or text. Java is strongly typed, so every variable has a type. Java's types fall into two groups: primitive types and reference types.
Primitive vs reference
Primitive types store the actual value in the variable — fast and simple. There are exactly eight. Reference types (String, arrays, objects) store a reference that points to data elsewhere in memory. Understanding this split explains a lot of Java behaviour later.
Integer types
| Type | Size | Range (approx) |
|---|---|---|
| byte | 1 byte | -128 to 127 |
| short | 2 bytes | -32,768 to 32,767 |
| int | 4 bytes | about ±2 billion |
| long | 8 bytes | very large |
int count = 100; long big = 10000000000L; // L marks a long
int is the everyday choice for whole numbers. Use long for very big values, with an L suffix.
Decimal types
float temp = 36.6f; // f marks a float double price = 99.99; // default for decimals
double is the default and most precise choice for decimals. float uses less memory but is less precise, and needs an f suffix.
char and boolean
char grade = 'A'; // one character, single quotes boolean pass = true; // only true or false
char holds a single character in single quotes. boolean holds only true or false — perfect for conditions and yes/no flags.
Reference types
String name = "Aman"; // text (a class)
int[] marks = {90, 85, 78}; // an array
System.out.println(name.length()); // 4
String and arrays are reference types — they point to objects. That is why String is capitalised and comes with methods like length(). Primitives have no methods.
Common mistakes
- Overflowing int. Very large numbers need
longwith anLsuffix. - Forgetting the
fon float.float x = 1.5;errors — write1.5f. - Using double quotes for char.
charuses single quotes:'A', not"A". - Thinking String is primitive. It is a class (reference type), written with a capital S.
Summary
- Data types define what kind of value a variable holds.
- Java has 8 primitive types and reference types like String and arrays.
- Whole numbers: byte, short, int, long; decimals: float, double.
charis one character;booleanis true/false.- Reference types point to objects and have methods; primitives do not.
Next, store values in Java variables, or work with them using Java operators.
Frequently Asked Questions
How many primitive data types does Java have?
What is the difference between primitive and reference types?
int x = 5). Reference types (like String and arrays) store a reference pointing to an object in memory. Primitives are simple values; references point to bigger objects.What is the difference between int and long?
int holds up to about 2 billion, while long holds much larger numbers. Use long when a value may exceed the int range, adding an L suffix like 10000000000L.When should I use float vs double?
double is more precise and is the default choice. Use float only to save memory in large arrays; for most maths, use double.