• 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

StringCode

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a pice of code:

class StringExample
{
public static void main(String[] args)
{
String s ="Hello";
String t = s.toLowerCase();
String u = s.toLowerCase();
if( t == u)
System.out.println("equals");
else
System.out.println("false");
}
}
In this code string obj 't' and 'u' are created using 's' obj
but it prints false
i can't under stand why like that?
I think toLowerCase() gives every time a new obj? am i right?
let me know?
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For wrapper classes, u have to use equals() for comparison. "==" will apply only for Primitive datatypes.

also u can look into this Thread
for when to use "==" and "equals"
[ December 16, 2005: Message edited by: Sri Ram ]
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by venkataramana ramana:
I have a pice of code:

class StringExample
{
public static void main(String[] args)
{
String s ="Hello";
String t = s.toLowerCase();
String u = s.toLowerCase();
if( t == u)
System.out.println("equals");
else
System.out.println("false");
}
}
In this code string obj 't' and 'u' are created using 's' obj
but it prints false
i can't under stand why like that?
I think toLowerCase() gives every time a new obj? am i right?
let me know?




Yes.

If you intern the two calls to toLowerCase(), then you will get a different result.



This prints equals.
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
"==" checks for the memory address of reference for two objects. where as equals doing the job of "==" check for the data. That is the reason why you have to use equals. That's why it printed false for you.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by venkataramana ramana:
...I think toLowerCase() gives every time a new obj? am i right?


Yes, and this code is a good demonstration of that.

But when it comes to value (reference) comparisons, Strings are special cases because of a "string pool," so note Keith's post above demonstrating the intern method. And to understand what's behind all of this, see Corey McGlone's article, Strings, Literally.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[venkataramana]: ...I think toLowerCase() gives every time a new obj? am i right?

Mmmnh, not always. It turns out that if a String is already all lowercase, then the method just returns the original string, without creating a new one. This fact is not documented, but it can be seen in source code or verified experimentally. You aren't expected to know this. However you also should not assume that the method always returns a new String, because the documentation doesn't say that.

There are many methods that return a String, whether the method is defined in the String class or elsewhere. For some of them the documentation explicitly states they always return a new String. For others, the documentation states that under certain conditions an existing string is re-used rather than creating a new string. And for other methods, the documentation doesn't say either way. Usually, this is intentional. Really, there's no good reason we should need to know whether a method is creating a new string or not. If it's possible to re-use an existing string, that's fine - it should make no difference to us. But we don't need to require the JVM to reuse a string - because sometimes it may take more processing power to detect cases where re-use is possible than it would to just create a new string and be done with it. There's no reason for the API to specify this sort of thing at all - we (the users) shouldn't care, and the authors of the API may want to leave some room for future implementations to try to optimize the performance however they like. If a detail is really unimportant, then leaving it unspecified is often a good design choice.

I think the only time you really need to know that a new String is being created is when you actually see a constructor being called. Anytime you see "new ...]" then that guarantees that a new object is being created. But for any method that returns a string - well, the documentation may or may not tell you if it's a new string. But in general, it doesn't matter anyway. As long as you don't accidentally use == to compare it to another string. Always use .equals() instead, and the distinction between new strings and preexisting strings is unimportant.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public static void main(String[] args)
{
String s ="Hello";
String t = s.toLowerCase().intern();
String u = s.toLowerCase().intern();
if( t == u)
System.out.println("equals");
else
System.out.println("false");
}
}
here as i know when u use '=='--it will return true if the both string references refer to same object
as in above code t and 'u' are two different object references
so after using 'intern()' method..does it make 't' and 'u' to refer to the same object as "t==u" returns true
Please guide on it
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to the API, a pool of String objects, initially empty, is maintained by the String class. If a call is made to intern() on a String object, if that object is already in the pool, as determined by equals, then a reference to that object is returned. Otherwise, the String object is added to the pool.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by pravin kumar:
...so after using 'intern()' method..does it make 't' and 'u' to refer to the same object as "t==u" returns true Please guide on it


See the link I posted above ("Strings, Literally").
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jim Yingst:
... It turns out that if a String is already all lowercase, then the method just returns the original string, without creating a new one. This fact is not documented, but it can be seen in source code or verified experimentally...


Thanks for pointing this out! (That's what happens when I assume without verifying. :roll: )
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic