• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String Question.

 
RAGU KANNAN
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both line 1 and 2 are true.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic