Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

How many String objects gets created

 
Greenhorn
Posts: 4
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 38
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 .



 
Ranch Hand
Posts: 333
Firefox Browser Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It was about total live String objects not dead+alive.
 
Rahul mir
Ranch Hand
Posts: 38
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 38
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 " ").
 
Well THAT's new! Comfort me, reliable tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic