Java - Interview Questions and Answers on Primitive

Q1.  What are the Wrapper classes available for primitive types ?

Ans. boolean  - java.lang.Boolean
byte - java.lang.Byte
char - java.lang.Character
double - java.lang.Double
float - java.lang.Float
int - java.lang.Integer
long - java.lang.Long
short - java.lang.Short
void - java.lang.Void

Q2.  Difference between long.Class and Long.TYPE ?

Ans. They both represent the long primitive type. They are exactly the same.

Q3.  What are wrapper classes ?

Ans. They are wrappers to primitive data types. They allow us to access primitives as objects.

Q4.  What is casting?


Ans. 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

Q5.  Does Declaring an object "final" makes it immutable ?

Ans. Only declaring primitive types as final makes them immutable. Making objects final means that the object handler cannot be used to target some other object but the object is still mutable.

Q6.  Difference between boolean and Boolean ?

Ans. boolean is a primitive type whereas Boolean is a class.

Q7.  Explain Autoboxing ?

Ans. Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes

Q8.  What are Wrapper Classes ? What are Primitive Wrapper Classes ?

Ans. A wrapper class is any class which "wraps" or "encapsulates" the functionality of another class or component. A Wrapper Class that wraps or encapsulates the primitive data type is called Primitive Wrapper Class.

Q9.  What are the Disadvantages of using Collection Classes over Arrays ?

Ans. Collections can only hold objects, It can't hold primitive data types.

Collections have performance overheads as they deal with objects and offer dynamic memory expansion. This dynamic expansion could be a bigger overhead if the collection class needs consecutive memory location like Vectors.

Collections doesn't allow modification while traversal as it may lead to concurrentModificationException.

Q10.  Is it legal to initialize List like this ?

LinkedList<Integer> l=new LinkedList<int>(); 

Ans. No, Generic parameters cannot be primitives.

Q11.  Can we compare Integers by using equals() in Java ?

Ans. Yes for the Wrapper class Integer but not for the primitive int.