wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How many String objects gets created Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How many String objects gets created" Watch "How many String objects gets created" New topic
Author

How many String objects gets created

Abhinav Jat
Greenhorn

Joined: Nov 05, 2012
Posts: 4

As per SCJP book there are 8 objects being created in the following code.

String s1 = "spring ";
String s2 = s1 + "summer ";
s1.concat("fall ");
s2.concat(s1);
s1 += "winter ";
System.out.println(s1 + " " + s2);

String concatenation is confusing now.
Can anybody help me with the explanations on this code.
Why do not we consider System.out.println(s1 + " " + s2); in this case.(2 objects creation)
Should the answer for this question be 10 objects.
Rahul mir
Ranch Hand

Joined: Oct 31, 2012
Posts: 38

Abhinav Jat wrote:As per SCJP book there are 8 objects being created in the following code.

String s1 = "spring ";
String s2 = s1 + "summer ";
s1.concat("fall ");
s2.concat(s1);
s1 += "winter ";
System.out.println(s1 + " " + s2);

String concatenation is confusing now.
Can anybody help me with the explanations on this code.
Why do not we consider System.out.println(s1 + " " + s2); in this case.(2 objects creation)
Should the answer for this question be 10 objects.


Why do not we consider System.out.println(s1 + " " + s2); in this case.(2 objects creation) .. here s1 and s1 are parameter to a function not two object they are reference for an object . ex: String s = "abc" here s is a reference and "abc" is an object.

In java strings are immutable, we can't do any modification in a string, every time we want do any change in string, JVM creates a new string object.

here "spring" "summer" "fall" and "winter" are string object you can easily recognized it (total 4 object).
String s2 = s1 + "summer " this operation create a new object "spring summer"(5)
s1.concat("fall "); it creates "spring fall"(6)
s2.concat(s1); "spring summer spring" (7)
s1 += "winter "; "spring winter"(8)

at final point s1 = "spring winter" and s2 = "spring summer spring" only 2 object survived and rest 6 are lost the reference and waiting for garbage collector .



Enkita mody
Ranch Hand

Joined: Aug 06, 2012
Posts: 333

It was about total live String objects not dead+alive.


OCA7
Rahul mir
Ranch Hand

Joined: Oct 31, 2012
Posts: 38

ankita modi. wrote:It was about total live String objects not dead+alive.


if question is total number of string object got created then it is 8 and total no. of live object it is 2.
Abhinav Jat
Greenhorn

Joined: Nov 05, 2012
Posts: 4

Rahul mir wrote:
Abhinav Jat wrote:As per SCJP book there are 8 objects being created in the following code.

String s1 = "spring ";
String s2 = s1 + "summer ";
s1.concat("fall ");
s2.concat(s1);
s1 += "winter ";
System.out.println(s1 + " " + s2);

String concatenation is confusing now.
Can anybody help me with the explanations on this code.
Why do not we consider System.out.println(s1 + " " + s2); in this case.(2 objects creation)
Should the answer for this question be 10 objects.


Why do not we consider System.out.println(s1 + " " + s2); in this case.(2 objects creation) .. here s1 and s1 are parameter to a function not two object they are reference for an object . ex: String s = "abc" here s is a reference and "abc" is an object.

In java strings are immutable, we can't do any modification in a string, every time we want do any change in string, JVM creates a new string object.

here "spring" "summer" "fall" and "winter" are string object you can easily recognized it (total 4 object).
String s2 = s1 + "summer " this operation create a new object "spring summer"(5)
s1.concat("fall "); it creates "spring fall"(6)
s2.concat(s1); "spring summer spring" (7)
s1 += "winter "; "spring winter"(8)

at final point s1 = "spring winter" and s2 = "spring summer spring" only 2 object survived and rest 6 are lost the reference and waiting for garbage collector .





we cannot say that s1 and s2 are the parameter passed to a method. parameters passed to a method can be like method(a,b). if we say method(1+2), these are not two parameters. similarly we cannot say that s1+""+s2 are 2 parameters. This is only one parameter but result of these operation(concatenation in string will be processed by the method.

s1 += " winter "; ==> s1 = s1+" winter; ( one object creation as "spring winter")
in syso s1+" " = "spring winter " --> +s2 = "spring winter spring summer". (why do not we say that there are 2 more objects created)

Rahul mir
Ranch Hand

Joined: Oct 31, 2012
Posts: 38

Abhinav Jat wrote:
Rahul mir wrote:
Abhinav Jat wrote:As per SCJP book there are 8 objects being created in the following code.

String s1 = "spring ";
String s2 = s1 + "summer ";
s1.concat("fall ");
s2.concat(s1);
s1 += "winter ";
System.out.println(s1 + " " + s2);

String concatenation is confusing now.
Can anybody help me with the explanations on this code.
Why do not we consider System.out.println(s1 + " " + s2); in this case.(2 objects creation)
Should the answer for this question be 10 objects.


Why do not we consider System.out.println(s1 + " " + s2); in this case.(2 objects creation) .. here s1 and s1 are parameter to a function not two object they are reference for an object . ex: String s = "abc" here s is a reference and "abc" is an object.

In java strings are immutable, we can't do any modification in a string, every time we want do any change in string, JVM creates a new string object.

here "spring" "summer" "fall" and "winter" are string object you can easily recognized it (total 4 object).
String s2 = s1 + "summer " this operation create a new object "spring summer"(5)
s1.concat("fall "); it creates "spring fall"(6)
s2.concat(s1); "spring summer spring" (7)
s1 += "winter "; "spring winter"(8)

at final point s1 = "spring winter" and s2 = "spring summer spring" only 2 object survived and rest 6 are lost the reference and waiting for garbage collector .





we cannot say that s1 and s2 are the parameter passed to a method. parameters passed to a method can be like method(a,b). if we say method(1+2), these are not two parameters. similarly we cannot say that s1+""+s2 are 2 parameters. This is only one parameter but result of these operation(concatenation in string will be processed by the method.

s1 += " winter "; ==> s1 = s1+" winter; ( one object creation as "spring winter")
in syso s1+" " = "spring winter " --> +s2 = "spring winter spring summer". (why do not we say that there are 2 more objects created)



Here s1 and s2 are references that have the address of the two new born strings, you can use many time this reference, no new object will be create, until you do some operation on the string. Here in print statement only result is displaying on the screen, there are no modification happen on reference or object.
Kisor Biswal
Greenhorn

Joined: Mar 10, 2011
Posts: 3
Rahul mir wrote:
Abhinav Jat wrote:
Rahul mir wrote:
Abhinav Jat wrote:As per SCJP book there are 8 objects being created in the following code.

String s1 = "spring ";
String s2 = s1 + "summer ";
s1.concat("fall ");
s2.concat(s1);
s1 += "winter ";
System.out.println(s1 + " " + s2);

String concatenation is confusing now.
Can anybody help me with the explanations on this code.
Why do not we consider System.out.println(s1 + " " + s2); in this case.(2 objects creation)
Should the answer for this question be 10 objects.


Why do not we consider System.out.println(s1 + " " + s2); in this case.(2 objects creation) .. here s1 and s1 are parameter to a function not two object they are reference for an object . ex: String s = "abc" here s is a reference and "abc" is an object.

In java strings are immutable, we can't do any modification in a string, every time we want do any change in string, JVM creates a new string object.

here "spring" "summer" "fall" and "winter" are string object you can easily recognized it (total 4 object).
String s2 = s1 + "summer " this operation create a new object "spring summer"(5)
s1.concat("fall "); it creates "spring fall"(6)
s2.concat(s1); "spring summer spring" (7)
s1 += "winter "; "spring winter"(8)

at final point s1 = "spring winter" and s2 = "spring summer spring" only 2 object survived and rest 6 are lost the reference and waiting for garbage collector .





we cannot say that s1 and s2 are the parameter passed to a method. parameters passed to a method can be like method(a,b). if we say method(1+2), these are not two parameters. similarly we cannot say that s1+""+s2 are 2 parameters. This is only one parameter but result of these operation(concatenation in string will be processed by the method.

s1 += " winter "; ==> s1 = s1+" winter; ( one object creation as "spring winter")
in syso s1+" " = "spring winter " --> +s2 = "spring winter spring summer". (why do not we say that there are 2 more objects created)



Here s1 and s2 are references that have the address of the two new born strings, you can use many time this reference, no new object will be create, until you do some operation on the string. Here in print statement only result is displaying on the screen, there are no modification happen on reference or object.


The method System.out.println(s1 + " " + s2); takes a sting as parameter so all the strings must be concatenated before the method is invoked. I believe this line creates 2more objects(1. " " and 2. After concatenating s1, s2 and " ").
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How many String objects gets created
 
Similar Threads
How many String objects are created.
Are String objects created in System.out.println Statement
Strings
how many String objects are created
Total Objects created ....