Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

String related question

Posted on 2004-08-20
6
Medium Priority
?
215 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 35

Accepted Solution

by:
girionis earned 200 total points
ID: 11849947
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
ID: 11849952
> 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
ID: 11849984
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 4

Expert Comment

by:bhpr
ID: 11850057
aggred with <girionis >
0
 
LVL 35

Expert Comment

by:girionis
ID: 11850111
:)
0
 
LVL 20

Expert Comment

by:Venabili
ID: 11850492
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses

618 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