JavaGian java tutorial and java interview question and answer

JavaGian , Free Online Tutorials, JavaGian provides tutorials and interview questions of all technology like java tutorial, android, java frameworks, javascript, ajax, core java, sql, python, php, c language etc. for beginners and professionals.

Showing posts with label core java interview question answer. Show all posts
Showing posts with label core java interview question answer. Show all posts

BufferedReader vs Scanner in Java

4:27 AM
BufferedReader vs Scanner in Java Here is the 5 key differences between the Scanner and BufferedReader class of Java API: 1. Scanner is a much more powerful utility than BufferedReader. It can parse the user input and read int, short, byte, float, long and double apart from String. On the other hand  BufferedReader  can only  read String in Java . 2. BuffredReader has significantly large buffer (8KB) than Scanner (1KB), which means if you are reading long String from file, you should use BufferedReader but for short input and input other than String, you can use Scanner class. 3. BufferedReader is older than Scanner. It's present in Java from JDK 1.1 onward but Scanner is only introduced in JDK 1.5 release. 4. Scanner uses  regular expression  to read and parse text input. It can accept custom delimiter and parse text into primitive data type e.g. int, long, short, float or double using  nextInt() ,  nextLong() ,  nextShort() ,  nextFloat() ,  and  nextDouble()  methods, while 

Difference between final vs finally and finalize in Java?

4:26 AM
Difference between final vs finally vs finalize in Java The difference given in first paragraph about final, finally and finalize() method is sufficient from an interview point of view but it's better to know some more differences if you want to impress the interviewer or want to prepare better for follow-up questions. Let's see couple of more points to learn this concept better. 1) First and foremost, even though their name sounds similar, final, finally and finalize() are completely different from each other. There is no similarity in their function, whatever is, its only on their names. 2) The final keyword can be used with a class, method or variable in Java. A final class is not extensible in Java, a final method cannot be overridden, and a final variable cannot be changed. You can make a class Immutable in Java by using the final keyword. Similarly you can prevent overriding by using the final keyword with method and you can declare static final method to represen

Difference between early (static) binding vs late (dynamic) binding in Java

4:24 AM
In  order to understand the difference between static and dynamic binding in Java, it's important to first learn what is binding? Binding means the link between reference and actual code e.g. when you refer a variable it's bonded to the code where it is defined, similarly when you call a method, it's linked to the code where a method is defined. There are two types of method binding in Java, static binding and dynamic binding. When a method is called in Java it's bonded to the actual code either at compile time or runtime, when the program is actually started and objects are created. As the name suggest, static binding is more of static nature hence it occurs at compile time i.e. your code knows which method to call once you compiled your Java source file into a class file. Since it happens early in program's life cycle it is also known as early binding in Java. On the other hand, dynamic binding occurs at runtime, when JVM starts your program. This time which me

Difference between IdentityHashMap, WeakHashMap and EnumMap in Java

4:18 AM
IdentityHashMap vs WeakHashMap vs EnumMap in Java 1) The fundamental difference between IdentityHashMap and other Map implementations e.g. HashMap, Hashtable, WeakHashMap or EnumMap it uses equality operator (==) to search and get the value back. If you know how to get method of Map works the know that other Map implementation, which uses equals() method of the key object for that purpose. Since == operator only return true if the reference variables point to the same object, it's not possible to get the value with a different object even if it appear equal in all field. You must hold the reference of the key object outside the IdentityHashMap to get the value back. Another consequence of using == operator instead of equals() method is that there would be less collision compared to other Map implementations e.g. HsahMap. See Java Performance The Definitive Guide By Scott Oaks to learn more about the performance impact of collisions in HashMap. 2) The fundamental differenc

Difference between Iterator and ListIterator in Java?

4:15 AM
The Iterator is the standard way to traverse a collection in Java. You can use Iterator to traverse a List, Set, Map, Stack, Queue or any Collection, but you might not know that there is another way to traverse over List in Java? Yes, it's called the ListIterator. There are many differences between Iterator and ListIterator in Java, but the most significant of them is that Iterator only allows you to traverse in one direction i.e. forward, you have just got a next() method to get the next element, there is no previous() method to get the previous element. On the other hand, ListIterator allows you to traverse the list in both directions i.e. forward and backward. It has got both next() and previous() method to access the next and previous element from List. Unfortunately, ListIterator only supports the List interface, you cannot use it to traverse over Map or Set in Java. Iterator vs ListIterator You can differentiate between Iterator and ListIterator on following topics

Difference between StringBuffer, StringBuilder and String in Java

4:11 AM
String vs StringBuffer in Java 1) First and foremost difference between String and StringBuffer is that former is Immutable while later is mutable. This means if you need to manipulate String data then wrap it inside a StringBuffer to avoid creating lots of small and temporary String objects and putting pressure on Garbage collector. 2) Even though both StringBuffer and String are thread-safe, String achieves its thread-safety by using Immutability while StringBuffer achieves it by synchronizing methods which change the state e.g. append(), delete() etc. By the way, apart from above differences, both String and StringBuffer also has some key similarities e.g. both represent text data, both are defined in java.lang package, both classes exists in JDK from the first release and both are thread-safe in Java. Here is also a nice diagram to highlight the mutable and immutable difference between String and StringBuffer in Java: Difference between StringBuffer, StringBuilder

How to sort ArrayList ascending descending order

4:07 AM
How to sort ArrayList ascending descending order  Sorting ArrayList in Java is not difficult, by using Collections.sort() method you can sort ArrayList in ascending and descending order in Java. Collections.sort() method optionally accept a Comparator and if provided it uses Comparator's compare method to compare Objects stored in Collection to compare with each other, in case of no explicit Comparator, Comparable interface's compareTo() method is used to compare objects from each other. If object's stored in ArrayList doesn't implements Comparable than they can not be sorted using Collections.sort() method in Java. Sorting ArrayList in Java – Code Example ArrayList Sorting Example in Java - Ascending Descending Order Code Here is a complete code example of How to sort ArrayList in Java, In this Sorting we have used Comparable method of String for sorting String on there natural order, You can also use Comparator in place of Comparable to sort String on any o

difference and Similarities between Hashtable and HashMap in Java

4:02 AM
Similarities between Hashtable and HashMap in Java 1) Both Hashtable and HashMap implements java.util.Map interface. 2) Hashtable and HashMap both are a hash based collection and works on the principle of hashing. 3) Hashtable and HashMap both provide constant time performance for put and get method if objects are distributed uniformly across bucket. 4) From JDK 4 both Hashtable and HashMap are part of Java collection framework. Difference between Hashtable and HashMap in Java 1) First and most significantly different between Hashtable and HashMap are that HashMap is not thread-safe  while Hashtable is a thread-safe collection. 2) The second important difference between Hashtable and HashMap is performance since HashMap is not synchronized it perform better than Hashtable. 3) The third difference on Hashtable vs HashMap is that Hashtable is obsolete class and you should be using ConcurrentHashMap in place of Hashtable in Java.

Difference between HashSet and HashMap in Java

4:00 AM
Similarities on HashMap and HashSet in Java 1) Both HashMap and HashSet are a hash based collection in Java. 2) Both HashMap and HashSet are not synchronized and can not be shared between multiple threads. 3) Iterator returned by HashMap's keySet() and HashSet are fail-fast and they throw ConcurrentModificationException if they detect any structural change in Collection. 4) Both HashMap and HashSet provided constant time performance for basic operations like put(), get() etc. 5) Both HashSet and HashMap allows null values. Differences between HashSet and HashMap in Java 1) The first and most significant difference between HashMap and HashSet is that HashMap is an implementation of Map interface while HashSet is an implementation of Set interface, which means HashMap is a key value based data-structure and HashSet guarantees uniqueness by not allowing duplicates.In reality, HashSet is a wrapper around HashMap in Java, if you look at the code of add(E e) method

Difference between CopyOnWriteArrayList and ArrayList in Java.

2:50 AM
CopyOnWriteArrayList and ArrayList in Java.  we have seen What is CopyOnWriteArrayList in Java and How it achieves thread-safety by creating a separate copy of List for each writes operation. Now let's see Some difference between ArrayList and CopyOnWriteArrayList in Java, which is another implementation of List interface : 1) First and foremost difference between CopyOnWriteArrayList and ArrayList in Java is that CopyOnWriteArrayList is a thread-safe collection while ArrayList is not thread-safe and can not be used in the multi-threaded environment. 2) The second difference between ArrayList and CopyOnWriteArrayList is that Iterator of ArrayList is fail-fast and throw ConcurrentModificationException once detect any modification in List once iteration begins but Iterator of CopyOnWriteArrayList is fail-safe and doesn't throw ConcurrentModificationException. 3) The third difference between CopyOnWriteArrayList vs ArrayList is that Iterator of former doesn

What is CopyOnWriteArrayList or CopyOnWriteArrayList vs Array List in Java-Tutorial

2:48 AM
CopyOnWriteArrayList vs Array List in Java CopyOnWriteArrayList is a concurrent Collection class introduced in Java 5 Concurrency API along with its popular cousin ConcurrentHashMap in Java. CopyOnWriteArrayList implements List interface like ArrayList, Vector, and LinkedList but its a thread-safe collection and it achieves its thread-safety in a slightly different way than Vector or other thread-safe collection class. As the name suggests CopyOnWriteArrayList creates a copy of underlying ArrayList with every mutation operation e.g. add, remove, or when you set values. That's why it is only suitable for a small list of values which are read frequently but modified rarely e.g. a list of configurations. Normally CopyOnWriteArrayList is very expensive because it involves costly Array copy with every writes operation but it's very efficient if you have a List where Iteration outnumbers mutation e.g. you mostly need to iterate the ArrayList and don't modify it too often

Difference between HashMap and ConcurrentHashMap in Java Collection

2:14 AM
Difference between HashMap and ConcurrentHashMap in Java In this section, we will see some more details about HashMap and ConcurrentHashMap and compare them on various parameters like thread-safety, synchronization, performance, ease of use etc. 1)  The significant difference between HashMap and ConcurrentHashMap is that later is thread-safe and can be used in a concurrent environment without external synchronization. Though it doesn't provide the same level of synchronization as achieved by using Hashtable but it's enough for the most practical purpose. 2)You can make HashMap synchronized by wrapping it on Collections.synchornizedMap(HashMap) which will return a collection which is almost equivalent to Hashtable, where every modification operation on Map is locked on Map object while in case of ConcurrentHashMap, thread-safety is achieved by dividing the whole Map into different partition based upon Concurrency level and only locking particular portion instead of loc

Difference between a List and ArrayList Reference Variable in Java?

8:56 AM
Someone who is just starting with Java programming language often has doubt about how we are storing an ArrayList object in List variable, what is the difference between List and ArrayList? or why not just store the ArrayList object in ArrayList variable just like we do for String, int, and other data types. Well, the main difference between List and ArrayList is that List is an interface while ArrayList is a class. Most importantly, it implements the List interface, which also means that ArrayList is a subtype of List interface. In Java or any object oriented language, super type of variable can store an object of subtype. This is known as Polymorphism because any virtual method will be executed from subclass only, even though they were called from super type. This is the beginning, now let's see those two questions as well. Why store ArrayList object on List variable? You might have seen something like this: List<Movie> listOfMovies = new ArrayList<Movie>()

Explain about Difference between Callable and Runnable in Java

12:33 AM
Difference between Callable and Runnable interface in Java is one of the interesting questions from my list of Top 15 Java multi-threading questions, and it’s also very popular in various Java Interviews. The Callable interface is newer than Runnable interface and added on Java 5 release along with other major changes e.g. Generics, Enum, Static imports and variable argument method. Though both Callable and Runnable interface are designed to represent a task, which can be executed by any thread, there is some significant difference between them. In my opinion, the major difference between Callable and Runnable interface is that Callable can return the result of an operation performed inside call() method, which was one of the limitations with Runnable interface. Another significant difference between Runnable and Callable interface is the ability to throw checked exception. The Callable interface can throw checked exception because it's call method throws Exception. Commonly

How to join threads in Java? Thread.join() example

12:02 AM
You can join two threads in Java by using join() method from java.lang.Thread class. Why do you join threads?  because you want one thread to wait for another before starts processing. It's like a relay race where the second runner waits until the first runner comes and hand over the flag to him. Remember, unlike sleep(), join() is not a static method so you need an object of java.lang.Thread class to call this method. Now, who calls and who wants and which thread dies? for example, if you have two threads in your program main thread and T1 which you have created. Now if your main thread executes this line T1.join() (main thread execute whatever you have written in the main method) then main thread will wait for T1 thread to finish its execution. The immediate effect of this call would be that main thread will stop processing immediately and will not start until T1 thread has finished. So, what will happen if T1 is not yet started and there is no one to start T1 thread? Well, 

.