File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes String problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String problem" Watch "String problem" New topic

String problem

kedar parundekar
Ranch Hand

Joined: May 10, 2006
Posts: 40
class DemoString
public static void main(String[] args)
String s1="abc";
String s2="abc";
System.out.println(s1);// abc

String s3=new String("abc");

System.out.println("false"); // prints true

System.out.println("false"); // prints false


Question : In case of above code
if(s1==s2) output is true
if(s1==s3) output is false
Can anybody tell me why like this?
Stephen Foy
Ranch Hand

Joined: Oct 17, 2005
Posts: 143
I dont know the exact reason, but i think it has something to do with the way the string is constructed using:

It works fine when it is declared as:

Stephen Foy - Microsoft Application Development Consultant
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15080

There are a few things you have to understand.

First of all, the == operator compares object references and not the contents of the objects themselves. So, == will only return true if the operands on both sides of it refer to the exact same object. If you have two separate String objects it will return false, even if the String objects contain the same characters. If you want to compare two String objects to see if they contain the same characters, use the equals() method:

Second, string literals are stored in a pool by Java. If you use the same string literal twice (or more times), the string is stored only once in the program. Because of this, s1 and s2 refer to the same String object in your program, so s1 == s2 is true.

With s3, you are explicitly creating a new String object, which is initialized with the contents of the literal "abc". Because you explicitly create a new String object, it is a different object than the one in the pool that s1 and s2 point to, so s1 == s3 is false.

By the way, you should never use new String("...") (the String constructor with a string literal as an argument) in a serious Java program, because it isn't necessary and only adds overhead (you create an extra String object without any benefits).

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Mandar Max
Ranch Hand

Joined: Mar 14, 2006
Posts: 38
The literal string "abc" is 'interned' means it is added to an internal pool of strings maintained by the JVM. So when you compared s1 == s2 the comparison took place between same object references and hence the result was 'true'. On the other hand when you create a String object using 'new', the JVM does not check the internal pool but creates a new object. This is the reason the result of s1 == s3 came as 'false'.

This is a good article on this -

"The trouble with doing something right the first time is that nobody appreciates how difficult it was!"
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

You need to know a few things to understand this:

1) The == operator asks if two variables refer to the same object -- i.e., they point to the same physical block of memory in the computer.

2) The equals() method, on the other hand, asks if two objects are "the same" in some way that makes sense for a particular class. For String, it means the two Strings contain the same characters in the same order.

3) Finally (and this is slightly oversimplifying, but indulge me, Tony) all identical String literals (Strings that appear in a program between double quotes) refer to the same String object; in other words, if the sequence "abc" appears three times in a program, then all three copies refer to the same physical String object in the computer's memory.

SO now we can look at your program and understand the output. s1 and s2 refer to the same String object, so "==" says true; s3 is another object altogether, since you created it with "new". It contains the same characters, in the same order, but it's a different chunk of memory.

Now, if you replace "==" with equals() -- i.e., if (s1.equals(3)) -- then all the answers would be true, since all the Strings have the same characters in the same order.

THe moral of this story is that you should never use == to compare Strings: always use the equals() function. As a general rule, you should use the equals() function to compare objects unless you have some specific reason to use ==; some specific reason why you're testing if two things are the same object, rather than just whether they're alike.

[Jess in Action][AskingGoodQuestions]
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11952

what you need to know is basically...

oh crud... nevermind.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
I agree. Here's the link:
subject: String problem
It's not a secret anymore!