This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
The moose likes Java in General and the fly likes Difference between String s Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Difference between String s" Watch "Difference between String s" New topic

Difference between String s

Rajendra Prakash
Ranch Hand

Joined: Sep 10, 2009
Posts: 293
Difference between 1.string s="raja"; and 2. string s=new String("raja");
what i read is , only one instance and one reference created for first case. Two instances and one
reference created for second case. Object created in constant memory for first case.
Object created in heap memory for second case.
My question is in general how many objects for both.
ayyappan Bas
Ranch Hand

Joined: Oct 11, 2008
Posts: 40

Just refer following URl

Thanks & Regards
Rajendra Prakash
Ranch Hand

Joined: Sep 10, 2009
Posts: 293
No , i am not clear. 1) String s=new String("raj"); for this code one object created in heap memory
and one object created in String pool.(String pool is also in heap memory ah
2) String s="raj"; for this code one objected created in string pool only .
Is it right or wrong. If wrong how
Rajendra Prakash
Ranch Hand

Joined: Sep 10, 2009
Posts: 293
I am getting different comments from google. Is it correct

In case of s1 2 objects will be formed one in heap memory while other in string constant memory.
In case of s2 1 object will be formed in string constant memory.
Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

This Journal article should interest you.

[My Blog]
All roads lead to JavaRanch
I agree. Here's the link:
subject: Difference between String s
It's not a secret anymore!