• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Strings

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Difference between
String str ="aaa";
String str = new String("aaa");

Where String literal will be stored in heap or stack?
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both string literals and string objects are stored in heap memory. The references to these strings are stored in stack memory. This is done by the JVM to avoid duplicate string objects in heap memory.

The difference between str="aaa" and str=new String("aaa") is about performance.
str=new String("aaa") is slower because the 'new' keyword creates a new object.
[ May 31, 2005: Message edited by: Andrew Nomos ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you'll find this article helpful...

http://www.javaranch.com/journal/200409/Journal200409.jsp#a1
 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there are basically two ways to creating String
1] String s1= "james Bond";
2] String s2= new String("james Bond");


we will see what happen when we write like 1]
here one String object is created and one reference variable pointing to it
when line one is get Compiled the String "James Bond"
is placed in String pool

now see what Happen when we do in 2nd way
when 2] get compile the "james Bond" get placed in String pool
at runtime due to new keyword fresh instance of string "james Bond" is created
and it is placed in non pool memory
hence now two object and one ref pointing to String in non Pool Memory
 
ganesh pol
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi andrew

i have not got u what u mean to say

The references to these strings are stored in stack memory. This is done by the JVM to avoid duplicate string objects in heap memory.

i think it depends upon where we declare String if it is declared in method
it's reference might have Stored on Stack pointing to String in Heap
but if String is not declared as locale i.e if it is declared as instance variable stored on heap pointing to String on HEAP
 
Ranch Hand
Posts: 577
Tomcat Server Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ganesh,

we will see what happen when we write like 1]
here one String object is created and one reference variable pointing to it
when line one is get Compiled the String "James Bond"
is placed in String pool

now see what Happen when we do in 2nd way
when 2] get compile the "james Bond" get placed in String pool
at runtime due to new keyword fresh instance of string "james Bond" is created
and it is placed in non pool memory
hence now two object and one ref pointing to String in non Pool Memory



Whatever you told was confusing for others who don't know string behaviour. Just go through the link provided above by Marc Weber. What I'm writing below is just in brief to the information provided in the link.

1] String s1="xyz";
2] String s2="xyz"+"pqr";
3] String s3= s1+"pqr";
4] String s2=new String("xyz");

When we compile the code, compiler notes that whether any string variables are initialized or not. We'll look into above four cases

1] When compiler encounters this, it will create a string object xyz on heap (NOT IN STRING LITERAL POOL) and the reference/address is stored in String Literal Pool/Constant table. After running the program, s1 is points to the created string object with this reference.
2] Compiler evaluates this at compile time and creates a string object xyzpqr, checks if there is any such string object is already present on the heap
if present, it just ignores this string object
if not present, places it on heap and referece is added to the string literal pool
3]As initialization doesn't take place at compile time(here for s1), compiler ignores to evaluate this expression and during run time the expression is evaluated and new string object containing xyzpqr is created which is identical to 1] but having different reference. Now this is not added to string literal pool
4]Just like 3] a new string object is created whose reference is not stroed in string literal pool is returned to s4.

When one adds these two statements, after string variable initializations
5] s3.intern();
6] s4.intern();
At run time, s3 and s4 will be referring to already created string objects with same string values and whose refereces are in string literal pool.

So the created string objects s3 and s4 before statement 5] and 6]are now dereferred and are ready for garbage collection.

Any comments are welcome.

Regards,
Narendranath
-----------------------
Preparing for SCJP
[ June 02, 2005: Message edited by: Naren Chivukula ]
 
Tomorrow is the first day of the new metric calendar. Comfort me tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic