Variables, Data Types and Type Casting
Unlike Python or JavaScript, Java is statically typed — you declare a variable’s type when you create it, the compiler checks that type is respected everywhere the variable is used, and it can never silently change to a different type later.
int age = 25;
double price = 19.99;
String name = "Tutoline";
boolean isActive = true;
Primitive types
int— a 32-bit whole number, the most commonly used integer typelong— a 64-bit whole number, for values too large forintdouble— a 64-bit decimal number, the default choice for decimalsfloat— a 32-bit decimal number, used less often thandoubleboolean— true/falsechar— a single character, written in single quotes like'A'
String isn’t a primitive type — it’s a full object, which is why it’s capitalized while every primitive type name is lowercase. This distinction matters more once you reach the object-oriented programming lesson, since String objects behave differently from primitives in a few subtle ways, including how equality comparisons work.
Why the compiler matters
The compiler checks your types before the program ever runs — trying to assign a String to an int variable is caught immediately as a compile error, rather than surfacing later as a confusing runtime bug the way it might in a dynamically typed language like Python or JavaScript. This upfront strictness is a large part of why Java is trusted for large, long-lived codebases with many contributors.
Type casting
double price = 19.99;
int rounded = (int) price; // 19 -- explicit cast, truncates the decimal
int whole = 10;
double asDouble = whole; // 10.0 -- implicit widening, no cast needed
Converting from a smaller type to a larger one (like int to double) happens automatically, since no information is lost. Converting from a larger, more precise type to a smaller one (like double to int) requires an explicit cast, because it’s a lossy conversion — the compiler wants you to acknowledge that on purpose.
Constants
final double TAX_RATE = 0.18;
// TAX_RATE = 0.20; -- this line would fail to compile
final is Java’s true constant keyword — unlike a naming convention, attempting to reassign a final variable is a genuine compile error, not just a style violation.