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.