File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes The 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 "The " == " sign is confusing...." Watch "The " == " sign is confusing...." New topic

The " == " sign is confusing....

Ferdinand Mcelmarry

Joined: Oct 15, 2000
Posts: 3

Please read the foll..what will be the o/p..i am a little confused
class qq
public static void main(String as[])
if("String".toString() == "String")
System.out.println("Not Equal");

if(" String ".trim() == "String")
System.out.println("Not Equal");

if("String".replace('g','G') == "String".replace('g','G'))
System.out.println("Not Equal");
In the above examples ---
1) is printing "Equal"
2) In this case its printing "Not Equal"
3)In this case its printing "Not Equal"
The == sign checks if the 2 references r referring to the same object..right?
My question is
Where r the objects created and how does the examples actually work?
Does "String" has to do anything with it???
Will somebody explain..
Thanks ia advance

Sunitha Sounderrajan
Ranch Hand

Joined: Sep 12, 2000
Posts: 36
hello Preyas,
replace(oldchar, newchar)
--> returns a new string, generated by replacing every occurance of oldchar with newchar. If there is nothing to replace then the returned string is NOT a new string. it returns the same string. No new string is returned.
For Example-1.
if ("sample".replace('s', 'S') == "Sample")
System.out.println("No new Strings returned");
System.out.println("New Strings returned");
In Example-1 displays "New Strings returned".
if ("Sample".replace('s', 'S') == "Sample")
System.out.println("No new Strings returned");
System.out.println("New Strings returned");
In Example-2 displays "No new Strings returned".
In case of trim() the same rules holds good.
if (" Sample ".trim() == "Sample")
System.out.println("No new Strings");
System.out.println("New Strings");

--> displays "New Strings"
if ("Sample".trim() == "Sample")
System.out.println("No new Strings");
System.out.println("New Strings");
--> displays "No new Strings"

Hope this helps.
Sunitha. S

[This message has been edited by Sunitha Sounderrajan (edited October 21, 2000).]
Ranch Hand

Joined: Oct 20, 2000
Posts: 68
Let me explaint some fundas about "==" , equals()

You can use "==" only for primitive value comparision not for
Object comparision. If you still use it for object comparision
it will try to compare the contents of object references which
are nothing but addresses.
String str1=new String("Hello"); // str1 is an address
String str2=new String("Hello"); // str2 is another address
// i.e str1 != str2

String object is formed using 2 methods

(a) usual method using "new" operator ,

String str1=new String("Hello");

(b) Using String literals
String str2="Hello";

(Here String pool concept is used where even if you create
any number of Strings having same literals"Hello" all
objects will point to the same address, only one literal
"Hello" will be stored in memory loation. No duplication is allowed
inside a string pool. All strings using above method will be stored
only in string pools)

String str2="Hello";
String str3="Hello";
String str4="Hello";
String str5="Hello";

you run this code you will see the adresses are eual

if (str2==str3 && str4==str5)
System.out.println("Equal"); //will print "Equal"

"==" will not bother what those adress are refering to
simply it will say addresses (i.e object reference)

String str1=new String("Hello");
String str2="Hello";

if (str1==str2 )
System.out.println("Not Equal"); //will print "Not Equal"
// here addresses are different

So always use equals() method of Object class for comparing objects.
euals() method will not compare the addresses but compares the contents
stored in addresses pointed by the reference.
if (str1.equals(str2) )
System.out.println("Equal"); //will print "Equal"
System.out.println("Not Equal");

Now let me come to u r problem

(i) "String".toString() this simply returns string literal"String"
both addresses are same
It is equivalent to say that "String"=="String" which is
always Equal(true).

(ii)" String ".trim() == "String" // Not Equal, diffrent addresses

"String" it is created in string pool address is returned.
" String ".trim() - Since Strings are immutable a new String
object will be created which is not in string pool here different
address is returned

(iii) "String".replace('g','G') == "String".replace('g','G')
Again same "immutable" as above

Try the following:

if(" String ".trim().equals( "String"))
System.out.println("Equal"); // prints equal

if("String".replace('g','G') .equals( "String".replace('g','G')))
System.out.println("Equal"); // prints equal

I hope , not bored u too much

With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: The " == " sign is confusing....