wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes == AND EQUALS Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "== AND EQUALS" Watch "== AND EQUALS" New topic
Author

== AND EQUALS

AVNI R
Greenhorn

Joined: Oct 30, 2000
Posts: 5
hi everybody,
I have a silly question, when do we use == and equals. Its kind of confusing at times. I am confused that if yu want to compare members(methods and variables), which one yu use. and for object reference which one to use.......... I am new to java, I dont know if I sound too bad....
Thanks in advance.......
avni
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
The operator == does what is known as reference comparison. It compares the memory address where the two entities being compared reside and then returns true( or false ) based on the comparison. If two references returns true when compared with == it means both the references are pointing to the same object in memory.
When you use == with primitives instead of references, it is nothing but equality comparison. ie., 4==5 is false and 3==3 is true.
Unlike the == which is an operator, <code>equals()</code> is a method defined on the <code>Object</code> class. Though the "original" version defined in the <code>Object</code> class does nothing but reference comparison, this method is available for subclasses of <code>Object</code> for overriding. The subclasses can chose to provide more meaningful, content-based comparison. For example, many wrapper classes such as Math, Double, Float etc override the equals method to actually compare the primitive value that they hold.
For the classes that do not override the <code>equals()</code> method, it behaves in a similar way as the method defined on the object ie., returns the result of basic reference comparison.
I would recommend you to write some code that makes use of both == and equals on various java language classes as well as your own classes. You can also search the previous discussions on this topic here at JavaRanch.
Hope that helps!
Ajith
[This message has been edited by Ajith Kallambella (edited October 30, 2000).]


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
AVNI R
Greenhorn

Joined: Oct 30, 2000
Posts: 5
Thanks Ajit......
I agree with it, and understand that all primitive datatypes can be compared using == and object references should be compared using equals.
I have one more question.......Where does string belong to. I understand that it is a literal(constant), and we also have a string class.. But from the book of moughal it says that yu can compare a string literal to a string object but not with ==
Here is an example

Ajith. I have two doubts here.......
1. Where does a literal(any literal-int,boolean,string etc)belong to.. Is it belonging to primitive datatypes? If yes then why do we have literals in the first place.... Can't we do without it..
2. In this example why cant we compare a string literal to an object reference in (1)? Why is it giving false... and why it is giving true at (2)...........
Mysterious strings.........

AVNI
Gautam Pandya
Greenhorn

Joined: Sep 29, 2000
Posts: 17
Avani,
String literals are also instances of String class. In your question all the three String variables, s1, s2 and s3 are object references and they are not different. (from part 2 of your question I felt that you consider them to be different)
The question now you may have - why s1 == s2 is true?
That is because when s1 is assigned with a literal "QSQT", a new instance of String (to store "QSQT") is created to represent the literal which will reside in a pool (of Strings). Reference to this instance is assigned to the string variable s1.
When s2 is assigned with "QSQT", the compiler does not create a new instance of String. It uses the same instance and reference to this instance is assigned to s2. s1 has the same reference and hence s1 == s2 is true.
in case of s3, there is an explicit instantiation of String object in the assignment, and hence the s3 has reference to a different String object but having the same value "QSQT".
Which means s2 == s3 is false and s2.equals(s3) is true.
Originally posted by AVNI R:
[B]Thanks Ajit......

Ajith. I have two doubts here.......
1. Where does a literal(any literal-int,boolean,string etc)belong to.. Is it belonging to primitive datatypes? If yes then why do we have literals in the first place.... Can't we do without it..
2. In this example why cant we compare a string literal to an object reference in (1)? Why is it giving false... and why it is giving true at (2)...........
Mysterious strings.........

AVNI
[/B]
</BLOCKQUOTE>
[This message has been edited by Gautam Pandya (edited October 30, 2000).]
[This message has been edited by Gautam Pandya (edited October 30, 2000).]
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

its kind of a trick question. for objects you should use equals(). but strings are an exception where == works if both strings are assigned the same literal. I read it is a compiler optimization thing.


SCJP
Visit my download page
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Gautam is right. Strings are not primitives and they are first-class objects whether you use new() or the = operator to instantiate them.
Randall - The string pool concept is quite important for the SCJP exam. Make sure you understand how the == and equals()behaves with various different kinds of strings objects. Write a lot of code!!!
Ajith
PS ' AVNI '- PROPER NAMES ARE NOW REQUIRED!!
Read this post for more details.
Javaranch appreciates your cooperation to comply with the official naming policy.
Ajith
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: == AND EQUALS
 
Similar Threads
equals and compareTo method difference?
How do I prepare my mind?
rmic tool needed?
JSF & Structs
Passed SCEA part 2 and 3 with 95 % ����