File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Wats the O/P...

 
Shiaber Shaam
Ranch Hand
Posts: 252
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
{
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.....
 
Pavel Kubal
Ranch Hand
Posts: 360
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 252
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks man.. can u make it more clear...
 
wise owen
Ranch Hand
Posts: 2023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This thread.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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 ]
 
Shiaber Shaam
Ranch Hand
Posts: 252
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone explain this plz......................
 
krish bajaj
Ranch Hand
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 142
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks wise
now my concept is clear
thank you very much
 
Vladimir Scheglov
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
{
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
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic