Skip to content
Computer ScienceISC Class 11 · Chapter 6

Primitive Values, Wrapper Classes, Types and Casting: ISC 11

Updated 15 October 2026 · Fact-checked

Java has eight primitive types (byte, short, int, long, float, double, char and boolean), each with a fixed size and range, and a wrapper class for each that turns the value into an object. ISC Class 11 Computer Science also covers classes as user-defined types, automatic type coercion in mixed expressions, and explicit casting.

What Primitive Values, Wrapper Classes, Types and Casting is about

Every value in a Java program has a type, which decides how much memory it takes, what range of values it can hold and what operations are allowed. Getting types right avoids subtle bugs: 7 / 2 is 3, not 3.5, and an int that grows past its limit silently becomes negative.

The CISCE 2028 syllabus (Section B, topic 6) asks for: primitive values and types: byte, int, short, long, float, double, boolean, char; the corresponding wrapper classes; class as type of the object; class as mechanism for user-defined types; and changing types through user-defined casting and automatic type coercion for some primitive types (for example, mixed-type expressions), noting that casting in general should be avoided if possible.

Four programs on this page (compiled and run) print the real sizes and ranges, demonstrate widening and narrowing, use wrapper classes, and build a Fraction type of our own.

Weightage: Primitive Values, Wrapper Classes, Types and Casting is topic 6 of the ISC Class XI Computer Science syllabus, in Section B - Java. CISCE weights Section B at 30 of the 70 marks in Paper I (Theory). Class XI is examined by the school, so your school sets the paper within this weightage.Source: CISCE ISC Computer Science syllabus, examination year 2028, Class XI unit weightage

Key concepts

The eight primitive types

Java has eight primitive (built-in) types. They hold simple values directly (not references) and have fixed sizes on every platform.

TypeSizeRange or valuesDefault (as a field)Example literal
byte8 bits−128 to 1270(byte) 10
short16 bits−32,768 to 32,7670(short) 500
int32 bits−2,147,483,648 to 2,147,483,647042
long64 bitsabout ±9.22 × 10¹⁸0L5000000000L
float32 bitsabout ±3.4 × 10³⁸, about 7 significant digits0.0f3.14f
double64 bitsabout ±1.8 × 10³⁰⁸, about 15 significant digits0.03.14
char16 bits0 to 65,535 (Unicode)'\u0000''A'
boolean(1 bit of information)true, falsefalsetrue

Integer types: byte, short, int, long. Floating-point types: float, double. Character type: char. Logical type: boolean.

Literals: whole numbers are int by default (add L for long); decimals are double by default (add f for float).

The program below reads the sizes and ranges from the wrapper classes, and shows overflow: adding 1 to the largest int wraps round to the smallest.

// Sizes and ranges of the primitive types, read from their wrapper classes.
public class Ranges
{
    public static void main(String[] args)
    {
        System.out.println("byte   " + Byte.SIZE + " bits: " + Byte.MIN_VALUE + " to " + Byte.MAX_VALUE);
        System.out.println("short  " + Short.SIZE + " bits: " + Short.MIN_VALUE + " to " + Short.MAX_VALUE);
        System.out.println("int    " + Integer.SIZE + " bits: " + Integer.MIN_VALUE + " to " + Integer.MAX_VALUE);
        System.out.println("long   " + Long.SIZE + " bits: " + Long.MIN_VALUE + " to " + Long.MAX_VALUE);
        System.out.println("float  " + Float.SIZE + " bits, largest " + Float.MAX_VALUE);
        System.out.println("double " + Double.SIZE + " bits, largest " + Double.MAX_VALUE);
        System.out.println("char   " + Character.SIZE + " bits: " + (int) Character.MIN_VALUE + " to " + (int) Character.MAX_VALUE);
        int big = Integer.MAX_VALUE;
        System.out.println("MAX_VALUE + 1 = " + (big + 1));       // overflow wraps round
    }
}

Output:

byte   8 bits: -128 to 127
short  16 bits: -32768 to 32767
int    32 bits: -2147483648 to 2147483647
long   64 bits: -9223372036854775808 to 9223372036854775807
float  32 bits, largest 3.4028235E38
double 64 bits, largest 1.7976931348623157E308
char   16 bits: 0 to 65535
MAX_VALUE + 1 = -2147483648

Worked example

Which type would you choose for (i) a person's age, (ii) the population of the world, (iii) the price of a book, (iv) a grade letter? [2 marks]

Try it first, then show the solutionSolution
  1. (i) Small whole number: byte or int (int is usual). (ii) About 8 billion exceeds int's range: long.
  2. (iii) Has a fractional part: double. (iv) Single character: char.

Answer: (i) int (or byte); (ii) long; (iii) double; (iv) char.

Wrapper classes

Each primitive type has a wrapper class in java.lang that wraps the value in an object:

PrimitiveWrapper
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean

Why wrappers are needed: (1) some library structures store only objects; (2) they provide useful methods and constants: Integer.parseInt("25"), Double.parseDouble("3.5"), Integer.toString(5), Integer.toBinaryString(10), Character.isDigit(c), Character.toUpperCase(c), Integer.MAX_VALUE.

Autoboxing: automatic conversion from a primitive to its wrapper (Integer x = 45;). Unboxing: automatic conversion back (int y = x + 5;).

Comparing wrapper objects: use equals(), not ==, because == compares references.

// Wrapper classes: autoboxing, unboxing, parsing strings, and useful constants and methods.
public class Wrappers
{
    public static void main(String[] args)
    {
        Integer boxed = 45;                       // autoboxing: int -> Integer
        int plain = boxed + 5;                    // unboxing: Integer -> int
        System.out.println("plain = " + plain);

        int n = Integer.parseInt("2028");         // String -> int
        double x = Double.parseDouble("3.75");    // String -> double
        System.out.println("n + 1 = " + (n + 1) + ", x * 2 = " + (x * 2));

        String s = Integer.toString(255);         // int -> String
        System.out.println("binary of 255 = " + Integer.toBinaryString(255) + ", hex = " + Integer.toHexString(255));
        System.out.println("length of \"" + s + "\" = " + s.length());

        System.out.println(Character.isDigit('7') + " " + Character.isLetter('7') + " " + Character.toUpperCase('q'));
        Boolean flag = Boolean.parseBoolean("TRUE");
        System.out.println("flag = " + flag);

        Integer a = 1000, b = 1000;
        System.out.println("a.equals(b) = " + a.equals(b));   // compare wrapper values with equals
    }
}

Output:

plain = 50
n + 1 = 2029, x * 2 = 7.5
binary of 255 = 11111111, hex = ff
length of "255" = 3
true false Q
flag = true
a.equals(b) = true

Worked example

What does System.out.println(Integer.parseInt("125") + Integer.parseInt("75")); print, and how does it differ from System.out.println("125" + "75");? [2 marks]

Try it first, then show the solutionSolution
  1. parseInt converts both strings to ints: 125 + 75 = 200.
  2. Without parsing, + joins strings: "12575".

Answer: 200 and 12575 (numeric addition after parsing vs string concatenation).

Class as type; class as a user-defined type

In Java, every class is also a type. Declaring MyDate d; means d is a variable of type MyDate: it can refer to MyDate objects only. Variables of class types are reference variables: they hold a reference (address) to an object, not the object itself; their default value as a field is null.

Primitive vs reference types:

BasisPrimitive typeClass (reference) type
HoldsThe value itselfA reference to an object
Defined byJava languageLibrary or programmer
MethodsNoneHas methods
Default0, 0.0, false, '\u0000'null
Exampleint n = 5;String s = "Java";

Class as a mechanism for user-defined types: when Java has no type for what we need (a fraction, a complex number, a date), we define a class; its objects then behave like values of a new type, with operations we write as methods.

// A class as a user-defined type: Fraction values can be created, added and printed
// just as int values can, but the rules are written by us.
public class Fraction
{
    private int num, den;

    public Fraction(int n, int d)
    {
        int g = gcd(Math.abs(n), Math.abs(d));
        num = n / g;                      // store in lowest terms
        den = d / g;
    }

    private static int gcd(int a, int b)
    {
        while (b != 0)
        {
            int r = a % b;
            a = b;
            b = r;
        }
        return a;
    }

    public Fraction add(Fraction other)    // returns a new Fraction: a value of our own type
    {
        return new Fraction(num * other.den + other.num * den, den * other.den);
    }

    public double toDouble()
    {
        return (double) num / den;        // cast so that the division is not integer division
    }

    public String toString()
    {
        return num + "/" + den;
    }

    public static void main(String[] args)
    {
        Fraction a = new Fraction(1, 4);
        Fraction b = new Fraction(2, 6);
        Fraction c = a.add(b);
        System.out.println(a + " + " + b + " = " + c + " = " + c.toDouble());
    }
}

Output:

1/4 + 1/3 = 7/12 = 0.5833333333333334

The Fraction type stores values in lowest terms, can add itself to another Fraction and convert itself to a double.

Worked example

Using the Fraction class, what does new Fraction(3, 4).add(new Fraction(5, 6)) give? [1 mark]

Try it first, then show the solutionSolution
  1. 3/4 + 5/6 = (18 + 20)/24 = 38/24 = 19/12 after dividing by the gcd 2.

Answer: 19/12 (the compiled check printed 19/12).

Automatic type coercion (widening) in mixed expressions

Type coercion (implicit conversion, widening) happens automatically when a value of a smaller type is used where a larger type is expected, because no information is lost:

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

Rules for mixed expressions (numeric promotion):

1. byte, short and char operands are first promoted to int.
2. If either operand is double, the other becomes double; else if either is float, float; else if either is long, long; otherwise int.
3. The result has the promoted type.

Consequences:

  • 7 / 2 = 3 (int ÷ int is integer division); 7 / 2.0 = 3.5.
  • short + short gives an int (so short s = s1 + s2; does not compile without a cast).
  • 'A' + 1 = 66 (char promoted to int).
  • Assigning int to double: double d = 7; gives 7.0.

Order of evaluation matters: in 10 / 4 * 4.0, 10 / 4 is computed first as int (2), then 2 * 4.0 = 8.0.

Worked example

Give the value and type of: (i) 5 / 2 + 5 % 2; (ii) 'a' + 'b'; (iii) 10 / 4 * 4.0. [2 marks]

Try it first, then show the solutionSolution
  1. (i) 2 + 1 = 3 (int). (ii) 97 + 98 = 195 (int).
  2. (iii) 10 / 4 = 2 (int), 2 × 4.0 = 8.0 (double).

Answer: (i) 3, int; (ii) 195, int; (iii) 8.0, double.

Explicit casting (narrowing) and why to avoid it

Casting is an explicit conversion written by the programmer: (type) expression. It is needed for narrowing (larger → smaller type), which may lose information:

  • (int) 99.99 = 99: the fraction is truncated, not rounded (use Math.round for rounding).
  • (byte) 300 = 44: the value wraps round modulo 256 (300 − 256).
  • (char) 66 = 'B'.
  • (double) 7 / 2 = 3.5, but (double) (7 / 2) = 3.0: the cast applies to what follows it.
// Automatic type coercion (widening) and explicit casting (narrowing) in mixed expressions.
public class Casting
{
    public static void main(String[] args)
    {
        int i = 7;
        double d = i;                 // widening: int to double, automatic
        System.out.println("d = " + d);
        System.out.println("7 / 2 = " + (7 / 2) + ", 7 / 2.0 = " + (7 / 2.0));
        System.out.println("(double) 7 / 2 = " + ((double) 7 / 2) + ", (double) (7 / 2) = " + ((double) (7 / 2)));
        double price = 99.99;
        int rupees = (int) price;     // narrowing: fraction is cut off, not rounded
        System.out.println("(int) 99.99 = " + rupees);
        int big = 300;
        byte b = (byte) big;          // 300 does not fit in a byte: 300 - 256 = 44
        System.out.println("(byte) 300 = " + b);
        char c = 'A';
        int code = c + 1;             // char promoted to int
        System.out.println("'A' + 1 = " + code + ", (char) ('A' + 1) = " + (char) (c + 1));
        long l = 5000000000L;
        float f = l;                  // long to float is allowed (may lose precision)
        System.out.println("float from long = " + f);
        short s1 = 10, s2 = 20;
        int sum = s1 + s2;            // short + short gives int
        System.out.println("short + short = " + sum);
        System.out.println("Math.round(2.5) = " + Math.round(2.5) + ", (int) 2.99 = " + (int) 2.99);
    }
}

Output:

d = 7.0
7 / 2 = 3, 7 / 2.0 = 3.5
(double) 7 / 2 = 3.5, (double) (7 / 2) = 3.0
(int) 99.99 = 99
(byte) 300 = 44
'A' + 1 = 66, (char) ('A' + 1) = B
float from long = 5.0E9
short + short = 30
Math.round(2.5) = 3, (int) 2.99 = 2

Why casting should be avoided where possible (as the syllabus advises): narrowing can silently lose data (fractions, high-order bits); it hides type errors the compiler would otherwise catch; and it makes code harder to read. Prefer choosing the right type from the start (use double for money calculations, long for large counts) and use library methods (Math.round, Integer.parseInt) instead of casts.

Compound assignment (+=, -=, ...) includes an implicit cast: short s = 5; s += 1; compiles, but s = s + 1; does not.

Worked example

Evaluate (byte) 200 and explain. [1 mark]

Try it first, then show the solutionSolution
  1. 200 exceeds byte's maximum of 127; the bit pattern 11001000 read as a signed byte is 200 − 256 = −56.

Answer: −56.

Primitive Values, Wrapper Classes, Types and Casting in real life

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

Overflow in counts

Suppose a video platform stores view counts in an int, and a popular video passes 2,147,483,647 views.

What would the counter show after one more view, and how should it be fixed?

Try it first, then show the solutionSolution
  1. int's maximum is 2,147,483,647; adding 1 wraps round to −2,147,483,648.
  2. Use long, whose maximum is about 9.2 × 10¹⁸.

Answer: A large negative number; use long for counts that can grow very large.

Memory hook: Choose a box big enough for the biggest value.

Integer division in averages

Imagine a program computes the average of marks 78, 85 and 92 as (78 + 85 + 92) / 3 and prints 85, but your calculator says 85.0.

What goes wrong when the same code averages the marks 70, 71 and 73?

Try it first, then show the solutionSolution
  1. 70 + 71 + 73 = 214; 214 / 3 with ints gives 71, but the true average is 71.33.
  2. Divide by 3.0 (or cast the sum to double) to keep the fraction.

Answer: Integer division drops the fraction; use 3.0 to get 71.33.

Memory hook: int ÷ int throws the fraction away.

Reading numbers typed as text

Suppose an app reads '250' from a text box and adds a delivery charge of 40.

Why might it show 25040, and how is it fixed?

Try it first, then show the solutionSolution
  1. Text from a box is a String; "250" + 40 joins them as text.
  2. Convert first: Integer.parseInt("250") + 40 = 290.

Answer: String concatenation; parse the text with the wrapper method Integer.parseInt.

Memory hook: Parse before you add.

Topics in this chapter

Primitive Values, Wrapper Classes, Types and Casting: syntax and key facts

Widening order
  1. 1.byte
  2. 2.short
  3. 3.int
  4. 4.long
  5. 5.float
  6. 6.double; char
  7. 7.int
Automatic, no data lost in range.
Casting
(targetType) expression
Narrowing truncates fractions and may wrap round.
Byte wrap-round
If v > 127: (byte) v = v − 256 (repeat until in range)
Parsing
Integer.parseInt(s), Double.parseDouble(s), Boolean.parseBoolean(s)
Range of n-bit signed integer
−2ⁿ⁻¹ to 2ⁿ⁻¹ − 1

Primitive Values, Wrapper Classes, Types and Casting: diagrams to practise

Practise each one on paper: draw it from the checklist, then tick off every label.

Widening ladder

Explain implicit type conversion.

Draw

  1. A staircase rising left to right: byte (8) → short (16) → int (32) → long (64) → float (32) → double (64).
  2. A side step from char (16) to int.
  3. Arrow up labelled 'automatic (widening)'; arrow down labelled 'cast needed (narrowing), may lose data'.

Label

  • widening
  • narrowing
  • sizes in bits

Watch out: Note that long → float is allowed though float has fewer bits: its range is larger.

Primitive vs reference in memory

Distinguish primitive and reference variables.

Draw

  1. Box n containing 5 (primitive: value inside).
  2. Box d containing an arrow to a separate object box 'MyDate: day 28, month 2, year 2028' (reference).
  3. Box s with 'null' (reference to no object).

Label

  • value
  • reference
  • object
  • null

Watch out: Arrows make the idea of a reference clear.

How the school exam asks Primitive Values, Wrapper Classes, Types and Casting

The question patterns to expect, and the method that works for each.

Question typeHow it is askedMethod
Theory (1-2 marks)Size and range of a type; name the wrapper class; define autoboxing; primitive vs reference type.Exact sizes in bits and ranges; one-line definitions with examples.
Output prediction (1-5 marks)Evaluate mixed expressions and casts: 7 / 2, (int) 3.99, (char) ('A' + 2), (byte) 200, 'a' + 'b'.Apply promotion rules step by step; track int vs double carefully.
Programming (5-10 marks)Write a class representing a user-defined type (fraction, complex number, money) with methods.Private fields, constructor, operations returning new objects, toString.

Tricks to remember Primitive Values, Wrapper Classes, Types and Casting

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

  • Sizes: 8-16-32-64

    byte 8, short 16, int 32, long 64; float 32, double 64; char 16.

    Use it when: Sizes of primitives.

  • Two odd wrapper names

    Only int → Integer and char → Character are not just capitalised.

    Use it when: Wrapper classes.

  • Up is free, down needs a cast

    Widening is automatic; narrowing needs (type).

    Use it when: Conversions.

  • Cast cuts, round rounds

    (int) 2.99 = 2; Math.round(2.5) = 3.

    Use it when: Truncation vs rounding.

Primitive Values, Wrapper Classes, Types and Casting: exam-level solved questions

Ordered from exam level to HOTS. Cover the solution, write your own, then compare step by step.

Example 1: Size of char · 1 mark · MCQExam level

The size of the char data type in Java is:
(a) 8 bits
(b) 16 bits
(c) 32 bits
(d) 64 bits

Try it first, then show the solutionSolution
  1. Java chars are 16-bit Unicode values.

Answer: (b) 16 bits.

Example 2: Truncation · 1 mark · Assertion-reasonHarder exam level

Assertion: (int) 7.9 evaluates to 7.
Reason: Casting a double to an int rounds it to the nearest whole number.
(a) Both Assertion and Reason are true and Reason is the correct explanation of Assertion.
(b) Both Assertion and Reason are true but Reason is not the correct explanation of Assertion.
(c) Assertion is true but Reason is false.
(d) Both Assertion and Reason are false.

Try it first, then show the solutionSolution
  1. The cast truncates (drops the fraction): 7 (Assertion true). It does not round (Reason false).

Answer: (c)

Example 3: Evaluate expressions · 2 marks · Short answerExam level

Give the output of: System.out.println((double) 7 / 2); System.out.println((double) (7 / 2)); System.out.println((char) ('A' + 1));

Try it first, then show the solutionSolution
  1. (double) 7 / 2 → 7.0 / 2 = 3.5; (double) (7 / 2) → (double) 3 = 3.0.
  2. 'A' + 1 = 66 → (char) 66 = 'B'.

Answer: 3.5, 3.0, B (as printed by the compiled Casting program).

Example 4: Primitive vs reference · 5 marks · Short answerHarder exam level

Distinguish between primitive types and reference (class) types on four bases, and explain why wrapper classes are needed.

Try it first, then show the solutionSolution
  1. What is stored: the value itself vs a reference to an object.
  2. Defined by: the language (8 fixed types) vs the library or the programmer (any class).
  3. Methods: none vs methods available (s.length()).
  4. Default value as a field: 0, 0.0, false or '\u0000' vs null.
  5. Wrappers let primitive values be treated as objects (for structures that hold only objects) and provide methods like parseInt and constants like MAX_VALUE; autoboxing and unboxing convert automatically.

Answer: Primitives hold values, are built-in, have no methods and default to zero-like values; reference types hold references, are classes with methods and default to null. Wrappers bridge the two and add useful methods.

Example 5: Hidden overflow and truncation · 5 marks · Short answerHOTS / competency

Predict the output and explain each line: long big = 3000000000L; int bad = (int) big; System.out.println(bad); double avg = (int) (78.6 + 0.5); System.out.println(avg); char g = 'G'; g++; System.out.println(g);

Try it first, then show the solutionSolution
  1. 3,000,000,000 exceeds int's maximum (2,147,483,647); the cast keeps the low 32 bits: 3,000,000,000 − 4,294,967,296 = −1,294,967,296.
  2. 78.6 + 0.5 = 79.1; (int) gives 79; stored in a double: 79.0 (a manual way of rounding).
  3. g++ on a char adds 1 to its code: 'G' (71) → 'H' (72).
  4. Compiled check output: −1294967296, 79.0, H.

Answer: −1294967296, 79.0, H.

Example 6: A Money type · 10 marks · Long answerHOTS / competency

Design a class Money as a user-defined type that stores an amount in rupees and paise as a single long field paise (to avoid floating-point errors). Provide a constructor Money(long rupees, int paise), a method Money add(Money m), Money multiply(int qty), and toString() giving 'Rs 125.50'. Write main() to compute the cost of 3 notebooks at Rs 45.75 plus a pen at Rs 12.50.

Try it first, then show the solutionSolution
  1. Field: private long paise; constructor sets paise = rupees × 100 + p.
  2. add returns a new Money built from the total paise (this.paise + m.paise) through a private helper ofPaise(long total).
  3. multiply returns a Money with paise × qty.
  4. toString: rupees = paise / 100, rem = paise % 100; format with two digits for paise.
  5. Using long avoids double rounding errors such as 0.1 + 0.2 = 0.30000000000000004.
  6. 3 × 45.75 = 137.25; + 12.50 = 149.75: the compiled program prints 'Total = Rs 149.75'.

Answer: See the compiled Money program:

// Money as a user-defined type: the amount is kept in whole paise in a long,
// so additions are exact (no floating-point rounding errors).
public class Money
{
    private long paise;

    public Money(long rupees, int p)
    {
        paise = rupees * 100 + p;
    }

    private static Money ofPaise(long total)
    {
        return new Money(total / 100, (int) (total % 100));
    }

    public Money add(Money m)
    {
        return ofPaise(paise + m.paise);
    }

    public Money multiply(int qty)
    {
        return ofPaise(paise * qty);
    }

    public String toString()
    {
        long r = paise / 100;
        long p = paise % 100;
        return "Rs " + r + "." + (p < 10 ? "0" : "") + p;
    }

    public static void main(String[] args)
    {
        Money notebook = new Money(45, 75);
        Money pen = new Money(12, 50);
        Money total = notebook.multiply(3).add(pen);
        System.out.println("3 notebooks = " + notebook.multiply(3));
        System.out.println("Total = " + total);
    }
}
Output:
3 notebooks = Rs 137.25
Total = Rs 149.75

Case-study practice (Section D style)

Read the situation, then answer every part. In a school exam set on the board pattern this is a 5-mark question split into 2 + 2 + 1 marks.

Sensor readings

Suppose a weather station's Java program reads temperature as text ('23.6'), counts total rainfall in millimetres over many years (it can exceed 3 billion), and stores a status code from 0 to 100. A junior programmer uses int for everything and casts freely.

  1. (i) · 2 marks

    Choose the best type for each value and give a reason.

    Answer

    Answer: Temperature: double (has a fraction). Total rainfall: long (can exceed int's maximum of about 2.1 billion). Status code: byte or int (small whole number).

    2 marks for all three with reasons.

  2. (ii) · 2 marks

    How should the text '23.6' be converted, and what would (int) of it give?

    Answer

    Answer: Double.parseDouble("23.6") gives 23.6; casting that to int gives 23, losing the fraction.

    1 mark each.

  3. (iii) · 1 mark

    Why is casting freely a bad practice here?

    Answer

    Answer: Narrowing casts silently lose data (fractions truncated, large values wrapped round to wrong numbers), hiding errors the compiler would otherwise report.

    1 mark.

Where the marks are

How a typical answer is split into step marks. Show every step: a right final answer alone rarely earns full marks.

Evaluate three cast expressions (2 marks) (2 marks)

StepMarks
Both double casts correct (3.5 and 3.0)1
char cast correct (B)1

Primitive vs reference types and wrappers (5 marks) (5 marks)

StepMarks
Four bases of difference4
Need for wrapper classes1

Money class (10 marks) (10 marks)

StepMarks
Field and constructor2
add and multiply returning new objects3
toString with two-digit paise2
main computing the bill2
Reason for avoiding double1

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

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

  • Mistake: Expecting 7 / 2 to be 3.5

    Both operands are int, so the division is integer division.

    Do this: Make one operand double: 7 / 2.0 or (double) 7 / 2.

    Usually costs: 1 mark.

  • Mistake: Writing (double) (a / b) to get a decimal result

    The integer division happens first; the cast only converts the truncated result.

    Do this: Cast an operand: (double) a / b.

    Usually costs: 1 mark.

  • Mistake: Wrong wrapper names (Int, Char)

    They are Integer and Character.

    Do this: Only those two are not simply capitalised.

    Usually costs: ½-1 mark.

  • Mistake: Comparing Integer objects with ==

    == compares references for objects; values may be equal while references differ.

    Do this: Use equals() or unbox to int.

    Usually costs: 1 mark.

Practice questions on Primitive Values, Wrapper Classes, Types and Casting

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 · MCQExam level

    Which is the wrapper class of char?
    (a) Char
    (b) Character
    (c) String
    (d) Chr

    Hint

    Full word.

    Answer

    (b) Character.

  2. Q2 · 1 mark · Very short answerExam level

    State the size in bits and range of the byte type.

    Hint

    8 bits signed.

    Answer

    8 bits; −128 to 127.

  3. Q3 · 2 marks · Short answerExam level

    Define autoboxing and unboxing with an example.

    Hint

    int ↔ Integer.

    Answer

    Autoboxing: automatic conversion of a primitive to its wrapper (Integer x = 45;). Unboxing: automatic conversion of a wrapper to its primitive (int y = x + 5; gives 50).

  4. Q4 · 2 marks · Short answerHarder exam level

    Give the output: System.out.println((byte) 200); System.out.println((int) 'z'); System.out.println(10 / 4 * 4.0);

    Hint

    Wrap, code, order.

    Answer

    −56, 122, 8.0 (as the compiled check printed).

  5. Q5 · 2 marks · Short answerHarder exam level

    Why does short s = 5; s = s + 1; fail to compile while s += 1; compiles?

    Hint

    Promotion to int.

    Answer

    In s + 1, s is promoted to int, so the result is int, which cannot be assigned to short without a cast. The compound operator += includes an implicit cast back to short, so s += 1 compiles (s becomes 6).

  6. Q6 · 1 mark · Very short answerHarder exam level

    Give the value and type of 'a' + 'b' and of (char) ('a' + 2).

    Hint

    Promotion to int.

    Answer

    195 (int); 'c' (char).

  7. Q7 · 5 marks · Short answerHarder exam level

    Explain with examples the difference between implicit type coercion and explicit casting, and give two dangers of casting.

    Hint

    Widening vs narrowing.

    Answer

    Implicit coercion (widening) is automatic when a smaller type is used where a larger one is expected: double d = 7; gives 7.0; in 7 / 2.0 the int 7 becomes 7.0. Explicit casting is written by the programmer, usually for narrowing: (int) 99.99 gives 99. Dangers: loss of the fractional part (truncation), and wrap-round of values outside the target range ((byte) 300 = 44), both of which happen silently.

  8. Q8 · 2 marks · Short answerHOTS / competency

    Why does System.out.println(0.1 + 0.2); not print 0.3, and what does it print?

    Hint

    Binary fractions.

    Answer

    0.1 and 0.2 cannot be stored exactly in binary floating point, so a tiny error appears: it prints 0.30000000000000004. For money, use whole paise in a long (or BigDecimal).

  9. Q9 · 10 marks · Long answerHOTS / competency

    Write a class Complex as a user-defined type with real and imaginary parts (double), a method Complex add(Complex c), Complex multiply(Complex c) and toString(). Give the result of (2 + 3i)(1 − 4i).

    Hint

    (a + bi)(c + di) = (ac − bd) + (ad + bc)i.

    Answer

    Fields double re, im; constructor; add returns new Complex(re + c.re, im + c.im); multiply returns new Complex(re × c.re − im × c.im, re × c.im + im × c.re); toString prints re + " + " + im + "i" (handling the sign). (2 + 3i)(1 − 4i) = (2 + 12) + (−8 + 3)i = 14 − 5i; (2 + 3i) + (1 − 4i) = 3 − i.

  10. Q10 · 2 marks · Short answerHOTS / competency

    What is printed by: Integer a = 1000, b = 1000; System.out.println(a.equals(b)); and why should equals be used?

    Hint

    Values vs references.

    Answer

    true. equals compares the wrapped values; == would compare references, and two Integer objects holding 1000 are usually different objects, so == may give false.

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.Eight primitive types?Show answer

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

  2. 2.Size of int?Show answer

    32 bits.

  3. 3.Wrapper of int?Show answer

    Integer.

  4. 4.(int) 9.99?Show answer

    9 (truncated).

  5. 5.7 / 2 in Java?Show answer

    3 (integer division).

  6. 6.Default value of an object reference field?Show answer

    null.

Best order to study it

  1. The eight primitive types: sizes, ranges, literals.
  2. Overflow.
  3. Wrapper classes; autoboxing and unboxing; parse methods.
  4. Class as a type; reference variables; user-defined types.
  5. Automatic coercion and promotion rules.
  6. Explicit casting and why to avoid it.

Quick revision checklist

The night before the exam: can you do each of these without looking?

  • byte 8, short 16, int 32, long 64, float 32, double 64, char 16, boolean.
  • Wrappers: Byte, Short, Integer, Long, Float, Double, Character, Boolean.
  • Autoboxing int → Integer; unboxing Integer → int; parseInt, parseDouble.
  • Class = type; class variables hold references (default null).
  • Widening automatic: byte → short → int → long → float → double; char → int.
  • Casting narrows: truncates fractions, wraps round; avoid when possible.

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

Is String a primitive type?

No. String is a class (a reference type) in java.lang, although Java gives it special support such as string literals and the + operator for joining.

Why is long → float allowed automatically when float has fewer bits?

Because float's range (about 3.4 × 10³⁸) is far larger than long's, so the value always fits, though some precision (low-order digits) may be lost.

When should I use float instead of double?

Rarely in school programs: double is the default for decimal literals and is more precise. float needs an f suffix (3.5f).

Based on: CISCE ISC 2028 Computer Science syllabus (Classes XI and XII); CISCE Regulations for the ISC Examination Year 2028 (Class XI internal examination)