Wrapper Class In Java Interview Questions

Wrapper Class In Java Interview Questions: Java Wrapper Class provides a mechanism to convert primitive data types into objects and perform various operations on them. They also bridge the non-objective and objective worlds since collection classes like ArrayList or HashMap only work with objects.

Wrapper Class Interview Questions are an essential part of the Java interview process, aimed at assessing a candidate’s understanding of the wrapper classes in Java, including their purpose, various methods available, and their application in programming. This interview question evaluates a candidate’s knowledge and proficiency in wrapper classes to manipulate primitive data types in a Java program.

In this context, this article provides some commonly asked Wrapper Class Interview Questions that can help Java developers prepare for interviews and comprehensively understand wrapper classes in Java programming.

Wrapper Classes Interview Questions

What are the Wrapper Classes?
In Java, for every primitive type, the corresponding class is defined. Such classes are called Wrapper Classes.

Primitive TypesWrapper Classes
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean

What is the hierarchy of the Wrapper classes?
The following diagram depicts the hierarchy of the Wrapper Classes:

Why do we need Wrapper classes?
We need Wrapper Classes:

  • In order to represent the primitive type of data into its object type
  • For representing every String object into its equivalent primitive data type.

What is boxing?
It is the process of converting from a primitive type into its object type.
Example:

int i=10; //primitive type
Integer i1 = new Integer(10); //converted into its object type

What is unboxing?
It is the process of converting from object type to its equivalent primitive type.

Integer i2=new Integer(20); //object type
int i3=i2.intValue(); //primitive type

What is autoboxing?
“implicit conversion from a primitive type to object type is known as autoboxing”. (OR) From java1.5 onwards, the process of converting primitive type to object type is done by JVM internally it is known as “autoboxing”.

What is auto Unboxing?
The implicit conversion from a primitive type to an object type is known as autoboxing. (OR) Form JDK 1.5 onwards, the process of converting object type into its equivalent primitive type is done by the JVM internally it is known as “auto-unboxing”.

Note: Hence we can apply arithmetic operations directly on both primitive type and object types based on the type of need JVM will take care of it.

Is string a wrapper class?
String is a class, but not a wrapper class. Wrapper classes like (Integer) exist for each primitive type. They can be used to convert a primitive data value into an object, and vice versa.

Can a Byte object be cast to a double value?
No, an object cannot be cast to a primitive value.

What is casting?
There are two types of casting, casting between primitive numeric types and casting between object references. Casting between numeric types is used to convert larger values, such as double values, to smaller values, such as byte values. Casting between object references is used to refer to an object by a compatible class, interface, or array type reference.

What is Downcasting?
Downcasting is the casting from a general to a more specific type, i.e. casting down the hierarchy.

How many bits are used to represent Unicode, ASCII, UTF-16, and UTF-8 characters?
Unicode requires 16 bits and ASCII require 7 bits although the ASCII character set uses only 7 bits, it is usually represented as 8 bits. UTF-8 represents characters using 8, 16, and 18-bit patterns. UTF-16 uses 16-bit and larger bit patterns.

What is the range of the char type?
The range of the char type is 0 to 216 – 1 (i.e. 0 to 65535).

What is the range of the short type?
The range of the short type is -(215) to 215 – 1. (i.e. -32,768 to 32,767).

Name the eight primitive Java types.
The eight primitive types are a byte, char, short, int, long, float, double, and boolean.

What is the numeric promotion?
Numeric promotion is the conversion of a smaller numeric type to a larger numeric type, so that integer and floating-point operations may take place. In numerical promotion, byte, char, and short values are converted to int values. The int values are also converted to long values, if necessary. The long and float values are converted to double values, as required.

To what value is a variable of the boolean type automatically initialized?
The default value of the boolean type is false.

What are wrapper classes?
Java provides specialized classes corresponding to each of the primitive data types. These are called wrapper classes.
They are an example: Integer, Character, Double, etc.

Why do we need wrapper classes?
It is sometimes easier to deal with primitives as objects. Moreover, most of the collection classes store objects and not primitive data types. And also the wrapper classes provide many utility methods also. Because of these reasons we need wrapper classes. And since we create instances of these classes we can store them in any of the collection classes and pass them around as a collection. Also, we can pass them around as method parameters where a method expects an object.

Is String a primitive data type in Java?
No. String is not a primitive data type in Java, even though it is one of the most extensively used objects. Strings in Java are instances of String class defined in java.lang package.

Program for Wrapping and unwrapping?

package com.softwaretestingo.wrapperclasses;
public class WrappingUnwrapping 
{
	public static void main(String[] args) 
	{
		byte grade = 2;
		int marks = 50;
		float price = 8.6f;
		double rate = 50.5;
		Byte g1 = new Byte(grade); // wrapping
		Integer m1 = new Integer(marks);
		Float f1 = new Float(price);
		Double r1 = new Double(rate);

		// let us print the values from objects
		System.out.println("Values of Wrapper objects (printing as objects)");
		System.out.println("Byte object g1: " + g1);
		System.out.println("Integer object m1: " + m1);
		System.out.println("Float object f1: " + f1);
		System.out.println("Double object r1: " + r1);
		// objects to data types (retrieving data types from objects)

		byte bv = g1.byteValue(); // unwrapping
		int iv = m1.intValue();
		float fv = f1.floatValue();
		double dv = r1.doubleValue();

		// let us print the values from data types
		System.out.println("Unwrapped values (printing as data types)");
		System.out.println("byte value, bv: " + bv);
		System.out.println("int value, iv: " + iv);
		System.out.println("float value, fv: " + fv);
		System.out.println("double value, dv: " + dv);
	}
}

Output:

Values of Wrapper objects (printing as objects)
Byte object g1: 2
Integer object m1: 50
Float object f1: 8.6
Double object r1: 50.5
Unwrapped values (printing as data types)
byte value, bv: 2
int value, iv: 50
float value, fv: 8.6
double value, dv: 50.5

Compile and run the below code? Mention the reason?

package com.softwaretestingo.java.wrapperclasses;
public class WrapDemo 
{
	public static void main(String[] args) 
	{
		int i1=10;
		System.out.println("\t"+i1);
		Integer i2=new Integer(10);
		System.out.print("\t" +i2);
		if(i1==i2)
		{
			System.out.print("\t i1 and i2 are same");
		}
		else
		{
			System.out.print("\t i1 and i2 are not same");
		}
	}
}

Output:

	10
	10	 i1 and i2 are same

Reason: “i1 is a local variable of primitive type“, so in println() method there is an implementation to print the primitive types as a value directly. “i2 is a local variable of user-defined type “, in all wrapper classes they have overridden the toString() and hashCode() method in such way that it returns content present in it instead of address of i2. i1==i2 gives true because it compares the values of primitive types.

What is the output of the below code?

package com.softwaretestingo.java.wrapperclasses;
public class WrapDemo2 
{
	public static void main(String args[])
	{
		String s1="135";
		Integer i1=new Integer(s1);
		Integer i2=new Integer(s1);
		System.out.println(i1.hashCode());
		System.out.println(i2.hashCode());
		System.out.println(i1==i2);
	}
}

Output:

135
135
false

Reason: Even though i1 and i2 have the same values, we are creating them using new operator it will create different address every time.

What will be the result of compiling and executing the below the code?

package com.softwaretestingo.java.wrapperclasses;
public class WrapDemo2 
{
	public static void main(String args[])
	{
		String s1="123abc";
		Integer i1=new Integer(s1);
		System.out.println(i1);
		String s2="123";
		Integer i2=new Integer(s2);
		System.out.println(i2);
	}
}

Output:

Exception in thread "main" java.lang.NumberFormatException: For input string: "123abc"
	at java.lang.NumberFormatException.forInputString(Unknown Source)
	at java.lang.Integer.parseInt(Unknown Source)
	at java.lang.Integer.<init>(Unknown Source)
	at com.softwaretestingo.java.basics.Test.main(Test.java:8)

Result:

At line no 6: it is a Runtime exception of NumberFormatException type because it is not a valid integer value. Line no 10: It prints 123; because it is a valid integer.

What will be the result of compiling and executing the below the code?

package com.softwaretestingo.java.wrapperclasses;
public class WrapDemo3 
{
	public static void main(String args[])
	{
		String s2="130";
		Byte b1=new Byte(s2);
		System.out.println(b1);
	}
}

output:

Exception in thread "main" java.lang.NumberFormatException: Value out of range. Value:"130" Radix:10
	at java.lang.Byte.parseByte(Unknown Source)
	at java.lang.Byte.<init>(Unknown Source)
	at com.softwaretestingo.java.basics.Test.main(Test.java:8)

The result is: Runtime Exception of type OutOfRangeException, Bcoz byte range, is up to 127.

What will be the result of compiling and executing the below the code?

package com.softwaretestingo.java.wrapperclasses;
public class WrapDemo3 
{
	public static void main(String[] args) 
	{
		String s1="true";
		boolean b1=Boolean.parseBoolean(s1.trim());
		System.out.println(b1);
		String s2="false";
		boolean b2=Boolean.parseBoolean(s2.trim());
		System.out.println(b2);
		String s3="false or true";
		boolean b3=Boolean.parseBoolean(s3.trim());
		System.out.println(b3);
	}
}

Output:

true
false
false

(Reason is: boolean treats everything as false other than true)

Write a program to convert String from back to its data types using parseXXX() methods?
Parsing Operations example:

package com.softwaretestingo.java.wrapperclasses;
public class ParsingDemo 
{
	public static void main(String args[])
	{
		String price ="100"; // declare some strings
		String rate = "5.8f";
		String tax ="50.2";
		// performing parsing operations on strings
		int x = Integer.parseInt(price);
		float f1 = Float.parseFloat(rate);
		double y = Double.parseDouble(tax);
		System.out.println("\nPrinting data type values after parsing");
		System.out.println("int value: " + x);
		System.out.println("float value: " + f1);
		System.out.println("double value: " + y);
		//another style of converting strings into data types, very less used
		Integer i1 = new Integer(price);
		Float f2 = new Float(rate);
		Double d1 = new Double(tax);
		// extracting data types from wrapper objects
		int x1 = i1.intValue();
		float f3 = f2.floatValue();
		double d2= d1.doubleValue();
		System.out.println("\nPrinting data type values after conversion");
		System.out.println("int value: " + x1);
		System.out.println("float value: " + f3);
		System.out.println("double value: " + d2);
	}
}

Output:

Printing data type values after parsing
int value: 100
float value: 5.8
double value: 50.2

Printing data type values after conversion
int value: 100
float value: 5.8
double value: 50.2

Avatar for Softwaretestingo Editorial Board

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment