Solved

Strings in Java

Posted on 2004-09-16
5
170 Views
Last Modified: 2010-03-31
I have read that if I use the == operator to compare two string objects,it compares the addresses while the "equals" operator would compare only the contents.
Is it true?

To the following code:

class n
{
public static void main(String art[])
{
String s1="HAARHUIS";
String s2="HAARHUIS";
if(s1==s2)
System.out.println("They are the same");
if(s1.equals(s2))
System.out.println("They are still the same");
}
}

I get:
They are the same
They are still the same
0
Comment
Question by:perfect_tranquility
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 12079967
>>Is it true?

Yes ;-)
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 250 total points
ID: 12079978
You get that output because the reference happens to be the same. Try this instead:

String s1="HAARHUIS";
String s2= new String("HAARHUIS");
0
 

Author Comment

by:perfect_tranquility
ID: 12080011
CEHJ,

Bingo!

the new operator forces a new reference string to be created instead of using the old one.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12080026
>>the new operator forces a new reference string to be created instead of using the old one.

Spot on ;-)
0
 

Author Comment

by:perfect_tranquility
ID: 12080029
I am happy I am getting the answers.I will be even happier if the answers come with a little bit of more explanation.
Thanks for the support
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question