Java Data Types

Data types specify the size and type of values a variable can store. Java has two groups: primitive and non-primitive.

Primitive Data Types

TypeSizeDefaultExample
byte1 byte0byte b = 10;
short2 bytes0short s = 500;
int4 bytes0int n = 1000;
long8 bytes0Llong l = 9000000000L;
float4 bytes0.0ffloat f = 3.14f;
double8 bytes0.0ddouble d = 3.14159;
char2 bytes'\u0000'char c = 'A';
boolean1 bit*falseboolean ok = true;

*The JVM specification doesn't fix the exact size of boolean.

Non-Primitive Data Types

Also called reference types: String, arrays, classes and interfaces. They store a reference (address) to an object.

Type Casting

Java
int a = 100;
double d = a;          // widening (automatic)
System.out.println(d);

double x = 9.78;
int y = (int) x;       // narrowing (manual)
System.out.println(y);
100.0 9
Track your progress through the Java course