• Post Reply Bookmark Topic Watch Topic
  • New Topic

== in strings  RSS feed

 
rajaraman navaneethan
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class strtest1
{

public static void main(String args[])
{
String s=new String("raja");
if(s=="raja")
{
System.out.println("1 is true");
}
if("raja"=="raja")
{
System.out.println("2 is true");
}

}
}

when u run the above code, result obtained is "2 is true".
when u create the string using String s="raja", the result is
"1 is true"
" 2 is true"

please explain how.

i have another doubt.how does == operator work when comparing
"raja"=="raja"
== compares only the references.how does this work?
how many string objects are created at the end of this code
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a *very, very, very* frequently asked question. Do a search for "String constant pool".
 
Divya Mehrotra
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please visit the following link :-

http://www.javaranch.com/journal/200409/Journal200409.jsp#a1
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It was a very good article.Thanks Divya.
Also big thanks Corey McGlone for a nice article.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!