This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String Question. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "String Question." Watch "String Question." New topic
Author

String Question.

RAGU KANNAN
Ranch Hand

Joined: Dec 16, 2005
Posts: 103
Hello Folks,

For the following question, why the line 1 and 2 are not correct. This is comparing string literal instead of Object. Pls explain to me.

Thanks, Raghu.K


class Test{
public static void main(String[] args){
int result = 0;
String s1 = "abc";
Object s2 = "abc";
StringBuffer s3=new StringBuffer("abc");
if(s1.equals(s2))//Line 1
result+=10;
if(s1==s2)//Line 2
result+=20;
if(s2.equals(s3))
result+=30;
System.out.println(result);
}
}
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2341
Both line 1 and 2 are true.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: String Question.
 
Similar Threads
Question on Strings and String pool
StringBuffer Question.
String object
how many objects are created?
Question on Garbage Collection