Getting Started

Variables and Data Types

Explore Java primitive types, reference types, type casting, and variable declarations.

Primitive Data Types

Java has 8 primitive data types:

TypeSizeRange
byte1 byte-128 to 127
short2 bytes-32,768 to 32,767
int4 bytes-2^31 to 2^31-1
long8 bytes-2^63 to 2^63-1
float4 bytes~7 decimal digits
double8 bytes~15 decimal digits
boolean1 bittrue or false
char2 bytesUnicode characters

Reference Types

Everything else is a reference type: Strings, arrays, objects, etc.

Type Casting

Java supports both implicit (widening) and explicit (narrowing) type casting.

Example

java
// Primitive types
byte b = 100;
short s = 30000;
int i = 2_000_000;        // underscores for readability
long l = 9_000_000_000L;  // L suffix for long
float f = 3.14f;          // f suffix for float
double d = 3.14159265358;
boolean flag = true;
char c = 'J';

// String (reference type)
String greeting = "Hello, Java!";
String name = new String("Alice");

// Type inference with var (Java 10+)
var message = "Hello";  // inferred as String
var count = 42;         // inferred as int

// Type casting
double pi = 3.14;
int truncated = (int) pi;  // explicit cast: 3

int small = 100;
long big = small;  // implicit widening cast

// String conversion
int num = 42;
String str = String.valueOf(num);   // "42"
int back = Integer.parseInt(str);   // 42

// Constants
final double PI = 3.14159;
// PI = 3.0; // Error! Cannot reassign final variable
Try it yourself — JAVA