Here I have categorized core java interviews questions and answers for fresher and experience developer in Basics of core java interview questions, OOPs interview questions, String Handling interview questions, Multi-threading interview questions, collection interview questions, JDBC interview questions etc.
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.
Read More>>>
Popular Tutorials
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.
Read More>>>
This is very common question in core java interview for entry level programmer so let’s discuss some important point of difference as below:
Marker Interface in java is an interface with no fields or methods. It is used to instruct to the JVM for specific task or special behavior for those classes whose implanting marker interface. Marker interface is also called tag interface by some java developers.
Actually marker interface is nothing but it is a design pattern in computer science by using any languages we can implement ad that provide run-time type information about objects for specific behavior. It provides run time metadata to associate class and it actual implementation different from the initial implementation like generating proxies around classes. In java, it is used as interfaces with no method specified.
Read More>>>
Immutable class is a class which once created, it’s contents can not be changed. Immutable objects are the objects whose state can not be changed once constructed. e.g. String class. An immutable class is one whose state can not be changed once created. There are certain guidelines to create an class immutable.
Read More>>>
Major difference between yield and sleep in Java is that yield() method pauses the currently executing thread temporarily for giving a chance to the remaining waiting threads of the same priority to execute. If there is no waiting thread or all the waiting threads have a lower priority then the same thread will continue its execution. The yielded thread when it will get the chance for execution is decided by the thread scheduler whose behavior is vendor dependent. Yield method doesn’t guarantee that current thread will pause or stop but it guarantee that CPU will be relinquish by current Thread as a result of call to Thread.yield() method in java.
Read More>>>
The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs. But finally is useful for more than just exception handling — it allows having cleanup code accidentally bypassed by a return, continue, or break. Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated.
If the JVM exits while the try or catch code is being executed, then the finally block may not execute. Likewise, if the thread executing the try or catch code is interrupted or killed, the finally block may not execute even though the application as a whole continues.
Read More>>>
In java there are two Date classes one in different packages (util & sql), they have different implementation according to different requirements. A java.util.Date represents date and time of day but a java.sql.Date only represents a date. But sql package have one more class Time to calculate the time of day. The java.sql.Date is a subclass of java.util.Date and it is used with JDBC. It was intended to not have a time part, that is, hours, minutes, seconds, and milliseconds should be zero but this is not enforced by the class. So, what changed in java.sql.Date:
When we create String with new() it’s created in heap and also added into string pool, while String created using literal are created in String pool only which exists in Perm area of heap.
hashCode() and equals() methods have been defined in Object class which is parent class for java objects. For this reason, all java objects inherit a default implementation of these methods.
hashCode() method is used to get a unique integer for given object. This integer is used for determining the bucket location, when this object needs to be stored in some HashTable like data structure. By default, Object’s hashCode() method returns and integer representation of memory address where object is stored. equals() method, as name suggest, is used to simply verify the equality of two objects. Default implementation simply check the object references of two objects to verify their equality.
Read More>>>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…