• 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
 
Ranch Hand
Posts: 41
  • 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");
The above statement creates new refrence, so the condition <if(s=="raja")> returns false.But instead (s.equals("raja")) should return true.
The condition <s=="raja"> comparing the object s and string "raja", so it returns false.
The condition <if("raja"=="raja")> return true becuase here comparing two strings.
The operator == not only work on reference but also values.

Hope you understand.
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
crosspost
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey
Have you heard of something like a string pool.NO??
well if u dont exclusively use new String("raja") you are only acessing an object from the pool of pre-created strings. So you are always accessing the same one you created in the first place.
If you use the new operator then you actually create a new one.whch will have a different refrence value.
Cheers
keep the questions comming...
 
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi


acessing an object from the pool of pre-created strings. So you are always accessing the same one you created in the first place.

what happens when there is no pre created string available...


i heard that equal() method is generally overrided to change its meaning.

by default the equal which is not overrided will be behave same as the ==

can any one clear me out of this problem.i am confused over equal() and ==
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
equals() refers to the contents of the specified strings.
== questions as to whether the 2 references are pointing at the same object.
"Some string" == "somestring" caomp[ares 2 objects from the string poool(i think!!)
 
donal horgan
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Am I correct in thinking that for every new string object created you have another copy created in the string poool(assuming the string pool doesn't already have a copy of that object)
i.e. 2 string objects are created with new String("string");?
 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
any suggestion reg string pool details
 
donal horgan
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's a question on it a few questions down the main page -- posted recently-ish.
This contains links to pages on the string pool.
 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, just to add. first thing to remember
is that all class inherits the aquals() fro class Object.
The default behavoir is same as ==. ie compare object references
and return true if 2 ref refers to the object.
But, the issue is that the String and Wrapper classes have overridden
equals() that determine if the references are meaningfully equal, by checking the value.


Hope it helps
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
to print the first statement add the following line





this will print the first statement as well.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic