Skip to content
Computer ApplicationsICSE Class 10 · Values and Data Types

Primitive Data Types and Type Conversion in Java (ICSE Class 10)

Updated 17 October 2026 · Fact-checked

Java has eight primitive types: byte, short, int, long (whole numbers), float, double (real numbers), char (a character) and boolean (true or false). Implicit conversion is done automatically when a smaller type is stored in a larger one (int to double). Explicit conversion, called casting, is written by the programmer as (type) value and is needed when a larger type goes into a smaller one.

Moving right up the ladder is automatic; moving back left needs a cast like (int), and it cuts off the fraction instead of rounding.

Illustration drawn for QuestionBuddy.

Understand Primitive Data Types and Type Conversion

Primitive types and sizes. A primitive type holds one simple value. Non-primitive (composite) types such as String, arrays and classes hold references to objects.

  • byte: 8 bits (1 byte), range -128 to 127
  • short: 16 bits (2 bytes)
  • int: 32 bits (4 bytes), range -2147483648 to 2147483647
  • long: 64 bits (8 bytes)
  • float: 32 bits (4 bytes), about 6 to 7 digits of accuracy
  • double: 64 bits (8 bytes), about 15 digits of accuracy
  • char: 16 bits (2 bytes), one Unicode character
  • boolean: only true or false (textbooks usually list 1 bit; the language does not fix the storage)

Whole-number literals are int unless written with L (100L is long). Real-number literals are double unless written with f (2.5f is float).

Implicit (automatic) conversion. Java converts a value to a wider type on its own:
byte → short → int → long → float → double and char → int. In double d = 5; the int 5 becomes 5.0. In an expression, operands are converted to the widest type present: 3 + 2.5 is double 5.5.

Explicit conversion (type casting). Going to a narrower type may lose information, so the compiler refuses it unless you cast: int x = (int) 3.99; gives 3 (the fraction is cut, not rounded). Without the cast you get the error *possible lossy conversion from double to int*. float f = 3.14; is also an error because 3.14 is a double; write 3.14f or (float)3.14.

Integer division trap. If both operands are int, the result is int: 7 / 2 is 3. The cast must be applied before the division: (double) 7 / 2 is 3.5 because the cast binds to 7 first, whereas (double)(7 / 2) is 3.0 because the division is already done. double d = 7 / 2; stores 3.0.

Characters. char and int convert freely: int n = 'A'; gives 65 and char c = 66; gives 'B' (a constant that fits).

Primitive Data Types and Type Conversion: key facts

Sizes of primitive types
byte 8 bits1 byte
short 16 bits2 bytes
int 32 bits4 bytes
long 64 bits8 bytes
float 32 bits4 bytes
double 64 bits8 bytes
char 16 bits2 bytes
boolean holds true or false only.
Implicit conversion order
  • byte
  • short
  • int
  • long
  • float
  • double (small to large)
  • char to int
Automatic only towards the right.
Explicit conversion
  • (targetType) expression
  • (int) 3.99 gives 3
  • (double) 7 / 2 gives 3.5
  • (double)(7 / 2) gives 3.0
Casting a double to int cuts the fraction.
Literal suffixes
  • 100L is long
  • 2.5f is float
  • 2.5 is double
  • 100 is int
float f = 2.5; is an error.

How to answer Primitive Data Types and Type Conversion questions

Use this routine for conversion and output questions.

  1. 1Write the type of every operand and literal (int, double, char and so on).
  2. 2Apply the cast, if any, to the item immediately after it; brackets change what is cast.
  3. 3Evaluate each operation in precedence order, using the widest type of its two operands to decide the result type.
  4. 4For int / int, cut the fraction; for % give the remainder.
  5. 5On assignment, check the target type: wider is fine, narrower needs a cast, otherwise state the lossy-conversion error.

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.

Integer division and casting

Suppose a canteen has 7 sandwiches to share equally among 2 classes and the program stores the result in a variable.

What do int share = 7 / 2; and double exact = (double) 7 / 2; store?

Try it first, then show the solutionSolution
  1. 7 / 2 with two ints gives 3, so share is 3.
  2. (double) 7 turns 7 into 7.0 first, then 7.0 / 2 is 3.5, so exact is 3.5.

Answer: share = 3 and exact = 3.5.

Memory hook: Cast first, divide second.

Tricks to remember 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 large is free

    Pouring from a small glass into a big one never spills (implicit); from big to small needs care (cast).

    Use it when: Deciding if a cast is needed.

  • Bytes: 1 2 4 8 4 8 2

    byte 1, short 2, int 4, long 8, float 4, double 8, char 2.

    Use it when: Size questions.

  • Cut, never round

    A cast from double to int just chops the decimal part.

    Use it when: (int) 3.99 and (int) -2.7 give 3 and -2.

Primitive Data Types and Type Conversion: solved examples

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

Example 1: Implicit or explicit · 3 marks · Short answerBoard level

State whether each statement uses implicit or explicit conversion and give the value stored:
(i) double d = 7;
(ii) int k = (int) 9.8;
(iii) float f = 'a';

Try it first, then show the solutionSolution
  1. (i) int to double happens automatically: implicit; d is 7.0. (1 mark)
  2. (ii) A cast is written by the programmer: explicit; the fraction is cut and k is 9. (1 mark)
  3. (iii) char is first treated as its code 97 and widened to float: implicit; f is 97.0. (1 mark)

Answer: (i) implicit, 7.0 (ii) explicit, 9 (iii) implicit, 97.0

Example 2: Division and casts · 4 marks · Short answerHarder board level

Write the output:

int a = 7, b = 2;
System.out.println(a / b);
System.out.println((double) a / b);
System.out.println((double) (a / b));
System.out.println(a % b);
Try it first, then show the solutionSolution
  1. a / b is int division: 7 / 2 = 3. (1 mark)
  2. (double) a makes 7.0, and 7.0 / 2 = 3.5. (1 mark)
  3. (double)(a / b) first does 7 / 2 = 3, then converts to 3.0. (1 mark)
  4. a % b is the remainder of 7 divided by 2: 1. (1 mark)

Answer: 3
3.5
3.0
1

Example 3: Which statements compile · 4 marks · Short answerHarder board level

State which statements compile. For those that do not, give the error and a correction.
(i) int x = 5.5;
(ii) double y = 5;
(iii) float f = 3.14;
(iv) long l = 100;
(v) char c = 65;
(vi) int n = 'A';

Try it first, then show the solutionSolution
  1. (i) Error: possible lossy conversion from double to int; write int x = (int) 5.5; (stores 5). (1 mark)
  2. (ii) Compiles: implicit conversion, y = 5.0. (1 mark)
  3. (iii) Error: 3.14 is a double, so it cannot be stored in a float; write 3.14f. (1 mark)
  4. (iv), (v) and (vi) compile: l = 100, c = 'A' (code 65) and n = 65. (1 mark)

Answer: (i) and (iii) do not compile; the rest do.

Example 4: Case study: an average · 4 marks · Case-basedHOTS / competency

A teacher has four marks stored as int variables with values 78, 85, 90 and 92, and sum = 345 is their total. She writes:

int avg1 = sum / 4;
double avg2 = sum / 4;
double avg3 = sum / 4.0;
double avg4 = (double) sum / 4;
int avg5 = (int) avg3;

(i) State the value of each variable. (3)
(ii) Which statements give the correct average, and why do the others not? (1)

Try it first, then show the solutionSolution
  1. (i) avg1: 345 / 4 = 86 (int division, fraction cut). avg2: 345 / 4 is done in int first = 86, then stored as 86.0.
  2. avg3: 345 / 4.0 is double division = 86.25. avg4: (double) 345 = 345.0, then / 4 = 86.25. avg5: (int) 86.25 = 86.
  3. (ii) avg3 and avg4 are correct (86.25). avg1, avg2 and avg5 lose the fraction because the division was between two ints before any conversion.

Answer: (i) avg1 = 86, avg2 = 86.0, avg3 = 86.25, avg4 = 86.25, avg5 = 86 (ii) avg3 and avg4, because one operand is double before the division.

Example 5: Chain of conversions · 3 marks · Short answerHarder board level

Write the output:

int a = 10;
long b = a;
float f = b;
System.out.println(f);
System.out.println((int) f / 4);
Try it first, then show the solutionSolution
  1. a (int) goes to long b = 10 and then to float f = 10.0 by implicit conversion. (1 mark)
  2. println(f) prints a float with its decimal point: 10.0. (1 mark)
  3. (int) f is 10, and 10 / 4 is int division: 2. (1 mark)

Answer: 10.0
2

Where students lose marks in Primitive Data Types and Type Conversion

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

  • Mistake: Believing 7 / 2 gives 3.5 because the target is double

    The division is done on two ints first; the conversion to double happens only after.

    Wrong working

    double d = 7 / 2; d = 3.5

    Right working

    double d = 7 / 2; d = 3.0

    Do this: Make one operand double before dividing: 7 / 2.0.

    Usually costs: 1 mark

  • Mistake: Rounding in a cast

    A cast from double to int cuts off the fraction.

    Wrong working

    (int) 3.99 = 4

    Right working

    (int) 3.99 = 3

    Do this: Chop, never round.

    Usually costs: 1 mark

  • Mistake: Putting the cast in the wrong place

    (double) (a / b) casts after the integer division; (double) a / b casts before.

    Wrong working

    (double)(7 / 2) = 3.5

    Right working

    (double)(7 / 2) = 3.0

    Do this: Work inside the brackets first.

    Usually costs: 1 mark

Exam tips

  • Learn the size table (bits and bytes) once; the syllabus asks for both.
  • For output questions with division, mark the type of each operand before calculating.
  • Mention the exact error words: "possible lossy conversion from double to int".
  • Write float literals with f and long literals with L when giving corrected code.

Practice questions on 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

    How many bytes does a variable of type int occupy in Java?
    (a) 2 (b) 4 (c) 8 (d) 1

    Hint

    32 bits.

    Answer

    (b) 4

  2. Q2 · 1 mark · Very short answerBoard level

    What kind of type conversion takes place in double d = 5;?

    Hint

    No cast is written.

    Answer

    Implicit (automatic, widening) conversion: int to double, so d is 5.0.

  3. Q3 · 2 marks · Short answerHarder board level

    Write the output:

    int x = (int) 7.9;
    double y = (double) x / 2;
    System.out.println(x + " " + y);
    Hint

    The cast cuts the fraction.

    Answer

    7 3.5

  4. Q4 · 3 marks · Short answerHarder board level

    The statements int n = 12.75; and float f = 2.5; give compile-time errors. Name the error and rewrite each so that it compiles. State the values stored.

    Hint

    Double literal to a narrower type.

    Answer

    Error: possible lossy conversion from double to int (and to float). Corrected: int n = (int) 12.75; stores 12. float f = 2.5f; stores 2.5.

  5. Q5 · 1 mark · Assertion-reasonHOTS / competency

    Assertion (A): After double d = 7 / 2; the variable d holds 3.0.
    Reason (R): 7 / 2 is evaluated as integer division before the result is assigned to d.
    (a) Both A and R are true and R is the correct explanation of A. (b) Both A and R are true but R is not the correct explanation of A. (c) A is true but R is false. (d) A is false but R is true.

    Hint

    When does the conversion to double happen?

    Answer

    (a) Both are true and R explains A.

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.Name the eight primitive types of Java.Show answer

    byte, short, int, long, float, double, char and boolean.

  2. 2.What is the size of a double in bits?Show answer

    64 bits (8 bytes).

  3. 3.What does (int) 3.99 give?Show answer

    3

  4. 4.What is 7 / 2 in Java?Show answer

    3 (integer division).

  5. 5.What type is the literal 2.5?Show answer

    double

  6. 6.What is the other name for explicit conversion?Show answer

    Type casting.

Primitive Data Types and Type Conversion: frequently asked questions

Why does float f = 3.14; give an error?

The literal 3.14 is a double, which is larger than float. Write 3.14f or cast with (float).

Is String a primitive type?

No. String is a class, so it is a non-primitive (composite) type.

Is char arithmetic done as int?

Yes. In an expression a char is converted to int, so 'a' + 1 is the int 98.