Skip to content
Computer ScienceISC Class 12 · Primitive Values, Wrapper Classes, Types

Primitive Types, Type Casting and Wrapper Classes (ISC Class 12 Computer Science)

Updated 17 October 2026 · Draft, not yet fact-checked

Java has eight primitive types: byte, short, int, long, float, double, char and boolean. Smaller types convert automatically to larger ones (coercion, int to double); the reverse needs an explicit cast such as (int) 7.9, which truncates to 7. Each primitive has a wrapper class (Integer, Double, Character and so on) that turns it into an object.

Moving right up the ladder is automatic; moving left needs a cast, and (int) 7.9 cuts the fraction to give 7.

Illustration drawn for QuestionBuddy.

Understand Primitive Types, Type Casting and Wrapper Classes

The eight primitive types.

TypeSizeRange or valuesDefault
byte8 bits-128 to 1270
short16 bits-32768 to 327670
int32 bits-2147483648 to 21474836470
long64 bitsvery large whole numbers (suffix L)0L
float32 bitsdecimals, about 7 digits (suffix f)0.0f
double64 bitsdecimals, about 15 digits0.0
char16 bitsone Unicode character, 0 to 65535null character
booleantrue or false false

Automatic conversion (type coercion). In an expression the smaller type is promoted: byte, short and char become int; then long, float, double. So char + int is an int, int + double is a double, and 5 / 2 is the int 2 while 5 / 2.0 is 2.5. The order is byte, short, int, long, float, double.

Explicit casting. Going down needs (type): (int) 7.9 is 7 (the fraction is cut, not rounded); (char) 66 is B; (byte) 200 is -56 because the low 8 bits are kept (200 - 256). A compound assignment such as x += 3.7 has a built-in cast. byte b3 = b1 + b2; does not compile because the sum is an int. A decimal literal such as 3.14 is a double, so float f = 3.14; needs 3.14f or a cast.

Overflow. Integer.MAX_VALUE + 1 is -2147483648: the arithmetic wraps without an error. In long q = Integer.MAX_VALUE + 1; the addition is done in int first.

Wrapper classes. Byte, Short, Integer, Long, Float, Double, Character and Boolean, in java.lang. They hold a primitive inside an object. Autoboxing converts a primitive to its wrapper automatically (Integer b = 45;); unboxing converts back. Useful methods: Integer.parseInt("45") gives the int 45, Double.parseDouble("2.5"), Integer.valueOf(7), obj.intValue(), obj.doubleValue(), Character.isDigit(c), Character.isLetter(c), and the constants Integer.MAX_VALUE and Integer.MIN_VALUE. parseInt on non-numeric text throws NumberFormatException.

Class as a user-defined type. Writing a class creates a new type; an object variable of that class holds a reference, unlike a primitive that holds the value itself.

Variables, operators and precedence are in the next chapter.

Primitive Types, Type Casting and Wrapper Classes: key facts

Widening order
  • byte, short, int, long, float, double
  • char goes to int in arithmetic
  • Automatic in the direction of the arrow
Opposite direction needs a cast.
Casting
  • (int) 7.9 = 7
  • (char) 66 = B
  • (byte) 200 = -56
  • (int) 'A' = 65
Casting a decimal to int truncates, never rounds.
Integer range
  • byte -128 to 127
  • short -32768 to 32767
  • int -2147483648 to 2147483647
  • MAX_VALUE + 1 = MIN_VALUE
Overflow is silent.
Wrapper methods
Integer.parseInts
Double.parseDoubles
obj.intValue()
Character.isDigitc
NumberFormatException for bad text.

How to answer Primitive Types, Type Casting and Wrapper Classes questions

Use this routine for type and output questions.

  1. 1Find the type of each operand; apply integer division when both sides are int.
  2. 2Promote in the order byte, short, int, long, float, double; char behaves as int in arithmetic.
  3. 3Apply a cast only to what it brackets: (double) a / b casts a first, while (double) (a / b) casts after the integer division.
  4. 4Narrow with a cast and work out the truncation, or the wrap-around for byte and int.
  5. 5For wrappers, decide whether the call parses a String or converts an object.
  6. 6Print with the correct type: a double prints with a decimal point.

Primitive Types, Type Casting and Wrapper Classes 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.

Average of two scores

Suppose a score app stores two quiz marks 7 and 8 as ints and shows their average.

What are the results of (7 + 8) / 2 and (7 + 8) / 2.0?

Try it first, then show the solutionSolution
  1. The first has an int on both sides, so integer division gives 7.
  2. 15 / 2.0 promotes to double and gives 7.5.

Answer: 7 and 7.5

Memory hook: One decimal operand turns the whole division into decimal division.

Reading a quantity typed as text

Suppose a form gives a quantity as the String "45" and the program must add 5.

What do "45" + 5 and Integer.parseInt("45") + 5 give?

Try it first, then show the solutionSolution
  1. String plus int joins text: "455".
  2. parseInt converts first: 45 + 5 = 50.

Answer: "455" and 50

Memory hook: Convert text to a number before doing arithmetic.

Tricks to remember Primitive Types, Type Casting and Wrapper Classes

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

  • Small to big is free

    byte, short, int, long, float, double: moving right needs no cast.

    Use it when: Coercion questions.

  • Cut, never round

    (int) 9.99 is 9. The casting just drops the fraction.

    Use it when: Narrowing casts.

  • Wrapper names

    All wrappers are the type name with a capital, except Integer and Character.

    Use it when: Recalling wrapper classes.

  • Bracket decides

    (double) a / b casts a; (double) (a / b) casts the answer.

    Use it when: Division output questions.

Primitive Types, Type Casting and Wrapper Classes: solved examples

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

Example 1: Wrapper of char · 1 mark · MCQBoard level

The wrapper class for the primitive type char is
(a) Char (b) Character (c) String (d) Chr

Try it first, then show the solutionSolution
  1. Wrapper names are Byte, Short, Integer, Long, Float, Double, Character and Boolean.

Answer: (b) Character

Example 2: Where the cast is placed · 3 marks · Short answerHarder board level

Write the output:

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

Answer: 3.0 3.5 3.0

Example 3: char arithmetic · 3 marks · Short answerHarder board level

Write the output (the Unicode value of A is 65):

char c = 'A';
int n = c + 2;
char m = (char) (c + 2);
c++;
System.out.println(n + " " + m + " " + c);
Try it first, then show the solutionSolution
  1. c + 2 is int arithmetic: 65 + 2 = 67, so n = 67. (1 mark)
  2. The cast (char) 67 is C. (1 mark)
  3. c++ moves A to B. Output: 67 C B. (1 mark)

Answer: 67 C B

Example 4: Narrowing conversions · 4 marks · Short answerHarder board level

Write the output:

double x = 9.99;
int i = (int) x;
long big = 3000000000L;
int j = (int) big;
byte b = (byte) 200;
System.out.println(i + " " + j + " " + b);
Try it first, then show the solutionSolution
  1. (int) 9.99 truncates to 9. (1 mark)
  2. 3000000000 does not fit in an int (maximum 2147483647), so it wraps: 3000000000 - 4294967296 = -1294967296. (1.5 marks)
  3. (byte) 200 wraps: 200 - 256 = -56. (1 mark)
  4. Output: 9 -1294967296 -56. (0.5 mark)

Answer: 9 -1294967296 -56

Example 5: Case: parsing and boxing · 4 marks · Case-basedHOTS / competency

Suppose a stock program receives the quantity as text and writes:

String s = "45";
int n = Integer.parseInt(s);
Integer boxed = n;
double dv = boxed.doubleValue();
System.out.println((s + 5) + " " + (n + 5) + " " + dv);

(i) Write the output. (2 marks)
(ii) Name the conversion in the line Integer boxed = n;. (1 mark)
(iii) What happens if s is "4a5"? (1 mark)

Try it first, then show the solutionSolution
  1. (i) s + 5 joins the text: 455. n + 5 = 50. dv = 45.0. Output: 455 50 45.0
  2. (ii) Autoboxing of an int into an Integer object.
  3. (iii) Integer.parseInt("4a5") throws NumberFormatException.

Answer: (i) 455 50 45.0 (ii) autoboxing (iii) NumberFormatException

Where students lose marks in Primitive Types, Type Casting and Wrapper Classes

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

  • Mistake: Rounding in a cast

    A cast to int drops the fraction.

    Wrong working

    (int) 9.99 = 10

    Right working

    (int) 9.99 = 9

    Do this: Truncate towards zero.

    Usually costs: 1 mark

  • Mistake: Integer division unnoticed

    Two int operands give an int result before assignment to double.

    Wrong working

    double d = 7 / 2; gives 3.5

    Right working

    double d = 7 / 2; gives 3.0

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

    Usually costs: 1 mark

  • Mistake: Printing char arithmetic as a char

    c + 2 is an int, so the Unicode number is printed unless cast.

    Wrong working

    System.out.println('A' + 2) prints C

    Right working

    System.out.println('A' + 2) prints 67

    Do this: Use (char) (c + 2) to get C.

    Usually costs: 1 mark

Exam tips

  • Write the type of each sub-expression next to it when tracing.
  • Give doubles with the decimal point in the output (3.0, not 3).
  • Mention the exception name (NumberFormatException) when parsing text.
  • State the range of each type exactly if the question asks for it.
  • Underline where the cast brackets end; the bracket decides the order.

Practice questions on Primitive Types, Type Casting and Wrapper Classes

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

    The range of the primitive type byte is
    (a) 0 to 255 (b) -128 to 127 (c) -256 to 255 (d) -32768 to 32767

    Hint

    It uses 8 bits with a sign.

    Answer

    (b) -128 to 127

  2. Q2 · 2 marks · Short answerBoard level

    What are autoboxing and unboxing? Name the wrapper classes of int and boolean.

    Hint

    Automatic primitive to object and back.

    Answer

    Autoboxing is the automatic conversion of a primitive value into an object of its wrapper class (int to Integer); unboxing is the reverse. The wrappers are Integer for int and Boolean for boolean.

  3. Q3 · 3 marks · Short answerHarder board level

    Write the output:

    int x = 5;
    x += 3.7;
    char w = 'a';
    char z = (char) (w + 1);
    System.out.println(x + " " + z + " " + (w + 1));
    Hint

    Compound assignment casts back to int.

    Answer

    x += 3.7 means x = (int) (5 + 3.7) = (int) 8.7 = 8. z is the character after a, which is b. (w + 1) is an int: 97 + 1 = 98. Output: 8 b 98

  4. Q4 · 3 marks · Short answerHarder board level

    Write the output:

    short s = 32767;
    s++;
    byte b = 127;
    b++;
    int p = 'x' - 'a';
    System.out.println(s + " " + b + " " + p);
    Hint

    What happens when a value goes past the top of its range?

    Answer

    The largest short is 32767, so s++ wraps to -32768. The largest byte is 127, so b++ wraps to -128. In p, the characters are used as numbers: x is 120 and a is 97, so p = 23. Output: -32768 -128 23

  5. Q5 · 4 marks · Case-basedHOTS / competency

    A student writes: byte b1 = 10, b2 = 20; byte b3 = b1 + b2; and float f = 3.14; and double avg = (7 + 8) / 2;. (i) Explain why the first two do not compile and correct them. (ii) State the value of avg.

    Hint

    What type does byte + byte give? What type is 3.14?

    Answer

    (i) b1 + b2 is promoted to int, which cannot be stored in a byte without a cast: byte b3 = (byte) (b1 + b2);. The literal 3.14 is a double, so use float f = 3.14f; or float f = (float) 3.14;. (ii) (7 + 8) / 2 is integer division, 7, stored as 7.0.

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.How many primitive types does Java have?Show answer

    Eight.

  2. 2.What is (int) 7.9?Show answer

    7.

  3. 3.What is the default value of a boolean field?Show answer

    false.

  4. 4.What does parseInt do?Show answer

    Converts a String of digits to an int.

  5. 5.What is the value of 5 / 2 in Java?Show answer

    2 (integer division).

  6. 6.What is Integer.MAX_VALUE + 1?Show answer

    -2147483648, the overflow wraps round.

Primitive Types, Type Casting and Wrapper Classes: frequently asked questions

What are the primitive data types in Java?

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

What is the difference between implicit and explicit type conversion?

Implicit conversion (coercion) is done automatically from a smaller to a larger type; explicit conversion is a cast written by the programmer, needed when narrowing.

Why do we need wrapper classes?

They let primitive values be treated as objects, for example stored where an object is needed, and they provide methods such as parseInt and isDigit.

Is (char) 66 the same as the String "B"?

No. (char) 66 is the char value B; "B" is a String object.