Solved

String related question

Posted on 2004-08-20
6
205 Views
Last Modified: 2010-03-31
Hello All,

Have a look on following code. I know that for comparision of Strings, we need to use equals() method. But When I test following code, it is always prints "Hello". How come this is possible??


class String_Test       
{
      public static void main(String[] args)
      {

            String str="Hello";
            String str1="Hello";
            if(str==str1)
            {
                  System.err.println("Hello");
            }
            else
            {
                  System.err.println("Hi");
            }

      }
}
0
Comment
Question by:sudhakar_koundinya
  • 4
6 Comments
 
LVL 35

Accepted Solution

by:
girionis earned 50 total points
Comment Utility
The literal "Hello" is in the string pool (to save memeory space) so any strings you will create with the same literal "Hello" will be taken from the pool (and will have the same reference) :)
0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
> so any strings you will create with the same literal "Hello"

to make it clear, any Strings that you will create *without* using the "new" keyword.
0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
If you want to have different memory addresses of course you use the following:

String str= new String("Hello");
String str1= new String("Hello");

Any String you create using the "" will be put in a string of pools, so any subsequent string you create with the same value and in the same way (using the double quotes) is taken from and put in the pool of strings.

So if the string "Hello" exists in the string pool and you craet another string with the same value then the same memory address is returned to the new string (that's why comparison with == will give true), otherwise a new string is created and is put in the pool.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 4

Expert Comment

by:bhpr
Comment Utility
aggred with <girionis >
0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
:)
0
 
LVL 20

Expert Comment

by:Venabili
Comment Utility
For further reading:
http://www.janeg.ca/scjp/lang/strLiteral.html

Summary

    * if String objects having the same data are created using a constant expression, a string literal, a reference to an existing string, or by explicitly using the intern() method, their references will be the same

    * if String objects having the same data are created explicitly with the new operator or their values are computed at runtime, their references will be different
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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:

772 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

12 Experts available now in Live!

Get 1:1 Help Now