Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes How many objects are being created here in this code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How many objects are being created here in this code" Watch "How many objects are being created here in this code" New topic
Author

How many objects are being created here in this code

monis raza
Ranch Hand

Joined: Sep 05, 2011
Posts: 38

Prajakta Acharya
Ranch Hand

Joined: Nov 08, 2012
Posts: 138

I guess its 3 since s1 and s2 would refer to same string in the pool and new operator will always create new strings in memory not part of pool.


Regards,
Prajakta
monis raza
Ranch Hand

Joined: Sep 05, 2011
Posts: 38

i think they are 5 or are they ?
monis raza
Ranch Hand

Joined: Sep 05, 2011
Posts: 38


i think they are 5 or are they ?
monis raza wrote:

String s = "abc"; // creates one String object and one
// reference variable
In this simple case, "abc" will go in the pool and s will refer to it.
String s = new String("abc"); // creates two objects,
// and one reference variable
In this case, because we used the new keyword, Java will create a new String object
in normal (nonpool) memory, and s will refer to it. In addition, the literal "abc" will
be placed in the pool.



am i deciphering it correctly ?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
When do you mean they are created? Do you mean at run time or are you including class loading time? The answer will be different.

Please go through the code and count all the object references and see which point to the same object and which point to different objects on the heap.

You do realise this is pointless code which is only of use for passing exams and has no real‑life use. This sort of question appears here frequently, so please search this forum and you will find similar questions.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
monis raza wrote: . . . am i deciphering it correctly ?
Afraid not.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Look at this old JavaRanch Journal article, which I think will be very helpful in this sort of problem.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42374
    
  64
Note that the answer could also be "hundreds" or possibly "thousands", because the JVM will create a great many objects on its own while it runs this code.


Ping & DNS - my free Android networking tools app
monis raza
Ranch Hand

Joined: Sep 05, 2011
Posts: 38

Campbell Ritchie wrote:When do you mean they are created? Do you mean at run time or are you including class loading time? The answer will be different.

Please go through the code and count all the object references and see which point to the same object and which point to different objects on the heap.

You do realise this is pointless code which is only of use for passing exams and has no real‑life use. This sort of question appears here frequently, so please search this forum and you will find similar questions.


I am not able to find please can you explain me both i will be grateful
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
You can find the JavaRanch journal article by clicking the underlined text in my previous post.
monis raza
Ranch Hand

Joined: Sep 05, 2011
Posts: 38

Campbell Ritchie wrote:You can find the JavaRanch journal article by clicking the underlined text in my previous post.


yeah i found it thanks
monis raza
Ranch Hand

Joined: Sep 05, 2011
Posts: 38

Campbell Ritchie wrote:
monis raza wrote: . . . am i deciphering it correctly ?
Afraid not.


i am afraid it is saying the same thing

In this case, we actually end up with a slightly different behavior because of the keyword "new." In such a case, references to the two String literals are still put into the constant table (the String Literal Pool), but, when you come to the keyword "new," the JVM is obliged to create a new String object at run-time, rather than using the one from the constant table.

In such a case, although the two String references refer to String objects that contain the same data, "someString", they do not refer to the same object. That can be seen from the output of the program. While the equals() method returns true, the == operator, which checks for referential equality, returns false, indicating that the two variables refer to distinct String objects.

Once again, if you'd like to see this graphically, it would look something like this. Note that the String object referenced from the String Literal Pool is created when the class is loaded while the other String object is created at runtime, when the "new String..." line is executed.





5 objects
monis raza
Ranch Hand

Joined: Sep 05, 2011
Posts: 38

Ulf Dittmer wrote:Note that the answer could also be "hundreds" or possibly "thousands", because the JVM will create a great many objects on its own while it runs this code.


no i am saying how many objects are there at runtime only by looking at code
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Runtime only? That is easy enough. But it isn't 3 and it isn't 5.
monis raza
Ranch Hand

Joined: Sep 05, 2011
Posts: 38

Campbell Ritchie wrote:Runtime only? That is easy enough. But it isn't 3 and it isn't 5.


then how many

and when class is loaded
I am not saying how many string objects but how many objects
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42374
    
  64
Just to confuse matters further: Given that the code does not do anything with any of the strings, it may well be that that all the code is optimized away by the compiler, so that no String objects are created at all.
monis raza
Ranch Hand

Joined: Sep 05, 2011
Posts: 38

Ulf Dittmer wrote:Just to confuse matters further: Given that the code does not do anything with any of the strings, it may well be that that all the code is optimized away by the compiler, so that no String objects are created at all.

monis raza
Ranch Hand

Joined: Sep 05, 2011
Posts: 38

I was reading this SCJP-Kathy sierra book(from which question originates) and i am quoting from that book page 432

Creating New Strings
Earlier we promised to talk more about the subtle differences between the various
methods of creating a String. Let's look at a couple of examples of how a String
might be created, and let's further assume that no other String objects exist in the
pool:
String s = "abc"; // creates one String object and one
// reference variable
In this simple case, "abc" will go in the pool and s will refer to it.
String s = new String("abc"); // creates two objects, Line X
// and one reference variable
In this case, because we used the new keyword, Java will create a new String object
in normal (nonpool) memory, and s will refer to it. In addition, the literal "abc" will
be placed in the pool.




why does it says in line X (in comments) that it creates two objects
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
That is not clear. If you put those two lines together, their behaviour is different. In the case of the line marked X, there can only be two objects if it is executed by a different JVM from that which executes the earlier line. You need to be clear. If you execute all those lines together that line X will not create two objects. What is more, if you assume that the first line creates one String object and the second two String objects, and apply it to your example, you will get a completely wrong answer.

Let us get back to your original posting. Let us confine ourselves to String objects which can be seen in the code. Tell us how many such String instances are created on the heap when that code is loaded, as opposed to the many objects which are created as parts of the JVM. Then tell us how many String objects are created when the code is executed.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
And assume you execute the code with this instruction, and nothing else:-
java agnitio.TestString
monis raza
Ranch Hand

Joined: Sep 05, 2011
Posts: 38

Campbell Ritchie wrote:That is not clear. If you put those two lines together, their behaviour is different. In the case of the line marked X, there can only be two objects if it is executed by a different JVM from that which executes the earlier line. You need to be clear. If you execute all those lines together that line X will not create two objects. What is more, if you assume that the first line creates one String object and the second two String objects, and apply it to your example, you will get a completely wrong answer.

Let us get back to your original posting. Let us confine ourselves to String objects which can be seen in the code. Tell us how many such String instances are created on the heap when that code is loaded, as opposed to the many objects which are created as parts of the JVM. Then tell us how many String objects are created when the code is executed.



when the class is loaded 3 string objects are there on the heap . one object by s1 and s2 and two by s3 and s4. I presume at runtime they will again be 3
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Wrong. At class loading time there is one String object, because all the String literals are identical.
At runtime each of the new String(...) calls will create one object, making another two. Now you have three.

Prajakta Acharya gave the correct answer yesterday and you appear to have overlooked it.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
When did "loading time" become something separate from "runtime"? That's very confusing. Loading is part of run time. Different classes can be loaded at different times, all during run time.

I think it would be more useful to say that one instance is created when the class loads, and two more are created when the main() method is executed. All of which occurs during run time.
monis raza
Ranch Hand

Joined: Sep 05, 2011
Posts: 38

Campbell Ritchie wrote:Wrong. At class loading time there is one String object, because all the String literals are identical.
At runtime each of the new String(...) calls will create one object, making another two. Now you have three.

Prajakta Acharya gave the correct answer yesterday and you appear to have overlooked it.



Thanks for your time . Now i understood
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Mike Simmons wrote:When did "loading time" become something separate from "runtime"? That's very confusing. Loading is part of run time. Different classes can be loaded at different times, all during run time.

I think it would be more useful to say that one instance is created when the class loads, and two more are created when the main() method is executed. All of which occurs during run time.
I think you are correct.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How many objects are being created here in this code