This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Wats the O/P... 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 "Wats the O/P..." Watch "Wats the O/P..." New topic
Author

Wats the O/P...

Shiaber Shaam
Ranch Hand

Joined: Jun 16, 2006
Posts: 252
{
String s = new String( "Computer" );
if( s == "Computer" )
System.out.println( "Equal A" );
if( s.equals( "Computer" ) )
System.out.println( "Equal B" );
if( "Computer" == s )
System.out.println( "Equal A" );
}
//Explanation Needed....
//Especially for II and III If and their differences.....


$Hi/\bEr
Pavel Kubal
Ranch Hand

Joined: Mar 13, 2004
Posts: 356
It prints equal B. In the first and third case you're comparing references. Third if is only switched first if. The second if is comparing objects if they are meaningfuly equivalent.
Shiaber Shaam
Ranch Hand

Joined: Jun 16, 2006
Posts: 252
thanks man.. can u make it more clear...
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
This thread.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
"Shiaber",
please read our JavaRanch Naming Policy and change your displayed name accordingly. We need two names in the form <firstname><space><familyname>, preferably your real name. Accounts with noncompliant displayed names are locked out.

Thanks
-Barry
[ June 19, 2006: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Shiaber Shaam
Ranch Hand

Joined: Jun 16, 2006
Posts: 252
Can anyone explain this plz......................
krish bajaj
Ranch Hand

Joined: Jun 19, 2006
Posts: 31
hey shiaber
i am also confused with (==)and equals in strings .if u will get the answer
please help me out
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
The equals() method compares the characters that make up String objects. It performs this comparison by putting the characters of the String objects into two char arrays and comparing the two arrays. The method returns true if all the elements of the char arrays match and false otherwise.

The == operator compares two object references to see whether they refer to the same instance.
krish bajaj
Ranch Hand

Joined: Jun 19, 2006
Posts: 31
ok wise i understand the concept of equals but please (==) make it more clear like

string atr1="hello"
string atr2="hello"
string atr3="himani";
atr1==atr2;
atr2==atr3;
atr1.equals(atr2)//true
tell me how == works

thanks for equals
S Thiyanesh
Ranch Hand

Joined: Mar 19, 2006
Posts: 142
Any of the String object created with new String() will return true with == only when the two references point to the same object.
But any String created with the assignment operator String str = "Hello";
will return true with any other String with the same value in similar style.
This works accross the class,package also.
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
String atr1=new String("hello"); //Create a String Object on heap
String atr2=new String("hello"); //Create a String Object on heap
String atr3=atr1; //Assign atr3 refers the same String Object as atr1

System.out.println(atr1 == atr2); // false.
System.out.println(atr1 == art3); // true
krish bajaj
Ranch Hand

Joined: Jun 19, 2006
Posts: 31
thanks wise
now my concept is clear
thank you very much
Vladimir Scheglov
Greenhorn

Joined: Jun 19, 2006
Posts: 20
{
String s = "Computer"; //in String pool
if( s == "Computer" ) // compare same object
System.out.println( "Equal A" );
if( s.equals( "Computer" ) ) // compare same object
System.out.println( "Equal B" );
if( "Computer" == s ) // compare same object
System.out.println( "Equal A" );
}

It prints:
Equal A
Equal B
Equal C

--------------------
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

"Shiaber"

You have been repeatedly warned about following the JavaRanch Naming Policy of using your real first and real last names.

This is your last warning, if you do not comply your account will be closed.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
GeeCON Prague 2014
 
subject: Wats the O/P...