aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes StringCode Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "StringCode" Watch "StringCode" New topic
Author

StringCode

venkat ramana
Greenhorn

Joined: Dec 15, 2005
Posts: 11
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?
Sri Ram
Ranch Hand

Joined: Oct 03, 2005
Posts: 118
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 ]
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
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.
Raghusham Sankargal
Ranch Hand

Joined: Oct 10, 2005
Posts: 59
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.


Raghusham Sankargal
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[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.


"I'm not back." - Bill Harding, Twister
pravin kumar
Ranch Hand

Joined: Nov 03, 2005
Posts: 30
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

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Aug 31, 2004
Posts: 11343

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: )
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: StringCode