Atlas

Roadmap

Fundamentals

Variables & Types

Mar 24, 2026

Static Typing

Java is statically typed — every variable must have a declared type, and that type cannot change:

int age = 25;
String name = "Alan";
boolean isStudent = true;
java

Compare this to Python, where you just write age = 25. In Java, the type declaration (int) is required. If you try to assign the wrong type, the compiler catches it before the program ever runs.

Primitive Types

Java has 8 primitive types built into the language. These are not objects — they hold values directly in memory:

TypeSizeExampleNotes
int32-bit42, -7Most common integer type
long64-bit123456789LUse L suffix
double64-bit3.14Most common decimal type
float32-bit3.14fUse f suffix
boolean1-bittrue, falseOnly two values
char16-bit'A'Single character, single quotes
byte8-bit127Rarely used directly
short16-bit32000Rarely used directly
int score = 95;
double gpa = 3.8;
boolean graduated = false;
char grade = 'A';
java

Reference Types

Everything that is not a primitive is a reference type — the variable holds a reference (pointer) to an object in memory, not the value itself.

The most important reference type to know first is String:

String major = "Computer Science";
String greeting = "Hello, " + major;  // concatenation with +
java

String is a class, not a primitive. This has practical consequences:

String a = "hello";
String b = "hello";

a == b       // unreliable — compares references, not values
a.equals(b)  // true — always use .equals() for String comparison
java

Type Conversion

Widening (safe, automatic) — going from smaller to larger type:

int x = 42;
double d = x;   // int → double, automatic
java

Narrowing (requires explicit cast) — going from larger to smaller type:

double pi = 3.14159;
int truncated = (int) pi;   // 3 — decimal part is dropped, not rounded
java

var (Java 10+)

Java 10 introduced var for local variable type inference — the compiler infers the type from the value:

var name = "Alan";    // inferred as String
var age = 25;         // inferred as int
var gpa = 3.8;        // inferred as double
java

var only works for local variables (inside methods), not for fields or parameters. The type is still static — it's just inferred at compile time instead of written out.

Constants

Use final to declare a variable that cannot be reassigned. By convention, constants use ALL_CAPS:

final int MAX_SCORE = 100;
final String APP_NAME = "Atlas";

MAX_SCORE = 99;  // compile error
java

Key Questions

Q: What is the difference between primitive types and reference types in Java?

Primitives (int, double, boolean, etc.) hold their value directly in memory and are stored on the stack. Reference types hold a memory address pointing to an object on the heap. Primitives cannot be null; reference types can. Primitives are compared with ==; reference types should be compared with .equals().

Q: Why should you use .equals() instead of == for String comparison?

== compares references — whether two variables point to the same object in memory. Two String variables with the same content may be different objects, so == can return false even when the values are identical. .equals() compares the actual character content, which is almost always what you want.

Q: What is the difference between int and Integer in Java?

int is a primitive type — it holds the value directly and cannot be null. Integer is a wrapper class — it wraps an int in an object, can be null, and provides utility methods like Integer.parseInt(). Java automatically converts between the two (autoboxing/unboxing), but Integer has more overhead since it's a heap object.

Q: What is the difference between widening and narrowing type conversion?

Widening goes from a smaller type to a larger one (e.g., int to double) and happens automatically because there is no risk of data loss. Narrowing goes from a larger type to a smaller one (e.g., double to int) and requires an explicit cast — the decimal part is truncated, not rounded, so data can be lost.

Q: What is var in Java and when can you use it?

var (introduced in Java 10) lets the compiler infer the type of a local variable from the assigned value. It can only be used for local variables inside methods — not for fields, method parameters, or return types. The type is still static and determined at compile time; var just saves you from writing it out.

Q: What does final mean when applied to a variable?

final means the variable can only be assigned once — after that, any attempt to reassign it is a compile-time error. For primitives, this makes the value constant. For reference types, it means the reference cannot point to a different object, but the object's contents can still be mutated. By convention, final constants are named in ALL_CAPS.

Q: What are the 8 primitive types in Java?

byte, short, int, long (integers of increasing size), float, double (decimals), char (single character), and boolean. The most commonly used are int, double, boolean, and char. long literals need an L suffix and float literals need an f suffix to distinguish them from int and double.