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 String Question. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String Question." Watch "String Question." New topic

String Question.

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
if(s1==s2)//Line 2
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2398
Both line 1 and 2 are true.
I agree. Here's the link:
subject: String Question.
jQuery in Action, 3rd edition