i'm new to java and this is my first post and a silly one too plzz help me out


under what context should these both be used and how is it internally expressed or used by the compiler

== has many meanings

1. When comparing simple data types (int, char, double, bool, etc), == compares the value of the two.

int x = 1;
int y = 2-1;
System.out.println( x == y ); // **** [B]prints true[/B] ****

2. When comparing two object references, it compares the value of the two references (not the value of the objects!). For example,

Integer x = new Integer(15);
Integer y = new Integer(15);
System.out.println( x == y ); // **** [B]prints false[/B] ****

x refers to an Integer object and y refers to an Integer object. Are the two references the same (do they refer to the same memory location)? No. So it prints false.

3. To compare two objects for their values you must use equals method

Integer x = new Integer(15);
Integer y = new Integer(15);
System.out.println( x.equals(y) ); // **** [B]prints true[/B]****

4. There is one exception to this and its the String class.

String x = "15";
String y = "15";
System.out.println( x == y );  // **** [B]prints true[/B]****

Why is it true? Aren't x and y different references? NO! Java returns the same reference each time the "15" was created because Strings are immutable (can't be changed) and it is inefficient to maintain many copies of an immutable object.

To prevent Java from giving you the same reference use the new operator.

String x = new String("15");
String y = new String("15");
System.out.println( x == y );  // **** [B]prints false[/B]****

For more help, www.NeedProgrammingHelp.com

== in fact has only one meaning in Java. It compares the actual values of the left and right operands no matter what they are.
The equals method also has only one meaning. It compares the content of the class instance it is executed on with the content of the class instance it is passed as a parameter.

Where many beginners go wrong is that they forget that == is NOT synonymous to calling the equals method, with the result that they're getting unexpected results when comparing two references to object instances (usually String instances) that are at first glance identical but still yield a false result when compared using == because there are actually 2 distinct references in place.
They are then further confused because under specific conditions == CAN yield true when it seems you have 2 instances of certain classes (most specifically Strings), because in some instances you actually have only a single instance when you may think you have several (in this String is rather peculiar).

As a general guide:
NEVER use == to compare object instances unless you are quite certain that what you're trying to find out is whether the two instances are in fact the same instance (and not just 2 instances that have identical (according to the equals method) content.

As another general guide:
ALWAYS override the equals method (and as a consequence the hashCode method) whenever you have an class that you want to compare instance of with each other.

This article has been dead for over six months. Start a new discussion instead.