🔤 Object-Oriented Programming

Values & Data Types

⏱ 3 hr4 topicsInteractive
🎯 By the end: You can identify Java tokens, declare constants and variables, name the primitive types and their sizes, and explain implicit (widening) vs explicit (narrowing) type conversion.

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):

EscapeMeans
\nNew line
\tTab space
\\A backslash
\"A double quote
\'A single quote
Key points
  • 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:

TokenExample
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)
Key points
  • 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:

TypeHoldsSize
byteSmall whole numbers1 byte
shortWhole numbers2 bytes
intWhole numbers (common)4 bytes
longLarge whole numbers8 bytes
floatDecimal numbers4 bytes
doubleDecimal (more precise)8 bytes
charOne character2 bytes
booleantrue / false1 bit
Reference (non-primitive) types — like objects, arrays and Strings — don't hold the value directly; they hold a reference (the location) of the object. Class types you make are reference types.
Key points
  • 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)
DirectionSmaller type → larger typeLarger type → smaller type
Done byJava, automaticallyYou, 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)
Widening is automatic and safe; narrowing needs a cast (type) and may chop off the decimal part.
Key points
  • 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:

  1. Declares an int, a double, a char and a boolean and prints them.
  2. Declares a constant using final and shows it cannot be changed.
  3. Uses \n and \t in a print statement to format the output.
  4. 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.