File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String declaration

 
swapnil paranjape
Ranch Hand
Posts: 126
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ,
i have a doubt about string declaration

String s = new string("abc");

this type of declaration creates two objects and one refrence variable.

1) java will create new string object in normal(nonpool) memeory
2)"abc" will be placed in the pool.

so when we say for example
s.concat("def")
New object will be created in the pool "abcdef" and also will "abcdef" object will be created in normal(nonpool) memory ?

that means will there be total 4 objects ?
1)"abc"(in pool)
2)"abc"(in nonpool memory)
3)"abcdef"(in pool)
4)"abcdef" (in nonpool)

please clear my doubt...


what i want to ask is
point no1) by using this kind of declaration we are creating two objects and one ref variable so any concat ,or such type of operation will create multiple objects ?like in example above ...
after concat 4 objects will be created or just object in pool memory will be craeted?
 
sherin stephen
Greenhorn
Posts: 6
IntelliJ IDE Java Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i dont think tht the statement causes 2 objects to be created .
as far as i know , a string object is created in the heap and reference given to the variable . the reason why no literal pool is involved is the presence of 'new' .

any other sugestions ?
 
wise owen
Ranch Hand
Posts: 2023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strig,Literally
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

s.concat("def")


This will create a String object "abcdef" in String Constant Pool.

Now total will be three objects( 2 in String Constant Pool and one in non-pool).
 
swapnil paranjape
Ranch Hand
Posts: 126
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Refer book K&B string class explanation,

two objects will be created and one ref variable for these objects.one object is in pool and one non pool object

now when we do concat operation , it should be on both the objects refered by s (reference variable)

and hence both these pool and nonpooled objects should be affected and two more objects should be created.



if it is not so then what happens to the nonpool object created at string creation.it never gets modified???

please explain.
 
kt randhawa
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think there are 3 objects as below:
1)String s = new string("abc"); creates 2 String objects(one in pool & other in non pool with same value "abc")
2)s.concat("def")
creates 2 objects technically,one with value "def" in pool and other again in pool with value "abcdef" and this one has no reference pointing to it.

However,the reference variable s still holdsthe value "abc".

Here is another example:

String s1 = "spring ";
String s2 = s1 + "summer ";
s1.concat("fall ");
s2.concat(s1);
s1 += "winter ";
System.out.println(s1 + " " + s2);
What is the output?
For extra credit, how many String objects and how many reference variables were created prior to the println statement?
Answer:
The result of this code fragment is �spring winter spring summer�.
There are two reference variables, s1 and s2. There were a total of eight String objects created as follows: �spring�, �summer � (lost), �spring summer�, �fall�(lost), �spring fall� (lost), �spring summer spring� (lost), �winter� (lost), �springwinter� (at this point �spring� is lost). Only two of the eight String objects are not lost in this process.
 
kt randhawa
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to me,
4 objects are:
1)String s = new string("abc");
creates 2 objects("abc" in pool and "abc" in non pool refered by s)
2)s.concat("def") ; creates 2 objects (one with value "def" in pool and other with value "abcdef" again in pool.
However s still contains value "abc". and "def" and "abcdef" are lost.

Here is another example:
String s1 = "spring ";
String s2 = s1 + "summer ";
s1.concat("fall ");
s2.concat(s1);
s1 += "winter ";
System.out.println(s1 + " " + s2);
What is the output?
For extra credit, how many String objects and how many reference variables were created prior to the println statement? Answer:
The result of this code fragment is �spring winter spring summer�.
There are two reference variables, s1 and s2. There were a total of eight String objects created as follows: �spring�, �summer � (lost), �spring summer�, �fall�(lost), �spring fall� (lost), �spring summer spring� (lost), �winter� (lost), �springwinter� (at this point �spring� is lost). Only two of the eight String objects are not lost in this process.
 
swapnil paranjape
Ranch Hand
Posts: 126
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx wise owen

the tutorial helped me a lot to understand the funda.

so this is my understanding correct me if i am wrong:

1) string literal pool only contains reference to string object ; objects are created on the heap only.

2)when we use new operator for creating the string object ,then java creates new object and put refrence of this object in string pool.it doesnt refer to already exsiting object if any.

 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic