Tuesday, September 12, 2006

Exception Handling

equals v/s ==

When you use == with a primitive (int, double, char, ...), you are checking whether the values are identical.When you use == with an object, you are checking whether the 2 objects are stored at the same address, in other wordswhether the two references are both pointing to the same object.

.equals() changes. Unless it is overridden by the object class, it is the same as ==. Some classes (like String) haveoverridden this method so that the class to create objects, they can check whether the contents are equivalent.it willcheck the contents of the object at the two addresses instead of just the addresses themselves.

public class EQUALSeqeq {
public static void main(String args[]) {
String s1=new String("5"); Long l1=new Long("5"); System.out.println(s1.equals(l1)); //displays false
}
}

The equlas( ) method is overridden in String class. But it compares only two String objects.If you are comparing a String object with any other object type, evne though they have same values, false will be returned.

The values in those classes are different. One is a String the other is a wrapper( long ), which is an integral type. How can 2 different types ever be equal.

Can anybody tell me what type of applications java can never be used for or shud not be used for?

Two reasons I can think of off the top of my head are 1) that Java (or rather, the current crop of JVMs) is not a real-time system, mostly due to the delays that can happen due to GC, and 2) that Java has not seen the kind of QA you'd want in a system on which lives depend. Anything that's highly platform dependant. Java is inherently multi-platform and you'll have great pains to tie your code to a specific platform.

For nuclear power plants, aircraft controls, etc, people tend to use programming which has a very small "footprint, ie fewer kB in the whole code than there are MB in Java, because (as Ulf Dittmer says), intensive testing is much easier on a small language.