Study and learn Interview MCQ Questions and Answers on Java Type Wrapper Classes. Attend job interviews easily with these Multiple Choice Questions. You can print these Questions in default mode to conduct exams directly. You can download these MCQs in PDF format by Choosing Print Option first and Save as PDF option next using any Web Browser.
Go through Java Theory Notes on Type Wrappers before reading these objective questions.
Yes. Developers are encouraged to use primitive types like int, float, long etc for better memory management and performance.
Integer in = new Integer(9);
Integer in = Integer.valueOf(9);
Integer in = new Integer("9");
The method valueOf() is used to convert a primitive data type like "int" to "Integer". All constructors of Wrapper classes are deprecated as of Java 9.
Number class is the super class of almost all of Wrapper classes.
Except booleanValue() and charValue(), all valueOf() methods are from Number class.
The above constants are available only to non Boolean wrapper classes. MIN_EXPONENT and MAX_EXPONENT are available only to Float and Double.
System.out.println(Byte.BYTES); System.out.println(Character.BYTES); System.out.println(Short.BYTES); System.out.println(Integer.BYTES); System.out.println(Long.BYTES); System.out.println(Float.BYTES); System.out.println(Double.BYTES);
1 2 2 4 8 4 8
1 4 2 4 8 4 8
1 2 4 8 16 8 16
The sizes of Byte, Character, Short, Integer, Long, Float and Double are 1, 2, 2, 4, 8, 4 and 8 bytes respectively.
Float pi = Float.valueOf(2.54f);
Float pi = Float.valueOf(2.54);
Float pi = new Float(2.54f);
Float pi = new Float("2.54f");
If you are using a non string argument, suffix the float number with a "f". Otherwise, the compiler throws error. Passing a string to Float.valueOf("2.54") works fine.
Error: The method valueOf(String) in the type Float is not applicable for the arguments (double)
float p1 = 10.45f; float p2 = 5.2f; if(Float.compare(p1, p2) == 1) { System.out.println("10.45 > 5.2"); } else System.out.println("No Biscuits"); //OUTPUT 10.45 > 5.2
Float f1 = Float.valueOf("2.55"); Float f2 = Float.valueOf("4.5"); if(f1.compareTo(f2) == -1) { System.out.println("2.55 < 4.5"); } else System.out.println("No Tea"); //OUTPUT 2.55 < 4.5
float t1 = 2.0f; float t2 = 2.0f; if(Float.compare(t1, t2) == 0) { System.out.println("Float numbers are equal"); } //OUTPUT Float numbers are equal
The methods Float.compare(f1,f2) and Float1.compareTo(Float2) works in the same way for Integer numbers also.
Example:
public class WrapperTest3 { public static void main(String[] args) { Character ct = Character.valueOf('a'); char ch = ct.charValue(); System.out.println(ch); System.out.println("Uppercase = " + Character.toUpperCase(ch)); System.out.println("Is digit = " + Character.isDigit(ch)); System.out.println("is Letter = " + Character.isLetter(ch)); } } //OUTPUT a Uppercase = A Is digit = false is Letter = true
Example:
public class WrapperTest4 { public static void main(String[] args) { Boolean b1 = Boolean.valueOf(false); Boolean b2 = Boolean.FALSE; System.out.println(b1 == b2); System.out.println(b1.equals(b2)); System.out.println(Boolean.logicalAnd(true, false)); System.out.println(Boolean.logicalOr(true, false)); System.out.println(Boolean.logicalXor(true, false)); } } //OUTPUT true true false true true
Example with NumberFormatException:
public class WrapperTest5 { public static void main(String[] args) { Integer in = Integer.valueOf("0.0"); System.out.println(in.intValue()); } } OUTPUT: java.lang.NumberFormatException: For input string: "0.0"
Short sh = Short.valueOf(10); System.out.println(sh);
The static method valueOf(short) accepts a short number but not integer. So, use casting to convert to short.
Error: java.lang.Error: Unresolved compilation problem: The method valueOf(String) in the type Short is not applicable for the arguments (int) //So, use casting. Short sh = Short.valueOf((short)10);
Yes. You can not convert one type of Wrapper object to another type Wrapper.
Error: java.lang.Error: Unresolved compilation problem: Type mismatch: cannot convert from Byte to Integer