?
Solved

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

Posted on 2003-02-23
8
Medium Priority
?
180 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
[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
8 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month15 days, 6 hours left to enroll

741 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