Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String Objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String Objects" Watch "String Objects" New topic
Author

String Objects

Ankita Jain
Greenhorn

Joined: Sep 12, 2008
Posts: 13

I am very confused about how many total objects will be created within method main() including and excluding lost objects.
AND
if LINE 1 is changed to final String s="abc"; then how many objects will be there?
Please help!!!
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Ankita Jain wrote:


String s="abc"; // LINE 1

1) Here "abc" literal created at the time of class loading.

String s1=s+"def";
2) "def" at class loading time
3) "abcdef" when this method is called then means when you run this code.

String s2=s+"def";
"def" is literal and already in the literal pool, so no new "def" this time
4) "abcdef" created again at runtime.

System.out.println(s1.equals(s2));
System.out.println(s1+" "+s2);
5) " " will be created in literal pool at the time of class loading.
6) abcdef abcdef at the time of running this code.

So I think total 6 objects.


SCJP 6
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Ankita Jain wrote:

I am very confused about how many total objects will be created within method main() including and excluding lost objects.


If you searched this forum , you'll find out this question has been answered many times.

Ankita Jain wrote:
AND
if LINE 1 is changed to final String s="abc"; then how many objects will be there?
Please help!!!


No effects, the object count will be same..


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
if LINE 1 is changed to final String s="abc"; then how many objects will be there?
Please help!!!


final String s="abc"; // LINE 1
1) "abc" at class loading time
String s1=s+"def";
2) "abcdef" at class loading time

String s2=s+"def";
"abcdef" is already in pool.
System.out.println(s1.equals(s2));
System.out.println(s1+" "+s2);
3) " " at class loading time
4) abcdef abcdef when you run this code.

So I think 4 objects here. I could be wrong, let others give their inputs.
Rafael Angarita
Ranch Hand

Joined: Jan 09, 2009
Posts: 67
I think 4 objects are created too. String literals aren't really objects right?


Rafael Angarita.
SCJP 6.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
String literals in literal pool are actually reference to String objects in heap, I have given 4 objects for second question, for first question I have counted 6 objects.
Ankita Jain
Greenhorn

Joined: Sep 12, 2008
Posts: 13
Sagar
this is not the same question....

What if first line is final String s="abc";
then how many objects? ???

and

No of objects creation is effected by the fact that method iis static or non static???
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
No of objects creation is effected by the fact that method iis static or non static???


No.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952


Where are Ruben and Ankit?

I have doubt here in LINE 2, whether "def" and "abcdef" will be created separately at the time of class loading or only "abcdef" will be created as here s+"def" is constant expression since s is declared final.

Put your inputs here.


Raza Khan
Greenhorn

Joined: Nov 19, 2008
Posts: 15
Hi Ankita are you a btech?
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Here I am Punit. Unfortunately I am not sure either. Maybe there is a way to devise a test to figure it out.

Do you think this would work? (Just guessing, its really late here and my brain is fried :lol: )
final String s="abc"; // LINE 1
String s1=s+"def"; //LINE 2
boolean b = (s1 == s1.intern()); // If string was created at loading time, b will be true (Maybe)


All code in my posts, unless a source is explicitly mentioned, is my own.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

Well I think that since s is final, so now "def" will not be created. only the String "abcdef" is created...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Hmmm both came very early great
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Yeah, Ankit you are probably right. I am going to let you and Punit do some detective work on this, unfortunately it's time to go for me. Good luck with it, I will check the results first thing in the morning.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

Well I'm not sure if the SCJP exam test this, but I said that "def" will not be created because I wrote these lines into a program

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

And then I decompiled it to find this

String s1 = "abcdef"

Since the string s was final so it's value was replaced by the compiler. Not only replaced, the compiler also concatenated "abc" and "def". That was the reason for my answer. But still I don't think that decompilation can be trusted for SCJP...
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Ruben Soto wrote:Yeah, Ankit you are probably right. I am going to let you and Punit do some detective work on this, unfortunately it's time to go for me. Good luck with it, I will check the results first thing in the morning.


Ok good night Ruben
Ankita Jain
Greenhorn

Joined: Sep 12, 2008
Posts: 13
According to me without final keyword there should be 4 objects....am i right or wrong???
1. abc
2. def
3. abcdef
4 abcdef
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Ankit I think we are on the right track.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
According to me without final keyword there should be 4 objects....am i right or wrong???
1. abc
2. def
3. abcdef
4 abcdef



No 6 objects as I explained above.

1)"abc"
2) "def"
3) "abcdef"
4) "abcdef"
5) " "
6)"abcdef abcdef"
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

Ruben Soto wrote:
final String s="abc"; // LINE 1
String s1=s+"def"; //LINE 2
boolean b = (s1 == s1.intern()); // If string was created at loading time, b will be true (Maybe)


It's not about "abcdef" being created here. It will be created so b WILL be true. We need to find if "def" is created in the String pool...
sunil langeh
Ranch Hand

Joined: Sep 04, 2007
Posts: 88
Well friends, About whole discussion i am not clear about the first point, As far as my knowledge is concerned String objects are implicitly final i.e it can't be changed once it created so i don't think so that using final it will create any impact on s ...Am i right ? if not please clear it to me...


Thanks
Sunil (SCJP 5)
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
sunil langeh wrote:Well friends, About whole discussion i am not clear about the first point, As far as my knowledge is concerned String objects are implicitly final i.e it can't be changed once it created so i don't think so that using final it will create any impact on s ...Am i right ? if not please clear it to me...


No Sunil, Final String and Final int are considered constant by compiler, so they are executed at compile time only when .java is compiled to .class.
Ankita Jain
Greenhorn

Joined: Sep 12, 2008
Posts: 13
Punit
that means objects are created in println statement also???
I was not aware of this...

But then why "abcdef abcdef" ? they are different objects..aren't they??
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Ankita Jain wrote:Punit
that means objects are created in println statement also???
I was not aware of this...

But then why "abcdef abcdef" ? they are different objects..aren't they??


JVM will concatenate all different objects and will make one String object.
Just like

String s2=s+"def";
Ronald Schild
Ranch Hand

Joined: Jun 09, 2008
Posts: 117
Ankita Jain wrote:


Class loading:

(L1) String object with content "abc" created and referenced by the String Constant Pool (SCP).
(L2) String object with content "def" created and referenced by the SCP.
(L5) String object with content " " created and referenced by the SCP.

3 String objects are created when the class that contains this code is loading.

Execution of main(String[]):

(L1) String reference s assigned to String object with content "abc".
(L2) String object with content "abcdef" created. String reference s1
assigned to this String object.
(L3) String object with content "abcdef" created. String reference s2
assigned to this String object.
(L5) String object with content "abcdef " created. String object with
content "abcdef abcdef" created.


4 String objects are created after the fifth line of the main(String[]) has completed.
String objects with content "abcdef " and "abcdef abcdef" are not assigned to a reference
and are considered eligible for the GC after line 5.

If s would be a final reference, the expressions of line 2 and 3 would be constant
expressions. The String object with content "abcdef" would have been created at class
load time and would have been referenced by the SCP. s1 and s2 would be set to refer
to this object as L2 and L3 would execute.

[e]: Tag fix, typo.
[e]: The red part - the compiler uses a StringBuffer class (see posts below) - now I don't think "abcdef " is created.


Java hobbyist.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
(L5) String object with content "abcdef " created.
String object with content "abcdef abcdef" created.


I did not get this one Ronald.
Ronald Schild
Ranch Hand

Joined: Jun 09, 2008
Posts: 117
Punit Singh wrote:
(L5) String object with content "abcdef " created.
String object with content "abcdef abcdef" created.


I did not get this one Ronald.


Multiple things happen at s1 + " " + s2. First s1 + " " is handled. Next "abcdef " + s2 is handled.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
hmmm, it's tricky here, you are right Ronald.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Yeah, this is tricky. I was doing some testing, and I think I can confirm what Ankit said about "xyz" not being created here:

It seems that when java is parsing the code it will do some processing by joining compile-time string constants, so "xyz" is never created as an object in the heap, even though it appears as a literal string in the code.

I also think that Ronald is correct, but it appears by looking at the heap dump (that's what I used to test) that when you do something like:

even though it's true that "uvwdef " must be created as an intermediate step, it is garbage-collected immediately, as there is no trace of it in the dump file.
Also, notice that I had to replace the println() statement by the String assignment statement, because in that case, every string created inside the println() statement seems to get garbage collected immediately.

Here's the code that I used to test:


Then, you do this in the command line:
javac StringTest2.java
java -Xrunhprof:format=b,file=snapshot1.hprof StringTest2 // Creates the dump file
jhat snapshot1.hprof // Starts a server with the dump view

Then, you can start any browser and enter the address http://<your_host_name>:7000

From there you can see the contents of the heap at the time exit() was called. I had to insert the exit() call at the end of the file, because if you don't it appears that all the objects you create in your main method get garbage collected and don't appear in the dump file.

When you browse the dump contents, click on "Show instance counts for all classes (including platform)", then from there select X instances of class java.lang.String

Only do this if you have some time to waste though.

Alpesh Rathod
Ranch Hand

Joined: Jan 06, 2009
Posts: 41
jhat snapshot1.hprof

This doesnt work when i type it in the command line...


Thanks,
Alps
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Alpesh, what do you get when you type:
jhat -version
Are you on Windows or Linux? I am on Linux.
Alpesh Rathod
Ranch Hand

Joined: Jan 06, 2009
Posts: 41
Hey I am on Windows..
When i type jhat:snapshot.hprof...it gives me following error..

The same is for jhat -version...

'jhat' is not recognized as an internal or external command,
operable program or batch file.

I think i need to have a "Java Heap Analysis tool" which i don't have currently...so could you please help me in getting that..
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
I didn't have to install anything, and I don't use Windows. Try and see if you can find this in Sun's website. Or maybe someone using Windows can help you find it and set it up.
Alpesh Rathod
Ranch Hand

Joined: Jan 06, 2009
Posts: 41
Hey i got it ....got the jar and i have also made it run...but i dont know now how to actually work with it...very wierd things i can see..as never seen before...
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Great that you got it working. So can you open the browser and go to http://localhost:7000? You should probably replace localhost by your specific computer host name.
Alpesh Rathod
Ranch Hand

Joined: Jan 06, 2009
Posts: 41
Yes i did it...now what i can see is something i haven't seen before....now how do i get to kno wat things to work with..

Actually when i click on class StringTest2 in that in instances when i click on include subclasses...i get to see that there are 0 instances occupying 0 bytes....

I am not getting it......how to actually work with it....Help !!!
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
This is what worked for me:
When you browse the dump contents, click on "Show instance counts for all classes (including platform)", then from there select X instances of class java.lang.String

Experiment and see what you find. It took me a while to find my way around it.
Alpesh Rathod
Ranch Hand

Joined: Jan 06, 2009
Posts: 41
There are 639 instances of class java.lang.String....now how do i check which instance has been created by our class....very sloggy thing...
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Easy: You need to create strings with specific contents, and then search in your browser. For example, in Firefox Edit, Find. :lol:
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Excellent Stuff Ruben, I did all the things you said.
I ran this program:



PunitSoto (24 bytes)
Ruben (24 bytes)
PunitSingh (24 bytes)
PunitSoto Ruben (24 bytes)



I was searching 6 objects, but got only 4.
I did not get these two:
final String s = "Punit"; //1 Punit
//5 PunitSoto space
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: String Objects