Loading...
Please wait while we prepare your content
Please wait while we prepare your content
Solutions for Computer Applications, Class 10, ICSE
What are wrapper classes?
Wrapper classes are specially designed classes that act as wrappers to primitive data types so that primitive values can be accessed as objects.
For example, Integer is a wrapper class for int data type and Float is a wrapper class for float data type.
Name the numeric wrapper classes in Java.
The numeric wrapper classes in Java are:
What is the need of wrapper classes when there are primitive datatypes ?
The need of wrapper classes when there are primitive data types are as follows:
When do the numeric wrapper class constructors raise NumberFormatException ?
The numeric wrapper class constructors may raise NumberFormatException at the time of conversion of String arguments to primitive data types. The exception is raised when the String argument cannot be converted to the desired data type.
For example,
int val = Integer.parseInt("A");
Here, the String argument "A"
cannot be converted to int type and therefore, NumberFormatException is thrown.
Name some methods that are commonly available in all wrapper classes and in all numeric wrapper classes.
Methods available in all wrapper classes are:
Methods commonly available in all numeric wrapper classes are:
What is autoboxing ? What is auto-unboxing ? How are these useful ?
The automatic conversion of primitive data type into an object of its equivalent wrapper class is known as Autoboxing.
The automatic conversion of an object of wrapper class into primitive data type is known as Auto-unboxing.
These are useful as:
Which methods return primitive values from Wrapper class objects?
The methods which return primitive values from Wrapper class objects are as follows:
Which methods return Wrapper class objects from primitive values ?
The methods which return Wrapper class objects from primitive values are as follows:
Predict the output.
int res = Integer.valueOf("100").compareTo(new Integer(100));
System.out.println(res);
0
Here, Integer.valueOf("100")
returns an Integer object storing 100
and new Integer(100)
creates a new Integer object with the value 100
.
Integer.compareTo() compares two Integer objects numerically, after auto-unboxing is performed by the compiler and the Integer objects are converted to integer type values. The function compareTo() returns 0 as primitive values of both the objects are equal.
Find the error:
Integer obj = new Integer("A");
System.out.println(obj);
Integer obj = new Integer("A");
statement will generate NumberFormatException at the time of conversion of String argument "A"
to Integer object. The exception is raised because "A" is not a valid string representation of a numeric value, therefore it cannot be converted to Integer object.
Find the error :
double n2 = Double.parseDouble("2");
double n3 = Double.parseDouble("OCA");
System.out.println(n2 + " " + n3);
double n3 = Double.parseDouble("OCA");
statement will generate NumberFormatException at the time of conversion of String argument "OCA"
to double data type. The exception is raised because "OCA" is not a valid string representation of a numeric value, therefore it cannot be converted to Integer object.
Predict the output :
double n1 = Double.parseDouble("8.0");
double n2 = Double.parseDouble("2");
System.out.println(n1 + " " + n2);
8.0 2.0
parseDouble() converts String arguments passed to it into double data type. Thus, 8.0
will be assigned to n1
and 2.0
will be assigned to n2
.
The values of n1
and n2
will be printed with a space " "
between them.
What important is automatically happening in following code ?
long a = 124235L;
Long b = new Long(a);
long c = b.longValue();
System.out.println(c);
Long b = new Long(a);
This statement boxes long type a
into a Long object b
, boxing a primitive type into a Wrapper class object.
long c = b.longValue();
This statement unboxes Long object b
and stores it in long variable c
, converting a Wrapper class object into a primitive data type.
Predict the output :
Integer c = 155;
Integer d = 155;
System.out.println(c == d);
System.out.println(c.equals(d));
false
true
c == d
compares two Integer objects and returns false
because when we compare two objects using the operator "==", Java compares their reference i.e., their memory address and not their value. Since these are two different objects stored at two different locations in memory, Java produces result false
c.equals(d)
method compares the values of the Integer objects after they are auto-unboxed by the compiler and converted to primitive data types. The function returns true
as both the primitive type values are equal.
In the following code, identify the statement where autoboxing is taking place :
Integer i = new Integer(10);
if (i < 100)
System.out.println(i);
else
System.out.println(i + 10);
Autoboxing is not taking place in this code as in the statement Integer i = new Integer(10);
, we are explicitly making an Integer object.
if(i < 100)
Auto-unboxing is taking place in this statement, as the value of an object cannot be compared to a numeric value 100
directly but a primitive integer type value can be compared. So, unboxing takes place before the value of i
gets compared to 100
.
System.out.println(i);
Auto-unboxing is happening in this statement as directly an object cannot be printed but a primitive value can be printed. So, unboxing occurred before the value of i
gets printed.
System.out.println(i + 10);
Auto-unboxing is taking place in this statement, as the value of an object cannot be used with +
operator like a primitive integer type value. Thus, i
gets auto-unboxed to a primitive type value and the value of i
gets modified by the +
operator.
From the following code, do the following :
(i) find its output.
(ii) find out the statements where autoboxing is taking place.
(iii) find out the statements where auto-unboxing is taking place.
Short age = Short.valueOf("35");
Integer salary = Integer.valueOf("2400");
Float height = Float.valueOf("1.78"); // in meters
Double weight = Double.valueof ("72.6");
double b = weight / (height * height);
System.out.println(age + "takes home" + salary);
System.out.println("bmi : " + b);
(i)
35takes home2400
bmi : 22.913774881799036
(ii) Autoboxing is not happening in this code as the valueOf() method returns the Wrapper class object. Thus, we are explicitly boxing the primitive type values to Wrapper objects in the following statements:
Short age = Short.valueOf("35");
Integer salary = Integer.valueOf("2400");
Float height = Float.valueOf("1.78"); // in meters
Double weight = Double.valueof ("72.6");
(iii) Auto-unboxing is taking place in the following statements:
double b = weight / (height * height);
weight
and height
are being auto-unboxed before mathematical operations can be performed on them.System.out.println(age + "takes home" + salary);
age
and salary
are being auto-unboxed before they can be printed as objects cannot be printed directly like primitive data type values.Which of these is a wrapper for data type int ?
Integer
Reason — Integer is a wrapper class for int data type.
Which of these is wrapper for simple data type char?
Character
Reason — Character is wrapper for simple data type char.
Which following method of wrapper Integer will convert the value of an object into int?
int intValue( )
Reason — int intValue( ) function returns the value of the invoking object as an int.
Which of the following is/are not valid wrapper classes?
integer, character
Reason — All the names of wrapper classes begin with capital letters. Thus, Integer, Float and Character are valid wrapper classes.
The Wrapper class objects' value is comparable to primitive type values. True/false ?
True
Reason — With Autoboxing/unboxing feature, we can use the wrapper class object in the same way as we use a primitive type data. Thus, the Wrapper class objects' value is comparable to primitive type values.
Write the return data type of the following functions :
Which of the following statements are true ?
The Integer class has a String- and an int-constructor.
The Integer has a floatValue( ) method.
The Double class has constructors for type double and float.
Reason — The Integer class has a String- and an int-constructor as we can create Integer objects by passing String and int type values at the time of object creation.
The Integer has a floatValue( ) method. The method float floatValue( )
returns the value of the invoking object as a float primitive type.
The Double class has constructors for type double and float as we can create Double objects by passing double and float type values at the time of object creation.
What is the output of this program?
class Output {
public static void main(String args[])
{
Integer i = new Integer(257);
byte x = i.byteValue();
System.out.print(x);
}
}
1
Reason — The byte datatype stores values in the range 0..255 and if we try to convert an integer value bigger than 255 using byteValue( ) method then, the byte data type will convert the value into the range of 0...255 in cyclic fashion. Thus, integer value 256
and 257
will be converted to byte value 0
and 1
respectively.
What is the output of this program ?
class Output
{
public static void main(String args[])
{
Integer i = new Integer(514);
float x = i.floatValue();
System.out.print(x);
}
}
514.0
Reason — The float data type stores decimal/floating point numbers. Thus, Integer object 514
will be converted and stored as float value 514.0
.