Results 1 to 6 of 6
Thread: Comparing Strings
- 05-20-2008, 04:18 AM #1
Member
- Join Date
- May 2008
- Location
- Santiago, Chile
- Posts
- 8
- Rep Power
- 0
Comparing Strings
Hi everyone,
Why does this return true?
By the way... I'm teaching an introductory java course and I know everything about objects and memory allocation... I also know C, PHP, Assembly, Java, etc. I mention this so you don't start explaining me about objects and stuff like that :-)Java Code:String s1 = "hola"; String s2 = "hola"; if(s1 == s2) return true;
I just want to know why the code above returns true even though s1 and s2 are supposed to be 2 different String objects... I just don't get it. Is it a Java bug??
Thx.
- 05-20-2008, 05:33 AM #2
Member
- Join Date
- May 2008
- Location
- Santiago, Chile
- Posts
- 8
- Rep Power
- 0
Never mind... I've found the answer:
When the java compiler finds two or more string "constants" that are equals, it points them to the same reference.
- 05-20-2008, 05:57 AM #3
When you are creating String object like this.....String s1 = "hello";......then the compiler first checks in HEAP ....if same object is already there it points new to the existing one. But if you create like .... String s1 = new String("hello");.....it will always create a new String object.
intern() method is also there to check for existing Objects(new String("hello"))....in HEAP....
String s = "helo";
String s1 = new String("hello");
if(s == s1).......gives false
s1.intern();
if(s == s1).......gives true
Compiler does this to save memory.
and == operator checks only references(two objects are pointing to the same Object) so it returns true.
sanjeev,संजीव
- 05-21-2008, 07:21 AM #4
This question was answered a few times before. It is always a good idea to search the forums for answers before you ask.
Daniel @ [www.littletutorials.com]
Language is froth on the surface of thought
- 05-21-2008, 07:24 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
And the Google too. You can found lots of information regarding this. Most people confused with this each day.
- 05-21-2008, 09:03 AM #6
Member
- Join Date
- May 2008
- Location
- Santiago, Chile
- Posts
- 8
- Rep Power
- 0
Of course I tried searching :-)
But the only answers I found were "you must use equals() to compare Strings" and that was not my question... I wanted to know why sometimes using "==" to compare to String objects would return true even though they seemed to be different objects.
sanjeevtarar, thanks for your answer.
Similar Threads
-
Comparing Images
By shaungoater in forum Advanced JavaReplies: 0Last Post: 03-17-2008, 10:38 AM -
JSTL -- Comparing two strings for equality
By trinkets in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 02-12-2008, 04:39 PM -
Comparing Strings
By Java Tip in forum Java TipReplies: 0Last Post: 12-03-2007, 09:44 AM -
comparing
By Feng in forum New To JavaReplies: 2Last Post: 11-23-2007, 09:40 AM -
Comparing JavaWebFrameworks
By pegitha in forum Web FrameworksReplies: 1Last Post: 05-18-2007, 06:23 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks