Java Data Types
Data types define the kind of value a variable can hold and how much memory it takes. Java is a strongly-typed language — every variable must have a declared type. It has 8 primitive types and non-primitive types like String, Arrays, Classes, and Interfaces.
Two Categories of Java Data Types
- Primitive — 8 built-in types (int, byte, short, long, float, double, char, boolean)
- Non-Primitive — String, Arrays, Classes, Interfaces, Objects
The 8 Primitive Data Types
1. byte
8-bit signed integer. Range: -128 to 127. Smallest integer type.
byte smallNum = 100;
System.out.println(smallNum); // 100
2. short
16-bit signed integer. Range: -32,768 to 32,767.
short s = 20000;
System.out.println(s);
3. int
32-bit signed integer. The most commonly used integer type.
int age = 22;
int marks = 50000;
System.out.println(age);
4. long
64-bit signed integer. Used for very large numbers. Add L suffix.
long population = 1400000000L;
long distance = 9876543210L;
System.out.println(population);
5. float
32-bit floating-point number. Add f suffix.
float price = 99.99f;
float temp = 36.6f;
System.out.println(price);
6. double
64-bit floating-point number. Default decimal type in Java.
double percentage = 89.5;
double pi = 3.14159;
System.out.println(percentage);
7. char
16-bit Unicode character. Enclosed in single quotes.
char grade = 'A';
char symbol = '$';
char letter = 'ह'; // Hindi character supported
System.out.println(grade);
8. boolean
Only two possible values — true or false.
boolean isActive = true;
boolean isLoggedIn = false;
System.out.println(isActive);
Primitive Types Summary Table
Type Size Range / Values
----- ---- ---------------
byte 8 bits -128 to 127
short 16 bits -32,768 to 32,767
int 32 bits -2^31 to 2^31-1
long 64 bits -2^63 to 2^63-1
float 32 bits ~7 decimal digits
double 64 bits ~15 decimal digits
char 16 bits Single Unicode character
boolean 1 bit true / false
Non-Primitive Data Types
String
Most common non-primitive type. A sequence of characters.
String name = "Ankit";
String city = "Varanasi";
// String methods
System.out.println(name.length()); // 5
System.out.println(name.toUpperCase()); // ANKIT
System.out.println(name.charAt(0)); // A
System.out.println(name.contains("nk")); // true
Array
int[] numbers = {1, 2, 3, 4, 5};
String[] fruits = {"apple", "banana", "mango"};
System.out.println(numbers[0]); // 1
System.out.println(fruits.length); // 3
Class / Object
class Student {
String name;
int age;
}
Student s = new Student();
s.name = "Ankit";
s.age = 22;
Default Values
Instance variables get default values — but local variables do NOT.
Type Default Value
----- -------------
byte 0
short 0
int 0
long 0L
float 0.0f
double 0.0
char '\u0000'
boolean false
String null
Object null
Type Conversion
Implicit (Widening) — Automatic
Smaller type → larger type, no cast needed.
int x = 100;
double y = x; // int → double automatically
System.out.println(y); // 100.0
byte b = 50;
int i = b; // byte → int
long l = i; // int → long
Explicit (Narrowing) — Manual Cast Required
double d = 99.99;
int x = (int) d; // 99 (decimal truncated)
System.out.println(x);
long big = 1000000L;
int smaller = (int) big;
Use int for most integers, long when numbers exceed 2 billion, double for most decimals. Only use byte, short, float when saving memory is critical.
float price = 99.99; causes compile error — because 99.99 is a double by default. Use 99.99f or 99.99F. Similarly, long literals need L suffix.
Create variables of all 8 primitive types with sample values. Print each with its type name. Then practice type conversion: declare a double with value 99.99 and cast it to int. Print both and observe the decimal truncation.
Quick Revision
- Java has 8 primitive types.
- int (most used), long (big numbers), double (decimals).
- float needs
fsuffix, long needsLsuffix. - char uses single quotes, String uses double quotes.
- boolean = true / false only.
- Non-primitive: String, Array, Class, Interface.
- Widening conversion is automatic; narrowing needs cast.
- Local variables have no default values.
Interview Questions
Q: How many primitive data types are there in Java?
A: 8 — byte, short, int, long, float, double, char, boolean.
Q: Difference between float and double?
A: float is 32-bit, ~7 decimal digits precision, needs f suffix. double is 64-bit, ~15 decimal digits precision, and is the default decimal type. Use double for accuracy.
Q: Difference between char and String?
A: char holds a single character (16-bit Unicode) and uses single quotes. String is a class that holds a sequence of characters and uses double quotes.
Q: What is the difference between implicit and explicit type conversion?
A: Implicit (widening) happens automatically when converting a smaller type to a larger one — int → long. Explicit (narrowing) requires a cast and may lose data — double → int.
Q: Why does float f = 99.99; give a compile error?
A: Because 99.99 is treated as a double by default. You must write 99.99f to explicitly tell the compiler it's a float.
Want to Master Java?
Join our Java course in Varanasi — practical classes and career support at our Sarnath centre.