Skip to content
Computer ApplicationsICSE Class 9 · Identifiers and Literals

Variables, Constants, Primitive Data Types and Type Conversion in Java (ICSE Class 9)

Updated 18 October 2026 · Fact-checked

A variable is a named memory location whose value can change; a constant, declared with final, cannot. Primitive data types have fixed sizes: byte 8 bits, short 16, int 32, long 64, float 32, double 64, char 16 and boolean. Java converts a smaller type to a larger one automatically (implicit conversion), while converting a larger type to a smaller one needs a cast, for example (int) 7.9, which gives 7 and may lose data.

Moving right to a larger type is automatic; moving left needs a cast like (int) 7.9, which drops the fraction and gives 7.

Illustration drawn for QuestionBuddy.

Understand Variables, Constants, Primitive Data Types and Type Conversion

Variable and constant. A variable is a named memory location declared with a type: int marks = 85; Its value may change. A constant is declared with final and cannot be changed after assignment: final double RATE = 8.5; Literals are also constants.

Primitive data types.

  • byte: 8 bits (1 byte); range -128 to 127.
  • short: 16 bits (2 bytes); range -32,768 to 32,767.
  • int: 32 bits (4 bytes); largest value 2,147,483,647.
  • long: 64 bits (8 bytes).
  • float: 32 bits (4 bytes); a literal needs f, as in 2.5f.
  • double: 64 bits (8 bytes); the default for decimal literals.
  • char: 16 bits (2 bytes); one Unicode character.
  • boolean: true or false.

Non-primitive (reference) types are built from primitives and hold a reference to an object: classes such as String and Scanner, arrays and interfaces. Default values of data members: 0 for numeric types, 0.0 for float and double, the null character for char, false for boolean and null for references.

Implicit conversion (type promotion). Java converts a smaller type to a larger one automatically, in the order byte, short, int, long, float, double (and char to int). No data is cut down. In a mixed expression each operand is promoted to the largest type present. Examples: int i = 7; double d = i; gives 7.0, and 'A' + 1 is 66. When both operands of / are int, integer division drops the fraction: 5 / 2 is 2, while 5 / 2.0 is 2.5.

Explicit conversion (type casting). The programmer writes the target type in brackets: (type) value. It is needed to go from a larger type to a smaller one and data may be lost. (int) 7.9 gives 7 (cut off, not rounded). (char) 98 gives 'b'. (double) 5 / 2 gives 2.5, because the cast applies to 5 first, whereas (double) (5 / 2) gives 2.0. (byte) 130 gives -126 because 130 does not fit in a byte and wraps around.

Compiler rules. int x = 3.7; is a syntax error ("possible lossy conversion from double to int"); write int x = (int) 3.7; float f = 2.5; is also an error because 2.5 is a double; write 2.5f.

Implicit versus explicit. Implicit is automatic and widening; explicit is written by the programmer and usually narrowing and may lose data.

Neighbouring topics: the character set, ASCII, escape sequences and tokens are on the previous page of this chapter.

Variables, Constants, Primitive Data Types and Type Conversion: key facts

Sizes
  • byte 8 bits
  • short 16
  • int 32
  • long 64
  • float 32
  • double 64
  • char 16
Divide by 8 for bytes.
Widening order
byte, short, int, long, float, double | char goes to int
Done automatically.
Cast
  • (type) value
  • (int) 7.9 gives 7
  • (double) 5 / 2 gives 2.5
  • (double) (5 / 2) gives 2.0
The cast applies to the nearest operand.
Constant
final type NAME = value; | cannot be changed later
Names in capitals by convention.

Variables, Constants, Primitive Data Types and Type Conversion in real life

The idea, met in an everyday situation. Answer the question in your own words, then compare with the model answer. The one-line hook is what to remember.

Pouring into smaller containers

Suppose you pour 1.5 litres of juice from a big jug into a 1-litre bottle.

How does this compare with casting a double to an int?

Try it first, then show the solutionSolution
  1. The bottle is the smaller type and the extra juice is lost.
  2. (int) 1.5 keeps only 1 and drops the fraction.
  3. Pouring a small bottle into a big jug loses nothing, like implicit conversion.

Answer: Casting to a smaller type loses data, like overflowing the bottle; widening loses nothing.

Memory hook: Big to small spills; small to big is safe.

Tricks to remember Variables, Constants, Primitive Data Types and Type Conversion

Short memory aids. Say each one aloud once, then use it on a question the same day.

  • Small to big is free

    Smaller to larger conversions are automatic; larger to smaller needs a cast.

    Use it when: Implicit versus explicit.

  • Cast cuts, never rounds

    (int) 7.9 is 7, not 8.

    Use it when: Casting questions.

  • Double is the default

    A decimal literal is a double unless it ends in f.

    Use it when: float errors.

Variables, Constants, Primitive Data Types and Type Conversion: solved examples

Ordered from board level to HOTS. Try each one before you open the solution.

Example 1: Size of a type · 1 mark · MCQBoard level

How many bits does a double occupy?
(a) 16 (b) 32 (c) 64 (d) 8

Try it first, then show the solutionSolution
  1. double is a 64-bit (8-byte) type.

Answer: (c) 64

Example 2: Division and casting · 3 marks · Short answerHarder board level

Find the output:

int a = 7, b = 2;
double d = a / b;
double e = (double) a / b;
int f = (int) 9.99;
System.out.println(d + " " + e + " " + f);
Try it first, then show the solutionSolution
  1. a / b is integer division = 3, stored in the double as 3.0. (1 mark)
  2. (double) a / b = 7.0 / 2 = 3.5. (1 mark)
  3. (int) 9.99 cuts off the fraction: 9. Output: 3.0 3.5 9. (1 mark)

Answer: 3.0 3.5 9

Example 3: Promotion of char · 3 marks · Short answerHarder board level

Find the output:

char ch = 'C';
int n = ch + 1;
char nx = (char) (ch + 1);
System.out.println(n + " " + nx + " " + (ch + 1));
Try it first, then show the solutionSolution
  1. 'C' is 65 + 2 = 67; ch + 1 is 68, an int. (1 mark)
  2. (char) (ch + 1) converts 68 back to the character D. (1 mark)
  3. The last part is the int 68 again. Output: 68 D 68. (1 mark)

Answer: 68 D 68

Example 4: Case study: marks that do not fit · 4 marks · Case-basedHOTS / competency

A program stores values in small types:

byte b = (byte) 200;
short s = (short) 40000;
System.out.println(b + " " + s);

(i) State the range of a byte. (1)
(ii) Find the output. (2)
(iii) Suggest a type that avoids the problem. (1)

Try it first, then show the solutionSolution
  1. (i) A byte ranges from -128 to 127.
  2. (ii) 200 does not fit in a byte; 200 - 256 = -56. 40000 does not fit in a short; 40000 - 65536 = -25536. Output: -56 -25536.
  3. (iii) Use int (or a larger type) for such values.

Answer: (i) -128 to 127 (ii) -56 -25536 (iii) int

Example 5: Choose the type · 3 marks · Short answerHarder board level

Choose a suitable primitive type for each and give a reason: (i) a letter grade; (ii) a price such as 45.75; (iii) whether a student has passed.

Try it first, then show the solutionSolution
  1. (i) char: one character. (1 mark)
  2. (ii) double: a decimal value with the default decimal type. (1 mark)
  3. (iii) boolean: only two outcomes, true or false. (1 mark)

Answer: (i) char (ii) double (iii) boolean

Where students lose marks in Variables, Constants, Primitive Data Types and Type Conversion

The slips examiners see most often, and what to write instead.

  • Mistake: Rounding in a cast

    Casting is mistaken for rounding.

    Wrong working

    (int) 7.9 = 8

    Right working

    (int) 7.9 = 7

    Do this: A cast cuts off the fraction.

    Usually costs: 1 mark

  • Mistake: Bracket placement

    The cast is applied to the wrong part.

    Wrong working

    (double) (5 / 2) = 2.5

    Right working

    (double) (5 / 2) = 2.0 and (double) 5 / 2 = 2.5

    Do this: Find the nearest operand.

    Usually costs: 1 mark

  • Mistake: Mixing sizes

    Bits and bytes are confused.

    Wrong working

    int: 32 bytes

    Right working

    int: 32 bits = 4 bytes

    Do this: Divide bits by 8.

    Usually costs: 1 mark

Exam tips

  • Write sizes in both bits and bytes.
  • Trace casts from the inside outwards.
  • State that implicit conversion loses no data and explicit may.
  • Remember char takes its code in arithmetic.

Practice questions on Variables, Constants, Primitive Data Types and Type Conversion

Write each answer on paper first. Open the hint only if you are stuck, and the answer only when you have finished.

  1. Q1 · 1 mark · MCQBoard level

    Which type is used for true or false values?
    (a) char (b) byte (c) boolean (d) short

    Hint

    Two possible values.

    Answer

    (c) boolean

  2. Q2 · 1 mark · Very short answerBoard level

    What is the type of the literal 3.14 and what is its size in bits?

    Hint

    Default type of decimal literals.

    Answer

    double, 64 bits.

  3. Q3 · 2 marks · Short answerHarder board level

    Find the output:

    System.out.println(5 / 2 * 2.0 + " " + 5 / 2.0 * 2);
    Hint

    Integer division first in the first part.

    Answer

    4.0 5.0 (5 / 2 = 2, then 2 * 2.0 = 4.0; 5 / 2.0 = 2.5, then 2.5 * 2 = 5.0)

  4. Q4 · 3 marks · Short answerHarder board level

    Explain why int x = 3.7; gives an error and how to correct it. What value does x get?

    Hint

    Which type is 3.7?

    Answer

    3.7 is a double and a double cannot be stored in an int without a cast (possible lossy conversion). Write int x = (int) 3.7; x becomes 3.

  5. Q5 · 3 marks · Short answerHOTS / competency

    A student writes float f = 2.5; and the compiler reports an error. Explain and give two corrections.

    Hint

    What is the default decimal type?

    Answer

    2.5 is a double literal and cannot be stored in a float without a cast. Corrections: float f = 2.5f; or double f = 2.5;

Test your recall

Answer in your head or on paper first, then open the card. Pulling an answer out of memory makes it stick far better than re-reading.

  1. 1.Size of int in bytes?Show answer

    4

  2. 2.Size of char in bits?Show answer

    16

  3. 3.Keyword that makes a constant?Show answer

    final

  4. 4.Result of (int) 7.9?Show answer

    7

  5. 5.Default type of a decimal literal?Show answer

    double

  6. 6.Default value of an int data member?Show answer

    0

Variables, Constants, Primitive Data Types and Type Conversion: frequently asked questions

Is boolean 1 bit?

Java does not fix its size; textbooks often describe it as 1 bit.

Why does a byte wrap around?

The value does not fit in 8 bits, so only the lower bits are kept.

Is String a primitive type?

No. It is a class, a non-primitive (reference) type.