aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String declaration Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String declaration" Watch "String declaration" New topic
Author

String declaration

swapnil paranjape
Ranch Hand

Joined: May 15, 2005
Posts: 126
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

Joined: May 14, 2006
Posts: 6

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

Joined: Feb 02, 2006
Posts: 2023
Strig,Literally
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026

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).


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
swapnil paranjape
Ranch Hand

Joined: May 15, 2005
Posts: 126
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

Joined: May 22, 2006
Posts: 16
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

Joined: May 22, 2006
Posts: 16
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

Joined: May 15, 2005
Posts: 126
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
 
subject: String declaration