Skip to main content

Posts

Showing posts from November, 2011

Hidden facts about HashMap and HashSet

We all know that hashcode is used when you store objects in map or set. And we all know that the hashcode() method of the object is used to uniquely identify the object stored in the set and that HashSet will not store duplicate values. What do we derive from these? Shall we say, "Objects having same hashcode are considered as same"?! Guess what is the output of the following program. public class HashCodeTest { static class Person { int hashCode = 1; @Override public int hashCode() { return hashCode; } } public static void main(String[] args) { Set persons = new HashSet (); persons.add(new Person()); persons.add(new Person()); persons.add(new Person()); persons.add(new Person()); System.out.println("Set size: " + persons.size()); Map map = new HashMap (); map.put(new Person(), 1); map.put(new Person(), 2); map.put(new Person(), 3); map.put(new Person(), 4); System.out.pr...

Basic Java Interview Questions

Show answers Hide answers What is the difference between Java and C++? Java C++ Memory managed (or garbage collected) language Programmer has to manage the heap memory. Compiled java code is platform independent C++ binary code is platform specific. Compiled and Interpreted language Compiled language Pure object oriented Functions can be disassociated from any class Supports multilevel inheritance Supports multiple and multilevel inheritance Supports function overloading Supports function and operator overloading Supports only signed numeric data types Supports both signed and unsigned data types Supports unicode characters and char type takes 2 bytes in memory Doesn't support unicode characters by default Is Java a compiled language or an interpreted language? Java is both compiled (to byte code) and interpreted language. What is byte code? Source java file is compiled into a intermediate code before interpreted by JVM. This intermediate code is called byt...