TutorialsICSEClass 10Computer Applications

Wrapper Classes and Character Methods in Java for ICSE Class 10

ICSE Class 10 Computer Applications notes on wrapper classes: primitive types and their wrappers, autoboxing and unboxing, parseInt and valueOf conversions, and Character class methods like isDigit, isLetter and toUpperCase, with output questions and a solved program.

10 min read
Wrapper Classes and Character Methods in Java for ICSE Class 10

Library classes, especially wrapper classes and the Character class, are a regular source of 1- and 2-mark questions in the ICSE Class 10 board paper. They also appear in programs that check each character of a string. These notes give you every method in the syllabus, with outputs.

What is a wrapper class?

A wrapper class "wraps" a primitive value inside an object, so the value can be used where Java expects an object and so you can use helpful methods such as converting text to numbers. All the wrapper classes are in java.lang.

Primitive   Wrapper class
byte        Byte
short       Short
int         Integer
long        Long
float       Float
double      Double
char        Character
boolean     Boolean
Only two wrapper names differ from their primitive type: int → Integer and char → Character.

Autoboxing and unboxing

  • Autoboxing: Java automatically converts a primitive into its wrapper object.
  • Unboxing: Java automatically converts a wrapper object back into a primitive.
Integer obj = 25;          // autoboxing: int → Integer
int n = obj;               // unboxing:   Integer → int
Double d = 3.5;            // autoboxing
double x = d + 1.5;        // unboxing, then addition: 5.0

Converting a String to a number

int a = Integer.parseInt("123");           // 123
double b = Double.parseDouble("45.6");     // 45.6
long c = Long.parseLong("9876543210");     // 9876543210
Integer d = Integer.valueOf("50");         // an Integer object holding 50
  • parseXxx() returns a primitive.
  • valueOf() returns a wrapper object.
  • If the string is not a valid number, for example Integer.parseInt("12a"), Java throws a NumberFormatException.

Converting a number to a String

String s1 = Integer.toString(75);     // "75"
String s2 = String.valueOf(3.14);     // "3.14"
String s3 = 42 + "";                  // "42" (a quick trick)

Character class methods

All of these are static, so you call them as Character.method(ch).

  • isLetter(ch): true if ch is a letter. Character.isLetter('a') is true.
  • isDigit(ch): true if ch is a digit 0–9. Character.isDigit('7') is true.
  • isLetterOrDigit(ch): true for a letter or a digit. Character.isLetterOrDigit('#') is false.
  • isWhitespace(ch): true for a space, tab or newline. Character.isWhitespace(' ') is true.
  • isUpperCase(ch): Character.isUpperCase('Q') is true.
  • isLowerCase(ch): Character.isLowerCase('Q') is false.
  • toUpperCase(ch): Character.toUpperCase('b') returns 'B'.
  • toLowerCase(ch): Character.toLowerCase('B') returns 'b'.
toUpperCase and toLowerCase return a char. Characters that are not letters are returned unchanged: Character.toUpperCase('5') is '5'.

Output questions

Q1. System.out.println(Integer.parseInt("20") + Integer.parseInt("30"));

Answer: 50. Both strings are converted to int and then added.

Q2. System.out.println("20" + 30);

Answer: 2030. This is string joining, not addition.

Q3. char c = 'd'; System.out.println(Character.toUpperCase(c) + 1);

Answer: 69. toUpperCase gives 'D', whose code is 68. Adding 1 promotes it to int, so the result is 69.

Q4. System.out.println(Character.isDigit('5') && Character.isLetter('5'));

Answer: false. '5' is a digit but not a letter.

Solved program: count the character types in a string

import java.util.Scanner;

class CharCount {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a string: ");
        String s = sc.nextLine();
        int upper = 0, lower = 0, digits = 0, spaces = 0, others = 0;

        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if (Character.isUpperCase(ch)) upper++;
            else if (Character.isLowerCase(ch)) lower++;
            else if (Character.isDigit(ch)) digits++;
            else if (Character.isWhitespace(ch)) spaces++;
            else others++;
        }
        System.out.println("Uppercase: " + upper + ", Lowercase: " + lower
                + ", Digits: " + digits + ", Spaces: " + spaces + ", Others: " + others);
    }
}

Sample run: for "ICSE Exam 2027!", the output is Uppercase: 5, Lowercase: 3, Digits: 4, Spaces: 2, Others: 1.

Practice questions

  1. Name the wrapper classes of char and boolean.
  2. What is the difference between Integer.parseInt() and Integer.valueOf()?
  3. What exception does Double.parseDouble("abc") throw?

Answers:

  1. Character and Boolean.
  2. parseInt returns a primitive int, while valueOf returns an Integer object.
  3. NumberFormatException.

Revise these methods alongside our 10 most important String programs for ICSE Class 10.