• 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

== with Strings.......

 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Source:www.javabeat.net



Answer:Equal




Answer:Equal


Answer:Not Equal


Answer:Not Equal

I know that Strings are immutable. When we are invoking methods to modify that it creates a new object with modified content.

How should we analyze these type of questions?

Can anyone help me�.?

Thanks in advance....


 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ganesh the toString method of the string class returns this. So

String s="some text";
s == s.toString()

will always be true. All the other methods of the String class are also optimized. So if you call toUpperCase on a string which contains only upper case letters (or has no letters at all eg. has only numbers or symbols), than the toUpperCase method will not create a new String object and will return this instead. Another example would be trim. If you call trim on a string which has no leading and trailing space characters, then you will get the same string as you passed to it. So

String s = "ANKIT";
s== s.toUpperCase();

will always return true...
 
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ganeshkumar cheekati:
When we are invoking methods to modify that it creates a new object with modified content.
[/QB]


So the trick is if the new content is different (modified), then a new object is created, hence the == will return false. Also if the "new" keyword is used a new object is created, then == will also return false. I.e.

"String" == new String("String")

is false.
[ December 15, 2008: Message edited by: Duc Vo ]
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good work Ankit....:-)
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by krishna prasad raghavan:
Good work Ankit....:-)



Nothing big man . Just peek into the documentation and you will find all these details...
 
reply
    Bookmark Topic Watch Topic
  • New Topic