Tuesday, December 3, 2019

Core Java Interview Questions | Very important

1- Why main() in java is declared as public static void main?
Why public? In java public is access modifier and it using when we want to method from any where like any native library JNDI, etc. So that is reason main method is public, it can be accessible everywhere and to every object which may desire to use it for launching the application.

Why static? In java static is a keyword and it tells the compiler that particular entity belongs to a class and should be loaded once the JVM starts. So static things we could invoke without creating instance of that class. Lets suppose we do not have main method as static. Now, to call any method you need an instance of it. Now how to create instance of class which have main method and which one should be used and from where the parameters for overloaded constructors will come.

Why void? In void use before the method definition its mean this method is not returning any to the caller of method. But main method is invoked by JVM so there is no use of returning any value to JVM. The only thing application would like to communicate to invoking process normal or abnormal termination. This is already possible using System.exit(int). A non-zero value means abnormal termination otherwise everything was fine.

2- Does Java Pass by Reference or Pass by Value?
Java is pass by value and not pass by reference i.e. Java copies and passes the reference by value, not the object. Thus, method manipulation will alter the objects, since the references point to the original objects. But if you change the reference inside method, original reference will not get change. If it was pass by reference, then it would have got changed also. Well, primitive types are always pass by value without any confusion. But, the concept should be understood in context of method parameter of custom types.

Initializing a final variable :
We must initialize a final variable, otherwise compiler will throw compile-time error.A final variable can only be initialized once, either via an initializer or an assignment statement. There are three ways to initialize a final variable :

You can initialize a final variable when it is declared.This approach is the most common. A final variable is called blank final variable,if it is not initialized while declaration. Below are the two ways to initialize a blank final variable.
A blank final variable can be initialized inside instance-initializer block or inside constructor. If you have more than one constructor in your class then it must be initialized in all of them, otherwise compile time error will be thrown.
A blank final static variable can be initialized inside static block.

Hashmap Performance Improvements in Java 8

The Problem:
Until Java 7, java.util.Hashmap implementations always suffered with the problem of Hash Collision, i.e. when multiple hashCode() values end up in the same bucket, values are placed in a Linked List implementation, which reduces Hashmap performance from O(1) to O(n).

The Solution:
Improve the performance of java.util.HashMap under high hash-collision conditions by using balanced trees rather than linked lists to store map entries.This will improve collision performance for any key type that implements Comparable.

This JDK 8 change applies only toHashMap, LinkedHashMap, and ConcurrentHashMap.
The principal idea is that once the number of items in a hash bucket grows beyond a certain threshold (TREEIFY_THRESHOLD), that bucket will switch from using a linked list of entries to a balanced tree. In the case of high hash collisions, this will improve worst-case performance from O(n) to O(log n), and when they become too small (due to removal or resizing) they are converted back to Linked List.

static final int TREEIFY_THRESHOLD = 8;
static final int UNTREEIFY_THRESHOLD = 6;
Also note that in rare situations, this change could introduce a change to the iteration order of HashMap and HashSet. A particular iteration order is not specified for HashMap objects – any code that depends on iteration order should be fixed.

https://stackoverflow.com/questions/29740709/example-of-an-immutable-class-with-hashmap

https://www.geeksforgeeks.org/prevent-singleton-pattern-reflection-serialization-cloning/


https://www.javaguides.net/2018/11/java-8-interview-questions-and-answers.html


https://stackoverflow.com/questions/4055634/same-class-name-in-different-packages/29786782

http://java-performance.info/string-intern-in-java-6-7-8/

https://www.java67.com/2014/08/difference-between-string-literal-and-new-String-object-Java.html

No comments:

Post a Comment