Question: Suppose a class have below overloaded function, and we call method1(null), which one will be called:
Scenario 1:
public void method1(Object o) { System.out.println("Object o"); } public void method1(String o) { System.out.println("String o"); }
Scenario 2:
public void method1(Object o) { System.out.println("Object o"); } public void method1(String o) { System.out.println("String o"); } public void method1(Double o) { System.out.println("Double o"); }
Answer:
For Scenario 1: method1(String o)
For Scenario 2: Compile Time Error
Analysis for the above answer:
Java will always try to use the most specific applicable version of a method that’s available.
Object, char[] and Integer can all take null as a valid value. Therefore all 3 version are applicable, so Java will have to find the most specific one.
Since Object is the super-type of char[], the array version is more specific than the Object-version. So if only those two methods exist, the char[] version will be chosen.
When both the char[] and Integer versions are available, then both of them are more specific than Object but none is more specific than the other, so Java can’t decide which one to call. In this case you’ll have to explicitly mention which one you want to call by casting the argument to the appropriate type.
Note that in practice this problem occurs far more seldom than one might think. The reason for this is that it only happens when you’re explicitly calling a method with null or with a variable of a rather un-specific type (such as Object).
Question: What is the output of the below code.
String s1="abc"; String s2=null; System.out.println(s1.compareTo(s2)); System.out.println(s2.compareTo(s1));
Answer:
NullPointerException
Question: How to write your own immutable class? If the same class has an Address field, how to make that to immutable as well ? How to do the same if a Collection object is in the class ?
Answer:
To create an immutable class in java, you have to do following steps.
1. Declare the class as final so it can’t be extended.
2. Make all fields private so that direct access is not allowed.
3. Don’t provide setter methods for variables
4. Make all mutable fields final so that it’s value can be assigned only once.
5. Initialize all the fields via a constructor performing deep copy.
6. Perform cloning of objects in the getter methods to return a copy rather than returning the actual object reference.
7. And if your method is returning a ArrayList type object then do the following:
public ArrayList<Address> getAddressList(){ return(ArrayList)Collections.unmodifiableCollection(addressList); }
Please have a reference link: https://codepumpkin.com/immutable-class-with-mutable-member-fields-in-java/
Question: What is the difference between ClassNotFoundException and NoClassDefFoundError ?
Answer:
ClassNotFoundException: First of all it is a checked Exception class and not an Error (I hope we know what is the difference between an Exception and Error).
It occurs when you use Class.forName(“”) or ClassLoader.loadClass(“”) to load a class.
It generally occurs when you miss to add some required library to the classpath.
NoClassDefFoundError: It is a fatal Error.
It occurs when JVM or Class Loader could not find the required class. This type of issue comes when the required class was available at compile time but was missing at runtime.
How to get this error:
Create a class A.java:
public class A{ public void do(){ System.out.println("Do somthing.."); } }
Export it as A.jar.
Add this jar to the classpath to B.java and import it:
import ?.?.A; public class B{ public static void main(String[] args) { A a=new A(); a.do(); //NoClassDefFoundError will be thrown here. } }
Export the whole project, make sure to exclude the A.jar while exporting.
You will get NoClassDefFoundError.