Library classes | ICSE Class 10 Computer Applications Notes
On this page
This note covers Java library classes, primitive and composite data types, wrapper classes, autoboxing and unboxing, conversion of numeric text, character classification, case conversion, and the interpretation of method calls and their results.
What are library classes, classes and objects?
How does a class describe a type?
A class describes a type of object, including the data it can hold and the operations it can perform. An object is an instance of a class: a particular object created according to that class definition.
A data type specifies the kind of value that can be represented and the operations available for it. A variable is a named place for storing a value during a program. A program is a set of instructions for a computer.
The variables belonging to an object describe its state, meaning the data it currently holds. Its methods describe its behaviour, meaning the operations it can perform. A method is a named operation defined as part of a class.
Definition: A library class is a predefined class supplied in a programming library. It provides reusable facilities that a program can use without writing their internal implementation again.
Why use an existing library class?
Java supplies classes for common operations. Using an existing method lets a program request the operation through a defined method call. A method call is an instruction that invokes a method, supplying any values the method requires.
A user-defined class is written by the programmer. A class may be considered as a new data type created by the user, that has its own functionality. A library class is already provided; both kinds of class can supply types used in programs.
A primitive type is a basic value type built into Java. A wrapper class is a class associated with a primitive type, allowing a value of that primitive type to be represented by an object. Wrapper classes also provide useful operations for working with values, including numeric text conversion and character tests.
Separate the class from the operation it supplies. The class name identifies where a method belongs. The method name identifies what operation is requested. The value supplied to the operation and the value it returns have types of their own.
How do primitive types differ from composite types?
What does each kind of variable hold?
A primitive type is one of Java's built-in basic value types. A composite type, in the class-based sense, is a type defined through a class whose objects can bring together data and operations. A class type is not itself a primitive type.
A variable of primitive type holds a primitive value. A variable of class type holds a reference, a value that refers to an object, or the special value null, which means that no object is referred to. The reference and the object are distinct.
| Feature | Primitive type | Class type |
|---|---|---|
| Definition | A basic type built into Java | A type described by a class |
| Variable contents | A primitive value | An object reference or null |
| Associated operations | Language operations appropriate to the type | Methods defined or available in the class |
| Relationship to wrappers | The basic value being represented | The corresponding wrapper can represent that value as an object |
How should declarations be read?
A declaration introduces a variable and states its type. A keyword is a word reserved by Java for a language purpose. Primitive type names are keywords; wrapper names are class names. Their spelling and capitalisation matter.
int is a primitive type for whole numbers, whereas Integer is its wrapper class. Declaring a variable as Integer does not make Integer another spelling of int. It states that the variable has a class type, with the corresponding object representation.
A String is a class whose objects represent text. Text containing digits remains text until an appropriate conversion interprets it numerically. Numeric parsing means interpreting text to obtain a numeric value. String is therefore relevant to parsing, but it is not the wrapper class corresponding to a primitive numeric type.
When distinguishing types, identify the declaration first and the stored value second. A value's printed appearance does not by itself reveal whether the program stored a primitive number, a wrapper reference or text. This distinction explains why conversion methods specify their input and output types.
Which wrapper class corresponds to each primitive type?
Property: Every primitive type has a corresponding class wrapper
Every primitive type has a corresponding wrapper class. The relationship is between a particular primitive type and its associated class, rather than between a general category such as “number” and a single universal wrapper.
The integer types represent whole numbers. The floating-point types represent numerical values using a format that can represent fractional parts. The character and Boolean types serve different purposes from these numeric categories.
| Primitive type | Purpose of the primitive type | Wrapper class |
|---|---|---|
| byte | An integer type | Byte |
| short | An integer type | Short |
| int | An integer type | Integer |
| long | An integer type | Long |
| float | A floating-point type | Float |
| double | A floating-point type | Double |
| char | A character value | Character |
| boolean | A truth value, true or false | Boolean |
The primitive type char is used for character values. A boolean value is either true or false, the Java values for a satisfied or unsatisfied condition. These terms also describe the results of character classification methods.
Why is the exact class name important?
Most wrapper names resemble the corresponding primitive name with an initial capital letter. The names Integer and Character need particular attention because the primitive names are int and char. Letter case distinguishes upper-case and lower-case forms of letters. Java distinguishes those forms in program names.
An identifier is a name used for a program element such as a variable, class or method. Correct capitalisation forms part of the identifier. A near match in spelling is not sufficient when naming a library class or invoking one of its methods.
The wrapper pairs should be learnt in both directions. Given a primitive declaration, identify its corresponding wrapper. Given a wrapper declaration, identify the underlying primitive type. This prepares you to recognise the direction of automatic conversions.
The classes provide different facilities. Integer, Long, Float and Double supply the numeric parsing methods considered below. Character supplies the character classification and case conversion methods. The existence of a wrapper class does not mean that every wrapper provides every operation in the chapter.
What happens during autoboxing and unboxing?
Which way does each conversion go?
Definition: Autoboxing is the automatic conversion of a primitive value to its corresponding wrapper representation. Unboxing is the conversion from a wrapper object to the corresponding primitive value.
The word automatic means that the programmer does not explicitly call a conversion method for that step. Java performs the conversion in a context that requires it, such as an appropriate assignment between a primitive type and its corresponding wrapper type.
An assignment stores the value of an expression in a variable. An expression is a piece of code that produces a value. The assignment symbol = places the value on its right into the variable on its left.
| Conversion | Starting representation | Required representation |
|---|---|---|
| Autoboxing | A primitive value | The corresponding wrapper representation |
| Unboxing | A wrapper object | The corresponding primitive value |
| Numeric parsing | Text representing a number | A numeric value of the requested type |
How can the direction be recognised?
Read the types on both sides of the assignment. When an int value is used where an Integer reference is required, autoboxing is involved. When an Integer object is used where its int value is required, unboxing is involved.
The same distinction applies to the other corresponding primitive and wrapper pairs. Focus on whether the result is an object representation or a primitive value. Do not decide from whether the source and destination names begin with similar letters.
Unboxing needs an actual wrapper object from which a value can be obtained. Attempting to unbox null causes a NullPointerException, a runtime exception indicating an invalid operation requiring an object. A runtime exception is an exceptional condition encountered while a program executes.
Rule: Keep primitive-to-wrapper conversion, wrapper-to-primitive conversion and text-to-number conversion separate. Autoboxing does not interpret the contents of a String as a number.
Boxing and unboxing concern the representation of an existing primitive value. Parsing concerns the interpretation of text. A parsing call may appear within a larger assignment that also involves boxing, but those remain separate operations with separate type changes.
How do you read a library method description and call?
What do the input and return types mean?
A method description states the return type, the kind of value produced by the method, followed by its name and parameter information. A parameter is a variable declared to receive a value when the method is called.
In int parseInt(String s), int is the return type, parseInt is the method name, and s is the parameter name. The type String says that s receives text. These are distinct pieces of information, even though they appear together.
An argument is the value or expression actually supplied in a method call. In the call Integer.parseInt(s), assume s is a previously declared String variable. Its current text value is supplied as the argument to the parsing method.
Syntax: Integer.parseInt(s) invokes parseInt through Integer. The dot selects the method belonging to the class. Parentheses enclose the argument. Here s denotes the String to be interpreted as an integer.
How does the class name affect the call?
A static method belongs to a class and can be called through the class name. The parsing and Character methods covered here are static. Their usual call form names the class, then the method, then the argument in parentheses.
The identifier ch will denote a char variable holding the character being examined or converted. Thus Character.isDigit(ch) calls a Character method with ch as its argument. It produces a boolean result, rather than a numeric value extracted from the character.
A statement is a complete program instruction. A semicolon, written ;, ends many Java statements. A method call used inside an assignment is part of that statement; the call's returned value supplies the value for the assignment.
For each call, establish the class, the operation, the argument type and the return type. Then decide what the surrounding code does with the returned value. A method may produce a value without displaying it or storing it in the original variable.
The input type and output type need not be the same. Parsing takes String and returns a numeric primitive. Character tests take char and return boolean. Case conversion takes char and returns char. These patterns are more useful than memorising method names in isolation.
How do parseInt and parseLong convert numeric text?
What does integer parsing do?
Parsing means interpreting text according to a required form. Integer parsing interprets a String as a whole-number value. The methods discussed here accept a String argument and return a primitive value, even though the methods belong to wrapper classes.
| Method description | Call using String s | Result type |
|---|---|---|
| int parseInt(String s) | Integer.parseInt(s) | int |
| long parseLong(String s) | Long.parseLong(s) | long |
long is an integer type with a wider range, meaning an interval of representable values, than int. Choose the parsing method for the required result type. The fact that the input is text does not remove the limits of the numeric type produced by the conversion.
What must be true of the input?
For these String-only integer parsing methods, the text must represent a valid decimal integer within the result type's range. Decimal means the ordinary base-ten representation. A leading plus or minus sign can indicate the sign of the integer.
A range is the interval of values a type can represent. Text can describe a number outside that interval, but the parsing method cannot return that number in the requested type. Valid-looking digits alone do not establish that conversion will succeed.
If the String is unsuitable for the requested integer conversion, the method throws a NumberFormatException, a runtime exception indicating that the text cannot be interpreted in the required numeric form or range. The call does not produce a normal integer result in that case.
Rule: Integer.parseInt(s) returns int; Long.parseLong(s) returns long. Neither method returns a String or a wrapper object merely because it is invoked through a wrapper class.
Parsing does not change the declared type of s. The String supplies the input; the method returns a separate numeric value. The program must use that returned value if it is needed in a later calculation or assignment.
Do not describe parsing as removing quotation marks. Quotation marks are part of how text may be written in source code. Parsing is an operation on a String value during execution, whether that text was written in the program or received through input.
How do parseFloat and parseDouble differ from integer parsing?
Which return type does each method provide?
The parseFloat and parseDouble methods interpret text as floating-point values. They belong to Float and Double respectively. Their arguments are Strings, just as with the integer parsing methods, but their return types differ.
| Method description | Call using String s | Result type |
|---|---|---|
| float parseFloat(String s) | Float.parseFloat(s) | float |
| double parseDouble(String s) | Double.parseDouble(s) | double |
float and double are floating-point primitive types. Double precision provides greater precision than float. Precision concerns how much detail can be retained in representing a number; it is different from the range of values a type can represent.
A floating-point value is not a guarantee of exact representation of every decimal fraction. The result is a value represented in the target floating-point type. Do not assume that a longer text input makes the returned value arbitrarily precise.
How should a parsing method be selected?
First identify the required numeric type. Then select the wrapper class and its matching parsing method. Use the return type as the check: int with parseInt, long with parseLong, float with parseFloat, and double with parseDouble.
A decimal point in numeric text does not make integer parsing appropriate. Integer and floating-point parsers accept different numeric forms. If the text is unsuitable for a floating-point parsing method, a NumberFormatException can occur instead of a normal numeric result.
Syntax: Float.parseFloat(s) returns a float value, and Double.parseDouble(s) returns a double value. In both calls, s is a String containing a representation accepted by the selected method.
Separate the conversion result from any subsequent assignment. A parser's return type is fixed by the method selected, even if later code converts or boxes that value. Naming the destination variable does not redefine the return type of the parsing method.
A clear explanation of a parsing expression therefore states the text input, the method's class, the requested conversion and the primitive result type. When an actual numeric result is requested, the complete input text must also be known.
How do Character methods classify a character?
Property: Character classification methods return boolean
Character classification determines whether a character belongs to a category. Each classification method here takes a char argument and returns boolean. The returned value tells whether the specified property holds for the supplied character.
| Method description | Call using char ch | Condition tested |
|---|---|---|
| boolean isDigit(char ch) | Character.isDigit(ch) | Whether ch is a digit |
| boolean isLetter(char ch) | Character.isLetter(ch) | Whether ch is a letter |
| boolean isLetterOrDigit(char ch) | Character.isLetterOrDigit(ch) | Whether ch is a letter or a digit |
| boolean isLowerCase(char ch) | Character.isLowerCase(ch) | Whether ch is a lower-case character |
| boolean isUpperCase(char ch) | Character.isUpperCase(ch) | Whether ch is an upper-case character |
| boolean isWhitespace(char ch) | Character.isWhitespace(ch) | Whether ch is Java whitespace |
Letter case distinguishes lower-case and upper-case forms where such forms exist. A digit is a character used in writing a number; testing whether a character is a digit does not convert it into a numeric primitive value.
What distinctions should be preserved?
Whitespace refers here to the character category tested by Java's isWhitespace method, including ordinary space, tab characters used for spacing, and line-feed characters used for line breaks. It is not a synonym for every character that is neither a letter nor a digit.
The letter-or-digit test accepts either of the named categories. It does not mean “letter and digit at the same time”, nor does it accept every possible character. Punctuation, meaning written marks used for separation or expression rather than letters or digits, is a different category.
Do not classify lower-case characters merely by rejecting upper-case characters. A digit can fail both case tests. More generally, a false result for one property does not automatically prove the opposite property unless those properties exhaust the possible cases.
Rule: Character.isDigit(ch), Character.isLetter(ch), Character.isLetterOrDigit(ch), Character.isLowerCase(ch), Character.isUpperCase(ch) and Character.isWhitespace(ch) report properties. They do not replace the character stored in ch.
A classification result can be used as the condition of an if statement, which selects an instruction or a block, a group of instructions, when its condition is true. The test's boolean return type is what makes it suitable as a condition.
These methods use Java's character classifications. Do not redefine their meaning through a home-made list of characters. When explaining a supplied call, state the relevant property of the input and then give the boolean result.
How do toLowerCase and toUpperCase convert characters?
Property: Character case conversion methods return char
Case conversion obtains a character in a requested letter case where an appropriate mapping exists. Unlike a classification method, a case conversion method returns char. Its result is a character value, not an answer of true or false.
| Method description | Call using char ch | Returned value |
|---|---|---|
| char toLowerCase(char ch) | Character.toLowerCase(ch) | The corresponding lower-case character, or the original character if no conversion applies |
| char toUpperCase(char ch) | Character.toUpperCase(ch) | The corresponding upper-case character, or the original character if no conversion applies |
A case mapping is the relationship used to obtain a corresponding character in another case. Calling a case conversion method does not imply that the input must change. A character already in the requested form can be returned unchanged.
Why does assignment matter?
The method receives the value supplied by ch and returns its result. It does not automatically store that result back in ch. To retain the converted character in that variable, the program must assign the returned value to ch.
Syntax: ch = Character.toUpperCase(ch); assigns the returned upper-case character value to ch. Here ch is a previously declared char variable, = performs assignment, the dot selects the class method, and the semicolon ends the statement.
Read the right-hand side first. The current character value is passed to toUpperCase, which returns a char. The assignment then stores the returned value on the left. This order explains why the later value of ch depends on whether assignment took place.
Character methods operate on the supplied character value. A String is a different type representing text. Do not pass a String to the char forms shown here merely because the text contains a single visible character.
The names isUpperCase and toUpperCase express different tasks. The first asks whether a property holds; the second obtains a converted character. The corresponding distinction applies to isLowerCase and toLowerCase. Check both the method name and its return type when interpreting a statement.
How can library method expressions be checked systematically?
Which information is needed before tracing?
A trace follows the execution of a program or expression to determine values and results. Begin with the declarations and the complete supplied input. Without the String contents or character value, a particular parsing or classification result may be undetermined.
Distinguish a return value, which a method supplies to its caller, from output, which the program displays. System.out.println is an output operation that displays its argument and ends the line. A parsing or classification call does not itself print its result.
- Identify the argument. Establish whether the supplied value is String, char, a numeric primitive or a wrapper object.
- Identify the class and method. Match the operation to Integer, Long, Float, Double or Character as appropriate.
- Establish the return type. Separate numeric results, boolean test results and char conversion results.
- Evaluate the operation. Use the complete input and the method's conditions rather than guessing from the variable's name.
- Follow the result. Check whether the value is assigned, printed, tested as a condition or left unused.
How can different operations be kept separate?
When several operations appear in a statement, describe each type change in order. Numeric text can be parsed to a primitive value; that value may then be boxed if a wrapper representation is required. The parsing and boxing should not be given the same name.
Similarly, a character can be converted and the returned character can later be classified. The conversion produces char; the classification produces boolean. Each step must receive an argument of the appropriate type, regardless of how similar the method names appear.
A useful explanation names the operation and accounts for its result. For a test, state the property checked. For a conversion, state the destination type or case. For an assignment, state which variable receives the result. For output, state what is actually displayed.
Finally, check for exceptional conditions. An unsuitable numeric String cannot be treated as a successfully parsed number. A null wrapper cannot supply a primitive through unboxing. A correct trace respects these conditions instead of continuing with an invented value.
Glossary
- Library class — A predefined class providing reusable facilities that programs can invoke through its available methods.
- Primitive type — A basic Java value type built into the language, rather than defined by a class.
- Composite type — A type that, through a class, can bring together data and operations in objects.
- Object — An instance of a class, representing a particular object created according to that class definition.
- Wrapper class — A class corresponding to a primitive type, allowing its values to have an object representation.
- Autoboxing — Automatic conversion of a primitive value to the representation provided by its corresponding wrapper class.
- Unboxing — Conversion from a wrapper object to the primitive value associated with its corresponding primitive type.
- Parsing — Interpreting text according to a required form to obtain a value of the requested type.
- Parameter — A variable declared by a method to receive a value supplied when the method is called.
- Argument — The value or expression supplied to a method when that method is called.
- Return type — The declared kind of value that a method supplies as its result to the caller.
- Static method — A method belonging to a class that can be invoked through the class name.
- Character classification — Testing whether a supplied character has a specified property, with a boolean result.
- Case conversion — Obtaining a corresponding character in the requested letter case where an appropriate mapping exists.
- Null — A special reference value indicating that a variable does not currently refer to an object.
Common errors and misconceptions
- Misconception: int and Integer are interchangeable names for one primitive type. Correct: int is primitive, while Integer is its wrapper class; their variable representations differ.
- Misconception: String is the wrapper for char. Correct: Character is the wrapper for char. String represents text and is used as the input type of the numeric parsing methods.
- Misconception: Parsing numeric text is autoboxing. Correct: Parsing interprets a String as a number. Autoboxing converts a primitive value to its corresponding wrapper representation.
- Misconception: Integer.parseInt returns an Integer object because Integer appears before the dot. Correct: The method returns an int primitive value; the class owning the method does not determine its return type by itself.
- Misconception: A character that is not upper case must be lower case. Correct: Some characters satisfy neither property. Test the property required instead of inferring it from an unrelated negative result.
- Misconception: isDigit converts a digit character into a number. Correct: It returns boolean, reporting whether the supplied character is a digit without performing numeric parsing.
- Misconception: Calling toUpperCase automatically changes ch. Correct: It returns a char value. The program must assign that result to ch if ch is to retain it.
- Misconception: A null wrapper can be unboxed to a default primitive value. Correct: Unboxing null throws NullPointerException because there is no wrapper object from which to obtain a value.
Exam-style questions with model answers
Q1. Define autoboxing and unboxing, stating the direction of each conversion. [2 marks]
- Autoboxing automatically converts a primitive value to its corresponding wrapper representation.
- Unboxing converts a wrapper object to the corresponding primitive value represented by that object.
Q2. Name the wrapper class corresponding to each of these primitive types: int, long, float and char. State that each named result is a class type. [4 marks]
- The wrapper for int is Integer. Integer is a class type, distinct from the primitive integer type int.
- The wrapper for long is Long. Long is a class type associated with the primitive integer type long.
- The wrapper for float is Float. Float is a class type associated with the primitive floating-point type float.
- The wrapper for char is Character. Character is a class type associated with the primitive character type char.
Q3. A variable s has type String. Give the class-qualified parsing call and primitive return type for each required result: int, long, float and double. Do not evaluate s. [4 marks]
- Integer.parseInt(s) interprets suitable integer text and returns int. The method is invoked through the Integer wrapper class.
- Long.parseLong(s) interprets suitable integer text and returns long. The method is invoked through the Long wrapper class.
- Float.parseFloat(s) interprets suitable floating-point text and returns float. The method is invoked through the Float wrapper class.
- Double.parseDouble(s) interprets suitable floating-point text and returns double. The method is invoked through the Double wrapper class.
Q4. A variable ch has type char. Give the Character method call for testing each property: digit, letter, letter or digit, lower case, upper case and whitespace. Explain the result of each test without assuming a particular character. [6 marks]
- Character.isDigit(ch) returns true when the supplied character is a digit and false otherwise. The result is boolean, not a numeric conversion.
- Character.isLetter(ch) returns true when the supplied character is a letter and false otherwise. It classifies the character without changing ch.
- Character.isLetterOrDigit(ch) returns true when the character is a letter or a digit and false otherwise. Either named category satisfies the test.
- Character.isLowerCase(ch) returns true when the supplied character has the lower-case property and false otherwise. Its return type is boolean.
- Character.isUpperCase(ch) returns true when the supplied character has the upper-case property and false otherwise. Its return type is boolean.
- Character.isWhitespace(ch) returns true when the character is classified as Java whitespace and false otherwise. It does not test for all non-letter characters.
Q5. A previously declared char variable ch is used in the statement ch = Character.toUpperCase(ch);. Explain the argument, the returned value and the effect of assignment. Do not assume an initial character. [3 marks]
- The argument is the current char value held in ch. It is supplied to the static method toUpperCase through the Character class.
- The method returns a char representing the corresponding upper-case character. If no case conversion applies, the returned character is the original one.
- The assignment stores that returned value in ch. The stored character therefore reflects the method result, rather than the call changing ch automatically.
Q6. Explain two differences between a primitive variable and a class-type variable: what each holds and whether its type is a built-in basic type or a class type. [2 marks]
- A primitive variable holds a primitive value, whereas a class-type variable holds an object reference or null.
- A primitive type is a built-in basic Java type, whereas a class type is described by a class.
Q7. A String variable s contains text that is invalid for Integer.parseInt. Separately, an Integer variable contains null and is used where unboxing to int is required. Identify the exception in each situation and explain why neither supplies a normal primitive result. [3 marks]
- The parsing call throws NumberFormatException because the supplied String is invalid for the required integer conversion. It does not return a normal int result.
- The attempted unboxing throws NullPointerException because the Integer reference is null. There is no wrapper object from which an int value can be obtained.
- These failures have different causes: unsuitable numeric text in the first case and a missing wrapper object in the second. Neither justifies inventing a default value.
Q8. For a char variable ch, compare Character.isUpperCase(ch) with Character.toUpperCase(ch) in terms of purpose, return type and effect on ch when the result is not assigned. [3 marks]
- Character.isUpperCase(ch) tests whether the character has the upper-case property. Character.toUpperCase(ch) obtains the corresponding upper-case character where an appropriate mapping applies.
- The classification method returns boolean, meaning true or false. The conversion method returns char, meaning a character value rather than a truth value.
- Neither call automatically assigns a new value to ch. When the result is not assigned back, the original character stored in ch remains unchanged.
Key takeaways
- A library class supplies reusable facilities, while a wrapper class connects a primitive type with its corresponding object representation.
- Primitive variables hold primitive values; variables of class type hold object references or the special reference value null.
- Autoboxing converts primitive values to their corresponding wrapper representations, while unboxing obtains primitive values from wrapper objects.
- The numeric parsing methods accept String arguments and return int, long, float or double according to the selected method.
- Character classification methods return boolean values describing properties; they neither parse numbers nor automatically change the supplied character.
- Case conversion methods return char values, and assignment is needed to retain the returned character in the original variable.
- Read a method's class, argument type and return type before tracing how the surrounding statement uses its result.
- Invalid integer text and unboxing null cause different exceptions, so neither situation should be traced using an invented normal value.
Test yourself
Which class wraps the primitive type char?
Character is the wrapper class for char; String is a separate class representing text.
What direction of conversion does autoboxing describe?
Autoboxing converts a primitive value to the representation provided by its corresponding wrapper class.
What primitive type does Long.parseLong return?
It returns long, even though the method is invoked through the Long wrapper class.
For a String variable s, which call parses it to double?
Double.parseDouble(s) parses suitable numeric text in s and returns a primitive double value.
Does Character.isLetterOrDigit return a converted character?
No. It returns boolean, reporting whether the supplied character is a letter or a digit.
Does a false result from isUpperCase prove that isLowerCase is true?
No. Some characters have neither case property, so the lower-case property must be tested separately.
What distinguishes isLowerCase from toLowerCase?
isLowerCase returns a boolean classification result, while toLowerCase returns a char representing the applicable lower-case form.
What happens when a null wrapper is unboxed?
Unboxing null throws NullPointerException because no wrapper object is available to supply the primitive value.
