Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-20-2008, 06:18 AM
Member
 
Join Date: May 2008
Location: Santiago, Chile
Posts: 3
souFrag is on a distinguished road
Comparing Strings
Hi everyone,

Why does this return true?
Code:
String s1 = "hola"; String s2 = "hola"; if(s1 == s2) 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 :-)

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.
__________________
Felipe Brahm

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 05-20-2008, 07:33 AM
Member
 
Join Date: May 2008
Location: Santiago, Chile
Posts: 3
souFrag is on a distinguished road
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.
__________________
Felipe Brahm

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 05-20-2008, 07:57 AM
sanjeevtarar's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Delhi(India)
Posts: 252
sanjeevtarar is on a distinguished road
Quote:
Originally Posted by souFrag View Post
Hi everyone,

Why does this return true?
Code:
String s1 = "hola"; String s2 = "hola"; if(s1 == s2) 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 :-)

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.
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,संजीव
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 05-21-2008, 09:21 AM
danielstoner's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Canada
Posts: 191
danielstoner is on a distinguished road
This question was answered a few times before. It is always a good idea to search the forums for answers before you ask.
__________________
Daniel @ [
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
]
Language is froth on the surface of thought
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 05-21-2008, 09:24 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 5,075
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
And the Google too. You can found lots of information regarding this. Most people confused with this each day.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Someone helped you?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.
Help:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Resources:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Web:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Tips:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 05-21-2008, 11:03 AM
Member
 
Join Date: May 2008
Location: Santiago, Chile
Posts: 3
souFrag is on a distinguished road
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.
__________________
Felipe Brahm

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Comparing Images shaungoater Advanced Java 0 03-17-2008 12:38 PM
JSTL -- Comparing two strings for equality trinkets JavaServer Pages (JSP) and JSTL 0 02-12-2008 06:39 PM
Comparing Strings Java Tip Java Tips 0 12-03-2007 11:44 AM
comparing Feng New To Java 2 11-23-2007 11:40 AM
Comparing JavaWebFrameworks pegitha Web Frameworks 1 05-18-2007 08:23 PM


All times are GMT +3. The time now is 12:39 PM.


VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org