• 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

String comparision

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,

Please make my doubt clear as

String str1 = new String("java");

String str2 = new String("java");

If str1 and str2 are same?

and if there is a person class

Person p1= new Person("A");

Person p2 = new Person("A");

sp both scenarios are equal?

Are these two newly created objects or it will refer from string pool, I know abt other operation for string comparision for literal pool but for this

Confused?
Please reply,

Regards,
Ujwwala


 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are a couple of things you need to be aware of and keep straight.

first: str1, str2, p1 and p2 are REFERENCES to objects, not objects themselves. they are like address cards, whereas the objects themselves are like houses.

So, when you ask if "str1 and str2 are the same", do you mean "do they have the same address written on them" or do you mean, "Are the houses they refer to equivilent?" Obviously, if both have the same address then the houses are equivalent...but even if they have different addresses it is possible the houses are both worth the same amount.

Second: String literals are stored in the String Pool, as you know. However, when you say "new String(<whatever>)", that does create a String object that is NOT in the pool. From your example, "java" would be in the string pool, however, two new string are also created by the 'new' operator. So, str1 and str2 will point to different objects that are functionally equivalent. str1 == str2 is false, but str1.equals(str2) is true.

Finally, we don't know enough about the Person class to say. p1 and p2 will both point to different objects - that should be clear - so p1 == p2 is false. But without knowing how (or if) the equals() method is defined, we can't say much about p1.equals(p2).
 
ujwwala tem
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for clarifying,

Regards,
Ujwwala
 
reply
    Bookmark Topic Watch Topic
  • New Topic