Skip to content
Computer ScienceISC Class 12 · Chapter 6

Primitive Values, Wrapper Classes, Types and Casting: ISC Class 12 Computer Science

Updated 15 October 2026 · Fact-checked

Java has eight primitive types: byte, short, int, long, float, double, char and boolean. Each has a wrapper class (Byte, Short, Integer, Long, Float, Double, Character, Boolean) that lets the value act as an object. Values change type by automatic widening coercion or by explicit casting, which can lose data.

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

The syllabus asks for:
1. Primitive values and types: byte, int, short, long, float, double, boolean, char.
2. The corresponding wrapper class for each primitive type.
3. Class as the type of an object; class as the mechanism for user-defined types.
4. Changing types through user-defined casting and automatic type coercion for some primitive types.

In the paper this appears as output questions (what does (int) 'a' + 1 print?), one-mark items on wrapper methods and ranges, and inside Section B programs that convert characters to codes and back (ciphers, letter shifting, digit extraction from strings). Examiners listed type casting while obtaining ASCII codes of characters among the difficult and confused topics in 2024.

All programs on this page were compiled and run, and the outputs shown are exactly what they printed.

Weightage: CISCE publishes no chapter or unit weightage for ISC Computer Science. Primitive Values, Wrapper Classes, Types and Casting is part of Section B, 6 of the 2027 syllabus and can be asked anywhere in the 70-mark Paper 1 (Part I compulsory: ten 1-mark items and a 10-mark short-answer question; Part II: two of three 10-mark questions in each of Sections A and B, and two of three 5-mark questions in Section C).Source: CISCE ISC Computer Science (868) syllabus, examination year 2027; latest Paper 1 specimen (2025) and the 2025 board paper

Key concepts

The eight primitive types
TypeSizeRange or valuesDefault (as field)
byte8 bits-128 to 1270
short16 bits-32768 to 327670
int32 bitsabout -2.1 × 10^9 to 2.1 × 10^9 (2147483647)0
long64 bitsabout ±9.2 × 10^180L
float32 bitsabout 7 significant digits0.0f
double64 bitsabout 15 significant digits0.0
char16 bitsUnicode 0 to 65535'\u0000'
booleannot fixed by the languagetrue or falsefalse

Notes:
1. Integer literals are int by default; add L for long (5000000000L). Decimal literals are double by default; add f for float (2.5f).
2. char stores a Unicode code; 'A' is 65, 'a' is 97, '0' is 48. Arithmetic on chars uses their codes.
3. boolean cannot be converted to or from any number type.
4. Primitive variables store the value itself; object (reference) variables store a reference to an object.

Class as type: a class defines a new, user-defined type. Account a; declares a variable of type Account that can refer to an Account object. Classes combine data and methods, so they define richer types than the primitives.

Worked example

[2] Give the size and range of byte, and explain what is printed by System.out.println((byte) 300); (Apply)

Try it first, then show the solutionSolution
  1. byte: 8 bits, range -128 to 127 - 1 mark.
  2. 300 does not fit; only the low 8 bits are kept: 300 - 256 = 44, so 44 is printed (confirmed by the Casting program) - 1 mark.

Answer: byte is 8 bits with range -128 to 127; (byte) 300 keeps the lowest 8 bits and prints 44.

Wrapper classes, autoboxing and useful methods

Each primitive type has a wrapper class in java.lang:

PrimitiveWrapper
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean

Why wrappers: to treat values as objects (for example in collections such as ArrayList<Integer>), and to use utility methods and constants.

Autoboxing: automatic conversion from primitive to wrapper (Integer boxed = 40;). Unboxing: wrapper to primitive (int x = boxed + 2;). Available since Java 5.

Useful methods:

  • Integer.parseInt("472") → 472; Double.parseDouble("3.75") → 3.75 (NumberFormatException if the string is not a valid number).
  • Integer.toString(255) → "255"; String.valueOf(x); Integer.valueOf("25") → Integer object.
  • Integer.toBinaryString(10) → "1010" (also toOctalString, toHexString).
  • Integer.MAX_VALUE, Integer.MIN_VALUE, Byte.MIN_VALUE and similar constants.
  • Character.isLetter, isDigit, isLetterOrDigit, isUpperCase, isLowerCase, isWhitespace, toUpperCase, toLowerCase.
  • equals( ) and compareTo( ) to compare wrapper objects.
// Wrapper classes: parsing, conversion, autoboxing, Character tests.
public class Wrappers
{
    public static void main(String[] args)
    {
        int n = Integer.parseInt("472");
        double p = Double.parseDouble("3.75");
        System.out.println(n + 8 + " " + (p * 2));

        String s = Integer.toString(255);
        String bin = Integer.toBinaryString(10);
        System.out.println(s + "1 " + bin);

        Integer boxed = 40;            // autoboxing: int -> Integer
        int unboxed = boxed + 2;       // auto-unboxing: Integer -> int
        System.out.println(unboxed);

        char ch = 'g';
        System.out.println(Character.isLetter(ch) + " " + Character.isDigit('7') + " "
            + Character.isUpperCase(ch) + " " + Character.toUpperCase(ch));
        System.out.println(Character.isWhitespace(' ') + " " + Character.isLetterOrDigit('#'));

        System.out.println(Integer.MAX_VALUE + " " + Byte.MIN_VALUE);

        Integer a1 = 100, a2 = 100;
        System.out.println(a1.equals(a2) + " " + a1.compareTo(150));
    }
}

Output:

480 7.5
2551 1010
42
true true false G
true false
2147483647 -128
true -1

Note n + 8 + " " adds first (480) because both are numbers, while s + "1" concatenates ("2551").

Worked example

[2] What are autoboxing and unboxing? Give one example of each. (Understand)

Try it first, then show the solutionSolution
  1. Autoboxing: automatic conversion of a primitive to its wrapper object, e.g. Integer boxed = 40; - 1 mark.
  2. Unboxing: automatic conversion of a wrapper object to its primitive, e.g. int x = boxed + 2; - 1 mark.

Answer: Autoboxing converts a primitive value into its wrapper object automatically (Integer boxed = 40;), and unboxing converts the wrapper back to the primitive (int x = boxed + 2; gives 42).

Automatic type coercion (widening) and promotion

Implicit (automatic) conversion happens when a value of a smaller type is used where a larger type is expected, so no information is lost:
byte → short → int → long → float → double, and char → int.

Examples: double d = 65; (d = 65.0); int code = 'a'; (97); long l = 100;.

Type promotion in expressions:
1. byte, short and char operands are promoted to int before arithmetic.
2. If any operand is long, float or double, the other is promoted to that type (the larger one).
3. So 'D' + 1 is an int (69), and s * f with a short and a float is a float.

A trap: the type of the result is decided by the operands, not by the variable on the left. In long big = Integer.MAX_VALUE + 1; the addition is done in int, overflows to -2147483648, and only then is widened. Cast first: (long) Integer.MAX_VALUE + 1 gives 2147483648.

Compound assignment operators (+=, -=, ...) include an automatic cast back to the variable's type: char ch = 'A'; ch += 2; is legal, but ch = ch + 2; is a compile-time error because ch + 2 is an int.

Worked example

[2] Explain the output of long big = Integer.MAX_VALUE + 1; System.out.println(big); (Analyse)

Try it first, then show the solutionSolution
  1. Both operands are int, so the addition is done in int arithmetic and overflows to -2147483648 - 1 mark.
  2. Only the overflowed result is widened to long, so -2147483648 is printed; casting one operand to long first gives 2147483648 - 1 mark.

Answer: It prints -2147483648: the sum overflows in int before being stored in the long. Writing (long) Integer.MAX_VALUE + 1 gives the correct 2147483648.

Explicit casting (narrowing) and char-int conversion

Explicit casting forces a value into a smaller or different type: (type) value. Information may be lost:

  • double → int truncates the fraction: (int) 9.99 = 9 (Math.round(9.99) = 10 rounds instead).
  • int → byte keeps only the low 8 bits: (byte) 300 = 44.
  • int → char gives the character with that code: (char) 65 = 'A'.

char and int:

  • char to int (code): (int) 'a' = 97, or just int c = 'a';.
  • int to char: (char) 66 = 'B'.
  • Next letter: (char) (ch + 1); ch + 1 alone is an int (prints 69 for 'D').
  • Digit character to value: '7' - '0' = 7.
  • Letter position: ch - 'A' (0 for 'A', 25 for 'Z').
// Implicit (widening) and explicit (narrowing) conversions, and char arithmetic.
public class Casting
{
    public static void main(String[] args)
    {
        int i = 65;
        double d = i;                 // widening: int -> double, automatic
        char c = (char) i;            // explicit: int -> char
        int code = 'a';               // char -> int, automatic
        System.out.println(d + " " + c + " " + code);

        double x = 9.99;
        int t = (int) x;              // narrowing: fraction is truncated, not rounded
        System.out.println(t + " " + Math.round(x));

        byte b = (byte) 300;          // 300 does not fit in a byte: 300 - 256 = 44
        System.out.println(b);

        char ch = 'D';
        char next = (char) (ch + 1);  // ch + 1 is an int; cast back to char
        System.out.println(next + " " + (ch + 1));

        short s = 10;
        float f = 2.5f;
        double r = s * f;             // short promoted to float, result stored in double
        System.out.println(r);

        long big = Integer.MAX_VALUE + 1;          // int overflow happens before widening
        long ok = (long) Integer.MAX_VALUE + 1;    // widen first, then add
        System.out.println(big + " " + ok);
    }
}

Output:

65.0 A 97
9 10
44
E 69
25.0
-2147483648 2147483648

User-defined casting with objects (casting a superclass reference to a subclass type) is covered in Inheritance.

Worked example

[2] Give the output: char ch = 'D'; System.out.println((char) (ch + 1) + " " + (ch + 1)); (Apply)

Try it first, then show the solutionSolution
  1. ch + 1 = 68 + 1 = 69 (int); cast to char gives 'E' - 1 mark.
  2. The second (ch + 1) is not cast, so it prints 69: output 'E 69' - 1 mark.

Answer: E 69

Casting in a complete program: a letter-shift cipher

Shifting a letter k places with wrap-round uses all the casting rules:
1. ch - 'A' converts the letter to a position 0 to 25 (int arithmetic).
2. Add k and take % 26 to wrap from Z back to A.
3. Add 'A' and cast to char to get the letter back.
Lower-case letters use 'a'; other characters are left unchanged.

import java.util.Scanner;

// Caesar cipher: shifts each letter k places forward, wrapping Z round to A.
// Uses char -> int and int -> char casting. Non-letters are unchanged.
public class Cipher
{
    String text;
    int k;
    String coded;

    Cipher()
    {
        text = "";
        k = 0;
        coded = "";
    }

    void accept()
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the text:");
        text = sc.nextLine();
        System.out.println("Enter the shift (1-25):");
        k = sc.nextInt();
    }

    char shift(char ch)
    {
        if (Character.isUpperCase(ch))
            return (char) ((ch - 'A' + k) % 26 + 'A');
        if (Character.isLowerCase(ch))
            return (char) ((ch - 'a' + k) % 26 + 'a');
        return ch;
    }

    void encode()
    {
        coded = "";
        for (int i = 0; i < text.length(); i++)
            coded = coded + shift(text.charAt(i));
    }

    void display()
    {
        encode();
        System.out.println("Original: " + text);
        System.out.println("Coded   : " + coded);
    }

    public static void main(String[] args)
    {
        Cipher ob = new Cipher();
        ob.accept();
        ob.display();
    }
}

Input: Zebra Crossing, 2027! with shift 3

Enter the text:
Enter the shift (1-25):
Original: Zebra Crossing, 2027!
Coded   : Cheud Furvvlqj, 2027!

Check: Z (25) + 3 = 28, 28 % 26 = 2 → C; e (4) + 3 = 7 → h; the comma, space, digits and ! are unchanged.

Worked example

[2] Using the cipher formula with k = 3, encode the letters y and Y. (Apply)

Try it first, then show the solutionSolution
  1. y: 'y' - 'a' = 24; (24 + 3) % 26 = 1; (char) (1 + 'a') = 'b' - 1 mark.
  2. Y: 24 → 1 → 'B' - 1 mark.

Answer: y → b and Y → B.

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.

Narrowing loses data

Imagine pouring a full 1-litre bottle into a 300 ml glass: only part of the water fits and the rest spills.

How is this like casting a double 9.99 to an int or 300 to a byte?

Try it first, then show the solutionSolution
  1. A smaller type cannot hold everything the larger type holds.
  2. (int) 9.99 drops the fraction (9); (byte) 300 keeps only what fits (44).

Answer: Narrowing casts, like pouring into a smaller glass, lose whatever does not fit, so they must be written explicitly and used with care.

Memory hook: Big into small spills.

Parsing input

Suppose a form on a website sends a person's age as the text "17", and the program must check whether the person is at least 18.

What must the program do first?

Try it first, then show the solutionSolution
  1. Text cannot be compared numerically with 18.
  2. Convert with Integer.parseInt("17"), catching NumberFormatException for bad input.

Answer: It must parse the text into an int using Integer.parseInt, then compare 17 >= 18 (false); invalid text throws NumberFormatException.

Memory hook: Text in, parse before maths.

Overflow

Imagine a car odometer with five digits rolling over from 99999 to 00000.

Which Java behaviour does this resemble?

Try it first, then show the solutionSolution
  1. A fixed-size type wraps when the value exceeds its range.
  2. Integer.MAX_VALUE + 1 wraps to Integer.MIN_VALUE.

Answer: Integer overflow: int arithmetic wraps from 2147483647 to -2147483648, so large results need long.

Memory hook: Fixed digits roll over.

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 loss.
Narrowing
(type) value
May truncate or wrap.
Character codes
'A' = 65, 'a' = 97, '0' = 48
'a' - 'A' = 32.
Digit and letter conversions
digit = ch - '0'; position = ch - 'A'; letter = (char) (pos + 'A')
Used in ciphers and parsers.
Cipher shift
(char) ((ch - 'A' + k) % 26 + 'A')
Wraps Z to A.
Parsing
Integer.parseInt(s); Double.parseDouble(s); Integer.toString(n)
NumberFormatException on bad text.

How the board 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
Output questionsOutputs of casts, char arithmetic, integer overflow, parse and toString calls.Track the type of each sub-expression; remember char + int is int.
One-mark theoryWrapper of char; size of a type; autoboxing; difference between implicit and explicit conversion.Precise names: Character and Integer, not Char and Int.
Programs [10]Ciphers, letter shifting, case conversion, digit extraction from strings, code sums.Use ch - 'A' and (char) casts; handle upper and lower case; leave other characters unchanged.
Assertion-reasonStatements about widening, narrowing or wrappers.Check direction of conversion and whether data can be lost.

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.

  • Byte Short Int Long Float Double

    'Big Snakes In Long Fat Dens': the widening chain.

    Use it when: Order of automatic conversion.

  • 65-97-48

    A is 65, a is 97, zero is 48.

    Use it when: Character codes.

  • Wrappers that change name

    Only two wrappers are not just capitalised: int → Integer, char → Character.

    Use it when: Wrapper names.

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

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

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 classes: Byte, Short, Integer, Long, Float, Double, Character, Boolean.
  2. char's wrapper is Character.

Answer: (b) Character.

Example 2: Truncation versus rounding · 1 mark · Very short answerBoard level

What do (int) 9.99 and Math.round(9.99) return?

Try it first, then show the solutionSolution
  1. A cast to int drops the fraction.
  2. Math.round rounds to the nearest whole number (returns long for a double argument).

Answer: 9 and 10.

Example 3: Assertion-reason: compound assignment · 1 mark · Assertion-reasonHarder board level

Assertion (A): char ch = 'A'; ch = ch + 2; compiles without error.
Reason (R): Arithmetic on char operands produces an int result.
(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 false but R is true.
(d) Both A and R are false.

Try it first, then show the solutionSolution
  1. ch + 2 is an int, and assigning an int to a char needs an explicit cast, so A is false (ch += 2 would compile).
  2. R is true.

Answer: (c) A is false but R is true.

Example 4: Sum of character codes · 2 marks · Short answerHarder board level

[2] Write a loop that prints the sum of the Unicode codes of the characters in the string "AB1", and give the result. (Apply)

Try it first, then show the solutionSolution
  1. int sum = 0; for (int i = 0; i < s.length(); i++) sum += s.charAt(i); (each char is promoted to int).
  2. 65 + 66 + 49 = 180.

Answer: sum = 180 ('A' 65 + 'B' 66 + '1' 49).

Example 5: Cipher class · 10 marks · Long answerHOTS / competency

[10] Design a class Cipher with data members text, k and coded; a constructor; void accept( ) to read the text and shift k; char shift(char ch) that shifts a letter k places with wrap-round, keeping its case and leaving other characters unchanged; void encode( ) that builds coded using shift( ); and void display( ) that invokes encode( ) and prints both strings. Write main( ). (Create)

Try it first, then show the solutionSolution
  1. Data members String text, coded; int k; constructor initialises them to "", 0, "".
  2. accept( ): nextLine( ) for text, nextInt( ) for k.
  3. shift( ): upper case → (char) ((ch - 'A' + k) % 26 + 'A'); lower case similarly with 'a'; else return ch.
  4. encode( ): loop over text, append shift(text.charAt(i)) to coded.
  5. display( ): call encode( ), print original and coded.
  6. main( ): create the object, call accept( ) and display( ). For 'Zebra Crossing, 2027!' and k = 3 the output is 'Cheud Furvvlqj, 2027!'.

Answer: See the compiled Cipher program above: 'Zebra Crossing, 2027!' with shift 3 becomes 'Cheud Furvvlqj, 2027!'.

Example 6: Decoding · 2 marks · Short answerHOTS / competency

[2] How would you modify shift( ) to decode a message encoded with shift k? Decode 'Cheud' with k = 3. (Evaluate)

Try it first, then show the solutionSolution
  1. Shift backwards: (ch - 'A' - k + 26) % 26 + 'A' (adding 26 keeps the value non-negative before %).
  2. C → Z, h → e, e → b, u → r, d → a: 'Zebra'.

Answer: Use (char) ((ch - 'A' - k + 26) % 26 + 'A') (and the lower-case version); 'Cheud' decodes to 'Zebra'.

Case-study practice (Section D style)

Read the situation, then answer every part. In the board paper this is a 6-mark question split into 1 + 2 + 1 + 1 + 1 marks.

Reading numbers from text (case-based)

A program receives the string "Room 42 has 3 windows" and must add the numbers in it.

  1. (i) · 1 mark

    Which method converts the token "42" into an int?

    Answer

    Answer: Integer.parseInt("42").

    1 mark.

  2. (ii) · 2 marks

    How can the program test whether a token is a number before parsing?

    Answer

    Answer: Check that every character satisfies Character.isDigit( ) (or catch NumberFormatException).

    1 for the method, 1 for applying it to each character.

  3. (iii) · 1 mark

    What is the sum of the numbers in the string?

    Answer

    Answer: 45.

    1 mark.

  4. (iv) · 1 mark

    What happens if Integer.parseInt("has") is called?

    Answer

    Answer: A NumberFormatException is thrown.

    1 mark.

  5. (v) · 1 mark

    Convert the character '7' to the number 7 without parseInt.

    Answer

    Answer: '7' - '0' = 7.

    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.

[2] Output of (char) (ch + 1) and ch + 1 for ch = 'D'. (2 marks)

StepMarks
Cast gives E.1
Uncast sum gives 69.1

[5] digitSum(String). (5 marks)

StepMarks
Loop over characters.1
Character.isDigit test.1
ch - '0' conversion and accumulation.2
Return and test value.1

[10] Class Cipher. (10 marks)

StepMarks
Data members and constructor.1
accept( ).1
shift( ) for upper case with wrap.2
shift( ) for lower case and other characters.1
encode( ).2
display( ) invoking encode( ).1
main( ) and documentation.2

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

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

  • Mistake: Forgetting that char arithmetic gives an int

    CISCE examiners' 2024 report lists 'Type cast the characters to find ASCII code and vice-versa' among the difficult topics and 'Type casting during obtaining ASCII codes of characters and vice-versa' among the confused concepts.

    Wrong working

    System.out.println(ch + 1); // expecting the next letter

    Right working

    System.out.println((char) (ch + 1));

    Do this: Cast back to char whenever a letter is wanted; use (int) ch when the code is wanted.

    Usually costs: 1-2 marks.

  • Mistake: Expecting a cast to round

    (int) 7.8 is 7, not 8.

    Do this: Use Math.round( ) for rounding; a cast truncates towards zero.

    Usually costs: 1 mark.

  • Mistake: Overflow in int before widening

    The type of an expression is decided by its operands; storing in a long afterwards does not help.

    Do this: Cast an operand to long first: (long) a * b.

    Usually costs: Wrong output.

  • Mistake: Wrong wrapper names and methods

    Writing Int.parseInt or Char.isDigit.

    Do this: Integer.parseInt, Character.isDigit; all wrappers are in java.lang.

    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 · Very short answerBoard level

    What is the size of a char in Java?

    Hint

    Unicode.

    Answer

    16 bits (2 bytes).

  2. Q2 · 1 mark · Very short answerBoard level

    What is the default type of the literal 2.5?

    Hint

    Not float.

    Answer

    double.

  3. Q3 · 1 mark · Very short answerBoard level

    What does Integer.toBinaryString(10) return?

    Hint

    Base 2.

    Answer

    "1010"

  4. Q4 · 1 mark · Very short answerHarder board level

    Give the output of System.out.println('a' + 'b');

    Hint

    Two chars add as ints.

    Answer

    195 (97 + 98).

  5. Q5 · 2 marks · Short answerHarder board level

    [2] Distinguish between implicit and explicit type conversion with an example of each.

    Hint

    Who does it?

    Answer

    Implicit (coercion) is done automatically by Java from a smaller to a larger type without loss, e.g. double d = 5; Explicit (casting) is written by the programmer and may lose data, e.g. int n = (int) 7.8; giving 7.

  6. Q6 · 2 marks · Short answerHarder board level

    [2] Write a statement that converts a lower-case letter ch to upper case without using Character.toUpperCase.

    Hint

    Difference of 32.

    Answer

    ch = (char) (ch - 32); (or (char) (ch - 'a' + 'A')); for 'g' this gives 'G'.

  7. Q7 · 5 marks · Long answerHOTS / competency

    [5] Write a method int digitSum(String s) that returns the sum of all digit characters in s (for "a1b22c3" it returns 8), using Character.isDigit and char arithmetic.

    Hint

    ch - '0'.

    Answer

    int digitSum(String s) { int sum = 0; for (int i = 0; i < s.length(); i++) { char ch = s.charAt(i); if (Character.isDigit(ch)) sum += ch - '0'; } return sum; } → 1 + 2 + 2 + 3 = 8 (checked by running the code).

  8. Q8 · 10 marks · Long answerHOTS / competency

    [10] Design a class CaseFlip with data member str; a constructor; void accept( ); String flip( ) that returns str with upper-case letters changed to lower case and lower-case to upper case using char arithmetic only, other characters unchanged; and void show( ) that prints the original and flipped strings and the number of letters changed. Write main( ).

    Hint

    Add or subtract 32.

    Answer

    Constructor sets str = ""; accept( ) reads a line; flip( ): for each ch, if 'A' <= ch <= 'Z' append (char) (ch + 32), else if 'a' <= ch <= 'z' append (char) (ch - 32), else append ch; show( ) calls flip( ), prints both strings and counts letters (Character.isLetter). For "Java 17 Rocks!" the output is "jAVA 17 rOCKS!" with 9 letters changed (checked by running the code).

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.Range of byte?Show answer

    -128 to 127.

  2. 2.Wrapper of int?Show answer

    Integer.

  3. 3.(char) 66?Show answer

    'B'.

  4. 4.'9' - '0'?Show answer

    9.

  5. 5.Integer.MAX_VALUE + 1 in int?Show answer

    -2147483648 (overflow).

Best order to study it

  1. Eight primitive types: sizes, ranges, defaults, literals.
  2. Wrapper classes, autoboxing and their methods.
  3. Widening coercion and promotion in expressions.
  4. Narrowing casts and char-int conversion.
  5. Programs using casting: ciphers, case change, digit sums.

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 bits; boolean true/false.
  • Wrappers: Byte, Short, Integer, Long, Float, Double, Character, Boolean.
  • Autoboxing: int → Integer; unboxing: Integer → int.
  • Widening automatic: byte → short → int → long → float → double; char → int.
  • Narrowing explicit: (int) 9.99 = 9; (byte) 300 = 44; (char) 65 = 'A'.
  • char + int = int; cast back with (char).
  • Integer.parseInt, Double.parseDouble, Integer.toString, toBinaryString; Character.isDigit and friends.

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

Is String a primitive type?

No. String is a class in java.lang; a String variable holds a reference to a String object.

What is the size of boolean?

The Java language does not fix it; it stores only true or false. Many books say 1 bit; do not quote a size unless asked, and then say it is JVM-dependent.

Why do Integer objects with equal values sometimes compare unequal with ==?

== compares references for objects. Use equals( ) to compare the values of wrapper objects.

Based on: CISCE ISC 2027 Computer Science (868) syllabus; CISCE ISC Computer Science (868) Paper 1 specimen question paper and answer key (latest specimen, 2025); CISCE Analysis of Pupil Performance, ISC Computer Science (868), 2025; CISCE Analysis of Pupil Performance, ISC Computer Science (868), 2024