What is the difference between HashMap and Hashtable class.
Hashtable | HashMap |
---|---|
Hashtable class is synchronize. | HastMap is not synchronize. |
Because of Thread-safe, Hashtable is slower than HashMap | HashMap works faster. |
Neither key nor values can be null | Both key and values can be null |
Order of table remain constant over time. | does not guarantee that order of map remain constant over time. |
Q2. How to remove duplicate element from a ArrayList.
At first this question will look quite difficult but the answer is very simple. By converting the ArrayList intoHashSet. When you convert a ArrayList to HashSet all duplicates elements will be removed but insertion order of the element will be lost.Q3. How to create a unmodifiable/read-only list in Java?
Collections.unModifiableList() method is used to create unmodifiable/read-only list in Java. Pass the listas a argument to this method.Q4. What is the difference between ArrayList and Vector class.
Vector | ArrayList |
---|---|
Vector class is synchronize. | ArrayList is not synchronize. |
Because of Thread-safe, Vector is slower than ArrayList | ArrayList works faster. |
Enumeration is used to iterate through the element of Vector. | Iterator is used to iterate through the element of ArrayList. |
Q5. Why String is immutable in Java?
In Java, strings are object. There are many different ways to create a String object. One of them is using string literals.Each time a string literal is created, JVM checks the string constant pool first. If string already exist in the pool, a reference to the pool instance is returned. Sometime it is possible that one string literals is referenced by many reference variable. So if any of them change the string, other will also get affected. This is the prominent reason why string object is immutable.
Q6. Difference between StringBuffer and StringBuilder?
Both StringBuffer and StringBuilder classes are almost same except for two major differences.StringBuffer | StringBuilder |
---|---|
StringBuffer is synchronize. | StringBuoder is not synchronize. |
Because of synchronisation, StringBuffer operation is slower than StringBuilder. | StringBuilder operates faster. |
Q7. Difference between equals() method and == operator?
Main difference between '==' and equals() in Java is that '==' is a operator used to check whether two different reference refers to same instance and equals() is a method used to check equality of an object. Another important difference is '==' operators is used more with primitive data type while equals() method is used for object.See example of '==' operator and equals() method for more detail.
Q8. What are the different ways to create a thread in java?
There are two different way to create a thread in java.1. Extending Thread class.
2. By implementing Runnable interface.
Q9. What are the difference between sleep() and wait() method.
wait() method in java should be called from synchronized block while there is no such requirement forsleep() method. Another difference is sleep() method is a static method, while wait() is an instance specific method called on thread object. In case of sleep(), sleeping thread immediately goes toRunnable state after waking up while in case of wait(), waiting thread first acquires the lock and then goes into Runnable state. notify() and notifyAll of Object class are used to awake a waiting Thread while sleeping thread can not be awaken by calling notify() method. wait() method is defined in Object class while sleep() is defined in Thread class.Q10. What is serialization in java?
Serialization is a process of converting object into a sequences of byte which can be written to disk or database or sent over network to any other running JVM. The reverse process of creating object from sequences of byte is called Deserialization.See more detail on
Q11. What is covariant return type?
After Java 5, it is possible to override a method by changing its return type. If subclass override any method by changing the return type of superclass method, then the return type of overriden method must be subtype of return type declare in original method in the superclass. This is the only way in which method can be overriden by changing its return type.Q12. How to find if JVM is 32 or 64 bit from Java program.
By using System.getProperty() method, you can find JVM size from java program. Although the size of JVM hardly matters because we know that java is platform independent i.e write once and run everywhere but in some situation it is required to know the size of JVM like if you are using native library. Example of how to get size of JVM.Q13. How to change file permission in Java?
setReadable(), setWritable(), setExecutable() method is used to change or set file permission in java.See
Q14. How you can force the garbage collection?
Garbage collection automatic process and can't be forced. You could request it by calling System.gc(). JVM does not guarantee that GC will be started immediately. Garbage collection is one of the most important feature of Java, Garbage collection is also called automatic memory management as JVMautomatically removes the unused variables/objects (value is null) from the memory. User program can't directly free the object from memory, instead it is the job of the garbage collector to automatically free the objects that are no longer referenced by a program. Every class inherits finalize() method from java.lang.Object, the finalize() method is called by garbage collector when it determines no more references to the object exists. In Java, it is good idea to explicitly assign null into a variable when no more in use. I Java on calling System.gc() and Runtime.gc(), JVM tries to recycle the unused objects, but there is no guarantee when all the objects will garbage collected.Java
Java Tutorial
Core Java
Java interview questions