• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

== in strings

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a *very, very, very* frequently asked question. Do a search for "String constant pool".
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please visit the following link :-

http://www.javaranch.com/journal/200409/Journal200409.jsp#a1
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It was a very good article.Thanks Divya.
Also big thanks Corey McGlone for a nice article.
 
reply
    Bookmark Topic Watch Topic
  • New Topic