Programs work with values — numbers, characters, true/false. Java is careful about the type of each value. This chapter covers how Java represents and stores data (ICSE Class 9).
1Character set & escape sequences
A character set is the collection of characters a computer understands. Two standards:
- ASCII — an older 7/8-bit set covering English letters, digits and symbols (e.g. 'A' = 65).
- Unicode — a modern set covering the world's languages; Java uses Unicode (each char is 2 bytes).
Escape sequences
Some characters can't be typed directly inside a string, so Java uses escape sequences (a backslash + a letter):
| Escape | Means |
|---|---|
\n | New line |
\t | Tab space |
\\ | A backslash |
\" | A double quote |
\' | A single quote |
- A character set is the characters a computer understands: ASCII (older, English) and Unicode (modern, all languages — Java uses it).
- Escape sequences are a backslash + a letter for special characters.
- Common ones: \n (new line), \t (tab), \\ (backslash), \" (double quote).
2Java tokens, constants & variables
A token is the smallest meaningful unit of a Java program. There are five kinds:
| Token | Example |
|---|---|
| Keywords (reserved words) | class, int, if, public |
| Identifiers (names you choose) | marks, total, Student |
| Literals (fixed values) | 25, 3.14, 'A', "Hi" |
| Operators | + - * / = |
| Punctuators/Separators | ; , ( ) { } |
A variable stores a value that can change; a constant (declared final) cannot change after it's set:
int marks = 92; // variable final double PI = 3.14; // constant (cannot change)
- Tokens are the smallest units: keywords, identifiers, literals, operators, punctuators.
- Keywords are reserved (int, class); identifiers are names you choose (marks).
- A variable's value can change; a constant (final) cannot change once set.
3Primitive data types
Java has eight primitive (built-in) data types:
| Type | Holds | Size |
|---|---|---|
byte | Small whole numbers | 1 byte |
short | Whole numbers | 2 bytes |
int | Whole numbers (common) | 4 bytes |
long | Large whole numbers | 8 bytes |
float | Decimal numbers | 4 bytes |
double | Decimal (more precise) | 8 bytes |
char | One character | 2 bytes |
boolean | true / false | 1 bit |
- Java's 8 primitive types: byte, short, int, long (whole); float, double (decimal); char (1 char); boolean (true/false).
- Sizes: int = 4 bytes, long = 8, double = 8, char = 2 bytes, boolean = 1 bit.
- Reference (non-primitive) types — objects, arrays, Strings — hold a reference to the object, not the value itself.
4Type conversion
Sometimes a value of one type must become another. Two ways:
| Implicit (widening) | Explicit (narrowing) | |
|---|---|---|
| Direction | Smaller type → larger type | Larger type → smaller type |
| Done by | Java, automatically | You, with a cast |
| Safe? | Yes (no data lost) | Can lose data |
int a = 10; double b = a; // implicit: int -> double (widening) double x = 9.7; int y = (int) x; // explicit cast: double -> int, y = 9 (narrowing)
(type) and may chop off the decimal part.- Implicit conversion (widening) goes small→large type automatically and safely.
- Explicit conversion (narrowing) goes large→small using a cast (type) and may lose data.
- Example: (int) 9.7 gives 9 — the decimal part is dropped.
★ Practical: types in action
In BlueJ, write code that:
- Declares an int, a double, a char and a boolean and prints them.
- Declares a constant using final and shows it cannot be changed.
- Uses \n and \t in a print statement to format the output.
- Casts a double like 8.9 to an int and prints the result.
Ready for the chapter test?
Answer 5 questions. Score 60% or more to mark this chapter complete.
Start the test →💡 Log in to save your progress and earn the certificate.