</>

Technology

Core Java

Difficulty

Beginner

Interview Question

What are primitive data types in Java?

Answer

Primitive Data Types in Java

Java has 8 primitive data types — the most basic data types, stored on the stack (not heap), not objects.

The 8 Primitive Types

TypeSizeDefaultRangeExample
byte1 byte0-128 to 127byte b = 100;
short2 bytes0-32,768 to 32,767short s = 1000;
int4 bytes0-2B to 2Bint i = 50000;
long8 bytes0L-9.2Q to 9.2Qlong l = 9999999999L;
float4 bytes0.0f±3.4×10³⁸float f = 3.14f;
double8 bytes0.0±1.7×10³⁰⁸double d = 3.14159;
boolean1 bitfalsetrue/falseboolean flag = true;
char2 bytes'null'0 to 65.535char c = 'A';

Examples

Java
byte   age        = 25;
short  year       = 2024;
int    population = 1_400_000_000;  // underscores for readability
long   distance   = 9_460_730_472_580_800L;  // must end with L
float  price      = 9.99f;          // must end with f
double pi         = 3.141592653589793;
boolean isActive  = true;
char   grade      = 'A';

System.out.println((int) 'A');  // 65 — char to int conversion
System.out.println((char) 66);  // 'B' — int to char conversion

Primitives vs Wrapper Classes

Every primitive has a corresponding Wrapper class (object):

PrimitiveWrapper
intInteger
doubleDouble
booleanBoolean
charCharacter
longLong
Java
// Autoboxing — primitive → object (automatic)
int primitive = 42;
Integer wrapped = primitive;   // autoboxing

// Unboxing — object → primitive (automatic)
Integer obj = 100;
int value = obj;               // unboxing

// Useful for collections (which need objects, not primitives)
List<Integer> numbers = new ArrayList<>();
numbers.add(1);   // autoboxing — int → Integer
int n = numbers.get(0);  // unboxing — Integer → int

Type Casting

Java
// Widening (automatic — no data loss)
int i = 100;
long l = i;        // int → long (safe)
double d = l;      // long → double (safe)

// Narrowing (explicit — possible data loss)
double pi = 3.14;
int truncated = (int) pi;  // 3 — decimal part lost

long big = 1_000_000_000_000L;
int small = (int) big;     // data loss — overflow!

Follow AutomateQA

Related Topics