A variable is a named piece of memory that holds one value at a time. In Java you must say, up front, what kind of value it will hold, and that type can never change afterwards. Note also that = does not mean equals: it means take the value on the right and store it in the variable on the left, which is why count = count + 1 is an instruction rather than a false equation.
int score; // declaration: reserve an int-sized box named score
score = 42; // assignment: put 42 in it
int lives = 3; // declaration and initialization in one line
double average = 78.5;
char grade = 'B'; // single quotes for exactly one character
boolean passed = true;
String name = "Amira"; // double quotes for text| Type | Size | Range or purpose | Example |
|---|---|---|---|
byte | 8 bits | −128 to 127 | byte b = 100; |
short | 16 bits | −32 768 to 32 767 | short s = 30000; |
int | 32 bits | −2 147 483 648 to 2 147 483 647 | int n = 250000; |
long | 64 bits | about ±9.2 quintillion; needs an L suffix | long big = 9000000000L; |
float | 32 bits | decimals, about 7 digits of precision; needs an f suffix | float f = 2.5f; |
double | 64 bits | decimals, about 15 digits — Java's default | double d = 2.5; |
char | 16 bits | exactly one character, in single quotes | char c = 'A'; |
boolean | 1 bit of information | true or false only | boolean ok = false; |
In practice, Grade 11 work uses four of these constantly: int for counting, double for measuring, boolean for conditions, and char for a single letter or symbol. Because each type has a fixed number of bits, each also has a hard ceiling — an int tops out at , which is 2 147 483 647. Adding one more does not raise an error and does not produce a bigger number; it wraps around to −2 147 483 648 in silence. Any total that could pass about two billion belongs in a long.
- Variables and methods use camelCase:
totalMarks,averageSpeed,isFinished. - Classes use PascalCase, starting with a capital:
Hello,BankAccount. - Constants are marked
finaland written in UPPER_SNAKE_CASE:final double HST_RATE = 0.13; finalmeans the compiler rejects any later attempt to reassign — it protects the value and names it at the same time.- Names may contain letters, digits and the underscore, but may not start with a digit and may not be a Java keyword.
- Say what the value means.
totalMarksneeds no comment;tmandxneed one every time.
Integer division and modulo
This is the single most common surprise in Java. When both operands of / are integers, Java performs integer division: it divides and then throws away everything after the decimal point. It does not round. So 5 / 2 is 2, 9 / 10 is 0, and -7 / 2 is −3, because truncation moves toward zero. The decimal part is not lost by accident — it was never calculated. Its partner %, the modulo operator, returns what integer division left behind, so n % 2 == 0 tests for an even number and n % 10 peels off the last digit.
System.out.println(5 / 2); // 2 both int, so integer division
System.out.println(5 / 2.0); // 2.5 one is a double, so double division
System.out.println(9 / 10); // 0
System.out.println(17 % 5); // 2 remainder after 3 whole fives
System.out.println(125 / 60); // 2 whole minutes in 125 seconds
System.out.println(125 % 60); // 5 seconds left overWidening, narrowing and casting
Moving a value into a larger type is safe, so Java does it automatically. This is implicit widening, and it follows the chain byte → short → int → long → float → double, with char joining at int. Moving a value into a smaller type risks losing information, so Java refuses unless you explicitly demand it with a cast: the target type written in brackets in front of the value. The cast is you signing off on the loss.
int count = 7;
double d = count; // implicit widening: d is 7.0
double price = 9.99;
int dollars = (int) price; // explicit narrowing: 9, the .99 is discarded
int a = 7, b = 2;
double wrong = a / b; // 3.0 — the division already happened in int
double right = (double) a / b; // 3.5 — a becomes 7.0, so b widens to match
char letter = 'A';
int code = letter; // 65, the numeric code for 'A'
char next = (char) (code + 1); // 'B'