Solved

Strings in Java

Posted on 2004-09-16
5
166 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now