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(...

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 ...

.