Java Tutorial

Variables & Data Types

Primitives, wrapper classes, reference types, and 'var' (JAVA 10+)

Strong Typing in Java

Java is a Statically Typed language. This means every variable must have a declared type before it can be used, and the compiler checks these types before the program ever runs. This makes Java code very safe and predictable.

1. Primitive Data Types

Java has eight "primitive" types which are the basic building blocks of all data. They are not objects; they are stored directly in the computer's memory.
CategoryTypeSize (Typical)Example
Integerbyte, short, int, long1, 2, 4, 8 bytes100, -50
Floating Pointfloat, double4, 8 bytes1.23, 10.5
Characterchar2 bytes (Unicode)'A', '1', '$'
Booleanboolean1 bit (virtually)true, false
java
int count = 10;
double price = 19.99;
char initial = 'A';
boolean isAvailable = true;

2. Reference Types (Objects)

A Reference Type is a variable that "refers" to an object in memory. The most common reference type is the String.
java
String name = "Alpha";
int[] numbers = {1, 2, 3}; // Arrays are also reference types!
Wait! Primitives start with a lowercase letter (int); Reference types start with an uppercase letter (String).

3. Wrapper Classes

Every primitive type has a corresponding Wrapper Class (e.g., int -> Integer). These allow you to treat primitives as objects, which is required for using the Java Collections Framework (like ArrayList).
java
// Autoboxing: Primitive to Wrapper!
Integer wrappedCount = 10; 

// Unboxing: Wrapper back to Primitive!
int rawCount = wrappedCount;

4. Modern Feature: Local Variable Type Inference (var)

Since Java 10, you can use the var keyword to let the compiler "infer" the type from the value you've assigned. This keeps your code clean and readable!
java
var message = "Hello, Java!"; // Infers String!
var scores = new ArrayList<Integer>(); // Infers ArrayList!

Summary

TypeExamplesDescription
byte/short127, 32767Small whole numbers
int/long10, 100000LMedium to large numbers
float/double1.2f, 3.14Decimals
char'X', '\n'Single Unicode character
booleantrue, falseLogic and branching
varvar x = 10;Modern type inference (Java 10+)
String"Hello, Ali!"Text (An Object, not a primitive!)
Best ForStoring and managing application data
PrimitiveStores values directly in memory
ReferenceStores the MEMORY ADDRESS of an object
Finalfinal int X = 10; (Value cannot be changed!)
ConstantUse static final for global constants
DefaultPrimitives have automatic defaults (0, null, etc.)
ScopeVariables ONLY exist inside the {} where they were created
NamingcamelCase (e.g., userScoreCount)
Casting(int) 3.14 (Explicitly convert double to int)
Promotionint is auto-promoted to double in math operations
OverflowJava doesn't protect against numbers exceeding their size!
UnicodeEvery char in Java is a 16-bit Unicode character
NullReference types can be null, primitives CANNOT!
Instanceinstanceof (Check if object is a specific type!)
CompareUse == for primitives; use .equals() for objects!
MemoryPrimitives = Stack; Objects = Heap
SafetyCompiler will NOT let you use an uninitialized variable!
Garbage COnly reference types are cleaned up by the GC