By the end of this module, you will be able to:
In JavaScript, you can write:
let message = "Hello";
message = 42; // perfectly fine in JS
In Java, every variable has a fixed type decided at compile time. Once you declare a variable as a String, it stays a String forever.
String message = "Hello";
message = 42; // ❌ Compile error — type mismatch
This strictness feels like extra work at first, but it catches entire categories of bugs before your program even runs.
Java has 8 built-in primitive types. These are not objects — they hold raw values directly in memory.
| Type | Size | Example | JS Equivalent |
|---|---|---|---|
int |
32-bit | 42 |
number |
long |
64-bit | 9999999999L |
number (BigInt for large values) |
double |
64-bit decimal | 3.14 |
number |
float |
32-bit decimal | 3.14f |
number |
boolean |
true/false | true |
boolean |
char |
single character | 'A' |
string (single char) |
byte |
8-bit integer | 127 |
no direct equivalent |
short |
16-bit integer | 32000 |
no direct equivalent |
💡 Most of the time you'll use:
int,double,boolean,char, andString.
int age = 25;
double price = 19.99;
boolean isLoggedIn = false;
char grade = 'A'; // Note: single quotes for char
Everything that is not a primitive is a reference type — it stores a reference (memory address) to an object, not the value itself.
The most common reference type you'll use right away is String.
String name = "Alice";
String empty = null; // reference types can be null
⚠️ Primitives cannot be
null. Only reference types can. This distinction matters when you start seeingNullPointerExceptionerrors.
var (Java 10+)Java 10 introduced var so you don't always have to write the type explicitly — the compiler infers it for you.
var city = "Rotterdam"; // inferred as String
var count = 10; // inferred as int
var price = 9.99; // inferred as double
💡
varworks only for local variables (inside methods). Use explicit types for class fields. In this course we'll mostly use explicit types so you build the habit of thinking about types consciously.
Java uses camelCase for variables and methods — same as JavaScript.
int userAge = 30; // ✅ camelCase
String firstName = "Bob"; // ✅ camelCase
int user_age = 30; // ❌ not conventional in Java
Constants use UPPER_SNAKE_CASE and the final keyword (similar to const in JS):
final int MAX_USERS = 100;
final String APP_NAME = "MyApp";
When you declare a field (a variable at class level) without assigning a value, Java automatically gives it a default value. This does not apply to local variables inside methods — those must be explicitly initialised before use.
| Type | Default Value |
|---|---|
int, long, short, byte |
0 |
double, float |
0.0 |
boolean |
false |
char |
'\\\\u0000' (null character) |
Reference types (e.g. String) |
null |
public class Example {
int count; // default: 0
boolean active; // default: false
String name; // default: null
public void show() {
System.out.println(count); // 0
System.out.println(active); // false
System.out.println(name); // null
}
}
⚠️ Local variables (inside methods) have no default — the compiler will refuse to compile if you try to use one before assigning it.
public static void main(String[] args) {
int x;
System.out.println(x); // ❌ Compile error: variable x might not have been initialized
}
Sometimes you need to convert between types. Java distinguishes between two kinds of casting based on whether data could be lost.
When converting to a larger type, no data is lost so Java does it automatically:
int myInt = 100;
long myLong = myInt; // int → long, automatic ✅
double myDouble = myLong; // long → double, automatic ✅
// The widening order:
// byte → short → int → long → float → double
When converting to a smaller type, data could be lost, so you must cast explicitly:
double pi = 3.14159;
int truncated = (int) pi; // double → int, decimal part dropped
System.out.println(truncated); // 3
long bigNumber = 1234567890123L;
int smaller = (int) bigNumber; // ⚠️ data loss possible if value doesn't fit
💡 The cast operator
(int)tells the compiler: "I know data might be lost here — I accept that."
Every integer type has a fixed size. If a value exceeds its maximum, it silently wraps around to the opposite end — Java does not throw an error.
int max = Integer.MAX_VALUE; // 2,147,483,647
System.out.println(max); // 2147483647
System.out.println(max + 1); // -2147483648 ← wraps around!
This is a silent bug — no crash, no warning, just a wrong answer. Use long when your values might exceed int range:
long safeValue = (long) Integer.MAX_VALUE + 1;
System.out.println(safeValue); // 2147483648 ✅
Computers represent decimal numbers in binary, which means some values cannot be stored exactly. This can produce surprising results:
double a = 0.1 + 0.2;
System.out.println(a); // 0.30000000000000004 😱
System.out.println(a == 0.3); // false
⚠️ Never use
==to comparedoublevalues. Instead, check if they are close enough:
double result = 0.1 + 0.2;
double expected = 0.3;
double tolerance = 0.0001;
if (Math.abs(result - expected) < tolerance) {
System.out.println("Close enough — values are equal");
}
For financial calculations where precision is critical (e.g. money), use BigDecimal instead of double. You'll encounter this in later weeks.
You'll use this constantly while learning:
System.out.println("Hello, World!"); // prints + new line
System.out.print("No newline here"); // prints, no new line
System.out.println("Age: " + 25); // string concatenation
🔍 Coming from JS,
System.out.println()is Java's equivalent ofconsole.log().