Thursday, October 30, 2008

equals() & hashcode() relation in Java

I assume that you have an idea about equals() and hashCode() methods present in Object class. What you may not know, is that a relation(or perhaps a contract) exists between them.

Remember the following points:
  • If you override equals(), then you must override hashCode()
  • equals() & hashCode() must be evaluated based on same fields
  • If two objects are equal using equals() then they must have same hashCode() value, but vice-versa need not be true.
Since hashCode() value of an object determines how it will be stored and located when it is used with collections like HashMap & HashSet, so it becomes necessary that equal objects must have same hashCode() value. 
So, implementing hashCode() when your program deals with collections become really important for reasons such as efficiency and correctness.

To give an analogy to this hashing process, imagine a sequence of buckets to be your hashtable. Now, to retrieve an element, we do
1. Locate the right bucket using hashCode() value
2. Search the bucket for the element using equals()

Now, if two equals objects (which would be present in one bucket) have different hashcodes, you would never be able to retrieve them back correctly, because you are not looking in the right bucket. 

Although, it is legal to have same hashCode() value for different (read unequal) objects, but it will hurt the efficiency as it would make it a bit slow to locate the correct bucket.

Also, if two objects have different hashCode() values, then they must not be equal using equals() i.e.
if x.hashCode() != y.hashCode() then x.equals(y) == false.

This also helps in clearing a popular misconception about hashcodes that they identify an element uniquely. They can be used as an object ID but they are not necessary unique.

Still interested in more details, read this article to get more insight. :-)

Monday, September 8, 2008

Google's Chrome - A Review

If you are here or if you are an aware IT professional/student, then there is no way that you haven't heard of Google's Chrome

For those, who still do not know what Chrome is, it is an open-source web browser that has been built from scratch by Google. Another feather in their cap, this browser promises a lot in its beta version that has been released for Windows Vista/XP SP 2 so far. The Mac and Linux versions of Chrome are under development and will be released soon.

Chrome offers you a world of rich features to play with. The major features are
  • OmniBar - The URL box in Chrome is called OmniBox as it shows you pages based on your web history, your bookmarks, popular searches, results of Google Suggest as you type words in the URL box. OmniBox can also be used to add a bookmark quickly. Thus, there is just "one box for Everything". Can be annoying sometimes, but if you do not delete your browsing history often, it tends to settle down the dust and offers you exactly what you want.
  • Incognito Mode - By default, Chrome saves all your web history on the local system and uses it for several purposes. The web history could be seen later also by anyone using your system. So, if you do not want your browser to keep a record of what you are surfing, then you can open a new window in Incognito Mode and the browser would not save any cookie related to that page. Looks useful, but there should be an option to configure Chrome to change this default behaviour of saving web history because some users might just not want to save all work they do because they work on systems that many people share.
  • Crash Control - Google claims Chrome to be more stable and safer than a lot others as a crash that occurs due to one tab would not effect other tabs of the same window. Traditionally, a browser creates a new thread when a user creates a new tab in a window. Chrome differs from the ordinary by creating a new process per tab. This means that separate data structures, more memory usage for each tab you open but that also means no dependence between various processes (or tabs). Google claims that it won't affect speed as much as we think it will because Chrome is based on WebKit, an open source rendering engine which uses memory efficiently. My experience with Chrome begs to differ with what Google has to offer. On my system, Chrome has CRASHED twice within a week of its usage and the fact that I was not running heavy web applications at that time is really scary. Also, I missed the restore session option that I have with Firefox.
  • Speedy - It is definitely faster than its counterparts like FF and IE. Firstly, it does not takes much time to open up and also the browsing experience for users with a slow net connection(like me) has improved with Chrome. Thanks to the new Javascript Engine V8, that Google has developed for the browser.
  • Chrome Task Manager - I do not know if such a facility exists with Firefox 3 or IE 8 beta version but it surely helps you to understand which tab is going "sad". It really treats each tab and plugins within a window as a separate process and shows the amount of memory used by each one of them.

Several things that don't work for me includes crash control and inability to scroll up when i use the scroller of my laptop. However, scrolling down works very fine. It gets a bit frustating not being able to scroll up and using the keyboard to achieve the effect.

The best thing that I like about Chrome is the comic book that they have released. The comic book proves the saying that "A picture is worth a thousand words". Its not only simple and easy to gather concepts but also entertaining, even for a layman like me, who is unaware of functioning of a web browser.

One thing that I fail to understand about Google chrome is that inspite of being Open Source, it is released for Windows first(something that can never be open source) and the Mac & Linux versions are still in the making...

It would have been definitely better, had Google released it for all OS's at the same time.
Perhaps the product was such that, Google could not hold it any longer after working so hard to develop it. They were desperate to release it and see the feedback and wild reactions. Still they wil miss out the feedback from the real open source community, which i believe do not use much of windows :-)

But one thing is for sure, Chrome is here to stay, no matter how many times it crashes....I won't stop using it :-)
And if you still haven't tried it yet, go here and download it right now. Happy Chroming !!

Wednesday, August 27, 2008

Tech Talks @ DU

There's a lot happening at Department of Computer Science, University of Delhi, when you talk about interesting tech-seminars taking place.
Recently, we had a talk on "Open Source : How we already use it" by Aadhar Mittal, our new Sun Campus Ambassador. It was an introduction to Open Source, much needed for the new batch. He started by giving some insights on Open Source and gave examples from daily life that how we already make use of so much Open Source Software(OSS) without knowing about it. He talked about advantages of OSS and told how big companies like Sun mint money from their OSS. It was an eye opener for the first years since it was their first talk. I'm sure he's going to blog about it pretty soon and put up some nice pics....:-)

Now, the big one. Today, we also had a great lecture by Prof. Christelle Schraff, Pace University, New York on "Towards 'More' Correct Software". She talked about various phases of SDLC giving emphasis on testing. She also gave a short demo on JUnit (she used it in Eclipse) but the same can be done with NetBeans too. She also talked about Java Assertions and showed us the Rational Functional Testing Tool but it was too slow on her machine. She talked in brief about formal methods like theorem proving and about some of her open source projects. It was definitely an interesting seminar with the students getting lucky enough to get a chance to meet and interact with professors of her status.

Sunday, August 17, 2008

Java Interview Questions

Based on my exposure and experience with the language, I have listed some important Java(Core) questions that I think can be asked in any interview related to Java. I'm not listing the answers with them over here, but that does not mean that I do not know them :))
The only reason for not writing answers here, is that it would unnecessarily increase the length of the entry, but if you have doubts in any question, you can ask me in the comments section or can mail me.

Questions

Ques. 1 What makes Java Portable?
(The answer can spark discussion of things like Bytecode, VM so be clear with your concepts)

Ques. 2 List some features of Java that are not present in C++
(List 5-6 main features that makes the language more powerful)

Ques. 3 Is Java Slow as compared to C/C++?
(Google over it if you do not know or ask me)

Ques. 4 Differentiate between JDK. JRE and JVM.
(Ok, read this link and you are through) :-)

Ques. 5 How are interfaces different from Abstract Classes.
(Pretty simple one)

Ques. 6 List major OOPS principles and tell how each can be implemented using Java.
(simple & still Frequently asked)

Ques. 7 Parameter passing in Java.
(may be this can help.......it can be indirectly asked by giving a piece of code)

Ques. 8 How many classes can you have in 1 .java file?? How many of them can be public ? Why so ?
(if you do not know this, its high time you clear your basic concepts)

Ques. 9 Is Java a compiled language or interpreted language ??
(Ans: It is both compiled & interpreted. You need to explain in detail the conversion(compilation) of .java file into .class file(s) using javac and then execution(interpretation) of the .class file by the JVM)

Ques. 10 Talk about Garbage Collection in Java.
(very frequently asked)

Ques. 11 Can we force Garbage collector to run in Java ??
(ans is no, we can only give it a hint, not a command.... Google to know more)

Ques. 12 Is Java purely object-oriented ??
(perhaps this would help)

Ques. 13 Can we call C++ code in Java ??
(can be done using Java Native Interface (JNI), if you need to know more, Google is your best friend)

Ques. 14 Tell 3 uses of final keyword.
(typically textbook based question)

Ques. 15 Why an abstract class cannot be final ?
(don't tell me you don't know this :p)

Ques. 16 Which version of Java have you worked on ? What are the different versions of Java ? How they differ from each other ??
(Tough one if you don't know it. At least remember 2 or 3 main points of differences between Java 4, 5 & 6 such as with Java 5 new features such as Auto-boxing, variable arguments, co-variant returns (in overriding) but you also need to know what they mean. Explaining them here would require another blog post)

Ques. 17 Tell how many different types of variables are possible in Java.
(Tricky! Talk about Local Variables, Instance Variables & Class Variables)

Ques. 18 What are immutable objects? Give an example of immutable object in Java.
(Hint: Eg is String objects are immutable...rest you have to find it your way)

Ques. 19 What is Serialization ? How it can be implemented in Java.
(If you can answer this in an interview, they would seriously consider you as not many ppl can answer this so read about it & talk about the concept first and then talk abt Serializable interface)

Ques. 20 Discussion restrictions placed on method overloading & method overriding and compare the two.
(another textbook based.......but consult a good textbook :p)

Will keep on posting new questions that I feel every Java developer (rather every aspiring student) must know, whether he wants to clear his interview or not !!

Waiting for Comments :-)