Sub topic 4
Variables and Types
In this chapter, you will learn what a variable is, how Java uses types, and how to store and print values. This is a core skill: almost every line of real Java code declares or uses a variable.
Prerequisite: You should have written and run the "Hello, World!" program from the previous chapter, and know how to compile and run Java from a terminal.
What Is a Variable?
A variable is a named storage box in the computer's memory. You put a value in the box, and you can read or change that value later using the name.
Java is a strongly typed language. That means every variable has a type — a rule that says what kind of value the box can hold. A variable made for whole numbers can only hold whole numbers. If you try to put the wrong kind of value in, the compiler (the program that checks your code) stops you with an error. This catches many mistakes before your program ever runs.
Declaring and Initializing Variables
Declaring a variable means telling Java its type and name. Initializing means giving it a first value. You can do both on one line.
int age = 30;
String name = "Aisha";
double height = 1.72;
boolean isStudent = true;
System.out.println(name);
System.out.println(age);Reading this code:
int age = 30;creates a variable namedageof typeint(whole number) with the value30. The semicolon ends the statement.Stringis the type for text. The text goes in double quotes.doubleholds decimal numbers like1.72.booleanholds onlytrueorfalse.
You can also declare first and assign later, or change the value any time:
int score;
score = 75; // first assignment
score = 80; // allowed: score now holds 80Java's Primitive Types
Java has eight built-in types called primitive types. These store simple values directly. The most useful ones:
| Type | Holds | Example |
|---|---|---|
| `int` | Whole numbers | `42` |
| `long` | Very large whole numbers | `9000000000L` |
| `double` | Decimal numbers | `3.14` |
| `float` | Smaller decimal numbers | `2.5f` |
| `boolean` | True or false | `true` |
| `char` | One character | `'A'` |
Note two details: long values end in L, float values end in f, and char uses single quotes while String uses double quotes.
There are also byte and short, small whole-number types, but beginners rarely need them.
Strings: Not a Primitive
String looks like a type in the table above but is different. String is a class — an object type, which we cover in a later chapter. You still use it the same way for now:
String greeting = "Hello";
System.out.println(greeting + " there");The + here joins text together. We call this concatenation.
Type Rules: What the Compiler Checks
Java enforces type rules at compile time. Try to compile this:
int count = "five";The compiler refuses: you cannot put text in an int box. That error is a feature — it prevents bugs.
One safe conversion happens automatically: putting a small type into a bigger one (like int into double) is fine, because no information is lost:
int whole = 7;
double bigger = whole; // fine: 7 becomes 7.0
System.out.println(bigger);Going the other way (a double into an int) can lose the decimal part, so you must ask for it explicitly with a cast:
double price = 9.99;
int rounded = (int) price; // you must write (int)
System.out.println(rounded); // prints 9 — the .99 is cut off, not roundedNaming Variables
Use names that describe the value. Java convention (the usual style) is camelCase: start lowercase, capitalize later words, like totalPrice or userName. Names cannot contain spaces or start with a digit.
Common Errors
- Using a variable before giving it a value.
int x; System.out.println(x);will not compile. - Misspelling the type, like
Intinstead ofint. Types are case-sensitive. - Wrong quotes:
"A"for a String is fine, but'A'for a String is an error.
Exercise
What is wrong with this line: String age = 30;?
Show answer
The type String can only hold text in double quotes. The value 30 is a whole number, so it must be declared as int age = 30; (or written as text: String age = "30";).
Exercise
What does this print? double d = 5.7; int n = (int) d; System.out.println(n);
Show answer
It prints 5. The cast (int) cuts off the decimal part; it does not round.