Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

what is the difference in these two types of String code?

Posted on 2003-02-23
8
Medium Priority
?
185 Views
Last Modified: 2010-03-31
what is the difference between these two?
a)
String a="abc";
String b="abc";
if(a==b)
System.out.println("true");
else
System.out.println("false");

b)
String a=new String("abc");
String b=new String("abc");
if(a==b)
System.out.println("true");
else
System.out.println("false");

In the first case it is printing true and in the second case it is printing false why?
0
Comment
Question by:pnagasuresh
7 Comments
 
LVL 1

Accepted Solution

by:
shji1 earned 80 total points
ID: 8002737
Strings in Java are handled, if initialised in the static form of 'String a="abc"', in a constants pool.
You can read all about it here (VM Specs - Section 5.1):
http://java.sun.com/docs/books/vmspec/2nd-edition/html/ConstantPool.doc.html#73272
--
0
 
LVL 5

Expert Comment

by:vemul
ID: 8003089
When u say String a = "abc",
Java VM creates a string "abc" and stores it a constants pool. If there is another one with the same value, it simply refers to the same object instead of creating a new one, which is why in you case
String b = "abc"

would point to the same one.

if you did
String a = "abc";
 a = "abcd";

since "abcd" does not exist in the constants pool, it is created, so now there will be two strings in the pool.

by doing
String a = new String("abc");
String b = new String("abc");

you are explicitly creating two different objects which do not reference to the same object and hence they will be false when compared
0
 

Expert Comment

by:fivesigmaevent
ID: 8005185
The == operator compares references, or memory addresses.

In part a) strings a and b have the same reference since it is a string in the constants pool. They refer to the exact same memory address.

In part b) even though the strings have the same contents, they have different references. They refer to different memory addresses.

To compare the actual strings, try

if(a.equals(b))
{ ...

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Expert Comment

by:odog1999
ID: 8005818
>>In part a) strings a and b have the same reference since it is a string in the constants pool. They refer to the exact same memory address.

Both part a) and part b) are actually the exact same thing.  In both cases, there will be two separate instances of the string "abc", with one stored at the memory address of String a, and one at address of String b.  with the above code, both will always return false, as a and b are never referring to the same memory address, unless you say a=b or b=a, when they will point to the same memory address.  To compare the value of two different strings, you need to use the a.equals(b),like fivesigmaevent said.

Hth,
0
 
LVL 1

Expert Comment

by:odog1999
ID: 8005832
Oops!  My bad.  I had misread the lecture slide on Strings.  Sorry!
0
 

Author Comment

by:pnagasuresh
ID: 8008579
Why it maintains constant pool? What are the advantages? when we will use?
0
 

Expert Comment

by:CleanupPing
ID: 9059136
pnagasuresh:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month11 days, 7 hours left to enroll

564 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