Solved

Strings in Java

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
mergeTwo  challenge 13 72
Performance issue while iterating with streams 5 39
groovy example issue 10 70
github account with ecipse 1 20
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

746 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

14 Experts available now in Live!

Get 1:1 Help Now