Java Interview Question
Categories: Java 8(JDK1.8)
What is the purpose of using javap?
The javap command disassembles a class file. The javap command displays information about the fields, constructors and methods present in a class file.
Syntax
javap fully_class_name
Can you access the private method from outside the class?
Yes, by changing the runtime behavior of a class if the class is not secured.
What are wrapper classes?
Wrapper classes are classes that allow primitive types to be accessed as objects. In other words, we can say that wrapper classes are built-in java classes which allow the conversion of objects to primitives and primitives to objects. The process of converting primitives to objects is called autoboxing, and the process of converting objects to primitives is called unboxing. There are eight wrapper classes present in java.lang package is given below.
What are autoboxing and unboxing? When does it occur?
The autoboxing is the process of converting primitive data type to the corresponding wrapper class object, eg., int to Integer. The unboxing is the process of converting wrapper class object to primitive data type. For eg., integer to int. Unboxing and autoboxing occur automatically in Java. However, we can externally convert one into another by using the methods like valueOf() or xxxValue().
It can occur whenever a wrapper class object is expected, and primitive data type is provided or vice versa.
a) Adding primitive types into Collection like ArrayList in Java.
b) Creating an instance of parameterized classes ,e.g., ThreadLocal which expect Type.
c) Java automatically converts primitive to object whenever one is required and another is provided in the method calling.
d) When a primitive type is assigned to an object type.
What is object cloning?
The object cloning is a way to create an exact copy of an object. The clone() method of the Object class is used to clone an object. The java.lang.Cloneable interface must be implemented by the class whose object clone we want to create. If we don't implement Cloneable interface, clone() method generates CloneNotSupportedException. The clone() method is defined in the Object class. The syntax of the clone() method is as follows:
protected Object clone() throws CloneNotSupportedException
What are the advantages and disadvantages of object cloning?
Advantage of Object Cloning
a) You don't need to write lengthy and repetitive codes. Just use an abstract class with a 4- or 5-line long clone() method.
b) It is the easiest and most efficient way of copying objects, especially if we are applying it to an already developed or an old project. Just define a parent class, implement Cloneable in it, provide the definition of the clone() method and the task will be done.
c) Clone() is the fastest way to copy the array.
Disadvantage of Object Cloning
a) To use the Object.clone() method, we have to change many syntaxes to our code, like implementing a Cloneable interface, defining the clone() method and handling CloneNotSupportedException, and finally, calling Object.clone(), etc.
b) We have to implement the Cloneable interface while it does not have any methods in it. We have to use it to tell the JVM that we can perform a clone() on our object.
c) Object.clone() is protected, so we have to provide our own clone() and indirectly call Object.clone() from it.
What is a native method?
A native method is a method that is implemented in a language other than Java. Natives methods are sometimes also referred to as foreign methods.
What is the purpose of the strictfp keyword?
Java strictfp keyword ensures that you will get the same result on every platform if you perform operations in the floating-point variable. The precision may differ from platform to platform that is why java programming language has provided the strictfp keyword so that you get the same result on every platform. So, now you have better control over the floating-point arithmetic.
What is the purpose of the System class?
The purpose of the System class is to provide access to system resources such as standard input and output. It cannot be instantiated. Facilities provided by System class are given below.
a) Standard input
b) Error output streams
c) Standard output
d) utility method to copy the portion of an array
e) utilities to load files and libraries
Which containers use a border layout as their default layout?
The Window, Frame and Dialog classes use a border layout as their default layout.