• 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
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

Number of Object Created?

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone help me with this question?
How does it work with StringBuffers does it refer to the original object or does it create a new object.

StringBuffer s1 = new StringBuffer("abc");
StringBuffer s2 = s1;
StringBuffer s3 = new StringBuffer("abc");

How many objects are created here?
Options are
1
2
3

Thanks in advance!!
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sriram Rajagopalan wrote:Can someone help me with this question?
How does it work with StringBuffers does it refer to the original object or does it create a new object.

StringBuffer s1 = new StringBuffer("abc");
StringBuffer s2 = s1;
StringBuffer s3 = new StringBuffer("abc");

How many objects are created here?
Options are
1
2
3

Thanks in advance!!


Hi Sriram,

Welcome to the ranch!

Every that you see a new Something() a new object gets created. When you assign a reference to another, except exceptions due to wrapper boxing, no object gets created.

So, above, you create a new StringBuffer and assign it to s1. Then, you assign s1 to s2, which means that s1 and s2 will point to the same StringBuffer object (no new object is created.) Then, you create a new StringBuffer and assign it to s3.

So only 2 objects are created (the first object is pointed to by two references, s1 and s2.)
 
Greenhorn
Posts: 7
Hibernate Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sriram

The answer should be 2 new objects .... am I correct folks??

According to me it has to be 2 as those two objects are created with the 'new' keyword and the reference variable s2 simply refers to the object referred by s1.

Amit
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this code, three reference variables and only 2 object are created using "new" operator. check this about String . Its a good read.
 
Sriram Rajagopalan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys...
Thanks for the replies...
:-)
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

One interesting difference between StringBuffer and String class is that objects of StringBuffer are mutable. It means that if I say

String s1 = "abc";
s1 = s1+"def";

The above statements create two different objects. Finally, s1 will point to the second object created.

StringBuffer s1 = "abc";
s1 = s1+"def";

Only one object is created in above case.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Soniya Ahuja wrote:Hi,

One interesting difference between StringBuffer and String class is that objects of StringBuffer are mutable. It means that if I say

String s1 = "abc";
s1 = s1+"def";

The above statements create two different objects. Finally, s1 will point to the second object created.

StringBuffer s1 = "abc";
s1 = s1+"def";

Only one object is created in above case.


Hi Soniya,

That's a good point. But in reality there are 3 objects created in both instances. In the first case, each of the 2 string literals will cause a String instance to be created in the string pool, and the concatenation on the second line will cause an additional String object to be created in heap memory, which s1 is bound to via assignment.

In the second case, first you need to modify the code (as it is, it won't compile, since you can't assign a String reference to a StringBuffer reference. Also, notice that you have the same problem in the second line.) This is the proper code:



In this case there will be again three objects (the two String objects in the String Literal Pool, and the single mutable StringBuffer object.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Soniya Ahuja wrote:Hi,

One interesting difference between StringBuffer and String class is that objects of StringBuffer are mutable. It means that if I say

String s1 = "abc";
s1 = s1+"def";

The above statements create two different objects. Finally, s1 will point to the second object created.

StringBuffer s1 = "abc";
s1 = s1+"def";

Only one object is created in above case.



As I understood,

String s1 = "abc";
s1 = s1+"def";

will create 3 string object.
1. create abc in String Constant Pool and assign reference to s1
2. create def in String Constant Pool and no assigning reference to other object.
3. create abcdef in String Constant Pool and assign reference to s1.

At this time, "abc" and "def" will be eligible to GC.

Correct me if i misunderstood.
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Panumat Wiwatmaikul wrote:

will create 3 string object.
1. create abc in String Constant Pool and assign reference to s1
2. create def in String Constant Pool and no assigning reference to other object.
3. create abcdef in String Constant Pool and assign reference to s1.

At this time, "abc" and "def" will be eligible to GC.

Correct me if i misunderstood.



Hi Panumat,

i think string pool is the special place. It's not resides in heap.
and as far as i know that the GC only run in heap.

please somebody correct me if i'm wrong or something missing.
Thank you
 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Panumat Wiwatmaikul wrote:
At this time, "abc" and "def" will be eligible to GC.

Correct me if i misunderstood.



the String object are not eligible for GC. String "abc" is created in a heap but it has a reference in the String pool which is used by the String objects. But is string object is set to null, still a reference is present in string pool for further reference by an other variable.
Check this javaranch link on Strings :String literal

 
Welly Tambunan
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Harshit Rastogi wrote:
the String object are not eligible for GC. String "abc" is created in a heap but it has a reference in the String pool which is used by the String objects. But is string object is set to null, still a reference is present in string pool for further reference by an other variable.
Check this javaranch link on Strings :String literal



mmm... so when the string literal is found by jvm its created in heap and at the same time
the reference to it is created in string pool.

wow...
is that true if i said the string literal can't be garbage collected?
 
Soniya Ahuja
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When we say String s1 = "abc"

The String object s1 is created in the pool and not in the heap.

But as soon as you say s1+"def", the concat method needs to be called on s1 for which, s1 is shifted from the pool to the heap. So after this stage, a new object is created on the heap.

If I had said String s1 = new String ("abc"), s1 would have been directly created on the heap and would not be in the pool. As long as it resides in the pool, a declaration like

String s2 = "abc" will point to the same location

However, String s2 = new String ("abc") will create a new object on the heap.

Please correct me if I am wrong
 
Welly Tambunan
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Soniya Ahuja wrote:When we say String s1 = "abc"
The String object s1 is created in the pool and not in the heap.



i think every object is created in heap.
in string pool we have only reference to string object in heap..

i hope i'm not make a mistake.
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ruben Soto wrote:


In the first case, each of the 2 string literals will cause a String instance to be created in the string pool, and the concatenation on the second line will cause an additional String object to be created in heap memory, which s1 is bound to via assignment.



In this case there will be again three objects (the two String objects in the String Literal Pool, and the single mutable StringBuffer object.


Hi ruben,
i am agree with you in the first case.i think in the second case only two objects will create.because stringbuffer is mutable that is we can change the existing object,where as in the case of string we can't change the existing object. in the above code line 1 one object is created then in line 2 another object is created and that will be append to existing object which is created in line 1.so totally 2 objects
that is my thinking
if i am wrong please correct me!!!

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Soniya Ahuja wrote:Hi,

One interesting difference between StringBuffer and String class is that objects of StringBuffer are mutable. It means that if I say

String s1 = "abc";
s1 = s1+"def";

The above statements create two different objects. Finally, s1 will point to the second object created.




I think in your first case 3 object is created 1) abc 2) def 3) abcdef and it goes to pool
String object created in heap only when you write keyword "new".
 
Soniya Ahuja
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm, one question - Do objects in the String pool also qualify as real objects. I mean, they can't be GCed and so on. Hence I wrote that only one object is created... on the heap. Ruben thanks for correcting my mistake with StringBuffer. Lost on that one . I'm looking at Core Java after a year now .. Looks like I messed up the buffer . However, I'd still like to know if we also consider objects created in the string pool while answering such questions.

@Chaitanya: A string o0bject is created in the heap even when you call a method on a string object from the pool. So jsut as I had mentioned, calling + would call concat and before concat is called, we'll have s1 on the heap. Am I right?
 
Chaitanya Jadhav
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Soniya Ahuja wrote:

@Chaitanya: A string o0bject is created in the heap even when you call a method on a string object from the pool. So jsut as I had mentioned, calling + would call concat and before concat is called, we'll have s1 on the heap. Am I right?




My be you right..!!
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

NagarajGoud uppala wrote:
Hi ruben,
i am agree with you in the first case.i think in the second case only two objects will create.because stringbuffer is mutable that is we can change the existing object,where as in the case of string we can't change the existing object. in the above code line 1 one object is created then in line 2 another object is created and that will be append to existing object which is created in line 1.so totally 2 objects
that is my thinking
if i am wrong please correct me!!!


NagarajGoud,

In the second case there are 2 String objects (each distinct string literal that appears in the code causes a different String object to be created) plus the StringBuffer object. I hope that clears your doubts.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Soniya Ahuja wrote:Hmm, one question - Do objects in the String pool also qualify as real objects. I mean, they can't be GCed and so on. Hence I wrote that only one object is created... on the heap. Ruben thanks for correcting my mistake with StringBuffer. Lost on that one . I'm looking at Core Java after a year now .. Looks like I messed up the buffer . However, I'd still like to know if we also consider objects created in the string pool while answering such questions.

@Chaitanya: A string o0bject is created in the heap even when you call a method on a string object from the pool. So jsut as I had mentioned, calling + would call concat and before concat is called, we'll have s1 on the heap. Am I right?


Soniya,

I don't think I expressed my self well. When a string literal is found that creates a new String object in the heap (just like any other string object.) The difference is that a reference to that String object is created in the String Literal Pool. You can go through this document to see the details: Strings, Literally.
 
NagarajGoud uppala
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi Ruben,
i think both the cases are similar statements, one is mutable another immutable objects.
number of immutable objects in case 1 are more than the mutable of case 2.
to clear my doubt can you please explain diagramatically how the objects are created in case 1 and case 2?
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

NagarajGoud uppala wrote:
Hi Ruben,
i think both the cases are similar statements, one is mutable another immutable objects.
number of immutable objects in case 1 are more than the mutable of case 2.
to clear my doubt can you please explain diagramatically how the objects are created in case 1 and case 2?


Hi NagarajGoud,

Case 1:
String for "hi" is created, and a reference in the String Constant Pool is made to point to it. (1 object created.)
String for "bye" is created, and a reference in the String Constant Pool is made to point to it. (1 object created.)
String reference str is made to point to "hi" (no new object created.)
New String object is created via concatenation of "hi" and "bye", and String reference str is made to point to it. (1 object created.)

Case 2:
String for "hello" is created, and a reference in the String Constant Pool is made to point to it. (1 object created.)
String for "how are you" is created, and a reference in the String Constant Pool is made to point to it. (1 object created.)
StringBuffer reference sb is made to point to a new StringBuffer object which contains "hello". (1 object created.)
StringBuffer object's contents are modified by append() method. (no new object created.)

So there are 3 objects created in each case. Notice that every time you call a String method which indicates in its signature that returns a String, a new String object is created (except toString(), which just returns a reference to this), whether that object is assigned to a reference or not.

I hope that clarifies things.
 
NagarajGoud uppala
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ruben,
Thanks for your explanation.
For case 2 objects will create as follows am i right??
digaramatic representation
 
NagarajGoud uppala
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

NagarajGoud uppala wrote:Hi Ruben,
Thanks for your explanation.
For case 2 objects will create as follows am i right??

 
NagarajGoud uppala
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please anybody tell me how to delete a message??
 
Sheriff
Posts: 9683
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

NagarajGoud uppala wrote:please anybody tell me how to delete a message??



I don't think you can do that ...
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

NagarajGoud uppala wrote:please anybody tell me how to delete a message??


Like Ankit said, you can't delete the message. But you can delete its content by editing it. Since I assume you want to delete the message you were addressing to me, I won't read it. Let me know if I'm wrong.
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ruben Soto wrote:


Case 2:
String for "hello" is created, and a reference in the String Constant Pool is made to point to it. (1 object created.)
String for "how are you" is created, and a reference in the String Constant Pool is made to point to it. (1 object created.)
StringBuffer reference sb is made to point to a new StringBuffer object which contains "hello". (1 object created.)
StringBuffer object's contents are modified by append() method. (no new object created.)

So there are 3 objects created in each case. Notice that every time you call a String method which indicates in its signature that returns a String, a new String object is created (except toString(), which just returns a reference to this), whether that object is assigned to a reference or not.



So with this case 2 explanation, the answer to the original question posted should be 3 and not 2 right?


so the answer should be 3 or do I have it all wrong when the exam is just about in two weeks
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kavita,

You are correct (3 objects are created in total.) I overlooked the string literal the first time around.

Good luck with your exam!
 
Kavita Tipnis
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ruben,
Thank you
 
NagarajGoud uppala
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ruben,
you need to read that message.i want to delete which is quoted again.in that i gave some url please go through that and let me know weather i am correct or not
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just went through your diagram, and that's the way Case 2 looks after the last line. Except that there are String references in the String Constant Pool pointing to the "hello" and "how are you" String objects.
 
NagarajGoud uppala
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK Ruben Thanks
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No problem Nagaraj, glad that cleared things up.
 
Would you like to try a free sample? Today we are featuring tiny ads:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic