• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

String problem

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class DemoString
{
public static void main(String[] args)
{
String s1="abc";
String s2="abc";
System.out.println(s1);// abc
System.out.println(s2);//abc

String s3=new String("abc");
System.out.println(s3);//abc


if(s1==s2)
{
System.out.println("true");
}
else
System.out.println("false"); // prints true

/*------------------------------------------------------------------------*/
if(s1==s3)
{
System.out.println("true");
}
else
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?
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 - http://www.precisejava.com/javaperf/j2se/StringAndStringBuffer.htm
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
lowercase baba
Posts: 13086
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what you need to know is basically...

oh crud... nevermind.

 
What's wrong? Where are you going? Stop! Read this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic