aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes What the output related to string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "What the output related to string" Watch "What the output related to string" New topic
Author

What the output related to string

Srinivasan Jayakumar
Greenhorn

Joined: Oct 27, 2007
Posts: 3
Hello Friends,
i have preparing for SCJP 5.0, i had a doubt the following question
Given:
11. public String makinStrings() {
12. String s = “Fred”;
13. s = s + “47”;
14. s = s.substring(2, 5);
15. s = s.toUpperCase();
16. return s.toString();
17. }
How many String objects will be created when this method is invoked?
A. 1
B. 2
C. 3
D. 4
E. 5
F. 6
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
3 objects, search this question, it has been asked lots of time.


SCJP 6
nancy gurudatta
Greenhorn

Joined: Jan 02, 2009
Posts: 3
5
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

I thought there are only 2 objects created.i.e at line 14 and 15.
Where is the third object :?:


SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
see this link
Srinivasan Jayakumar
Greenhorn

Joined: Oct 27, 2007
Posts: 3
Hi nancy gurudatta,
how do you say 5 objects can you explain it
nancy gurudatta
Greenhorn

Joined: Jan 02, 2009
Posts: 3
As far as i know Strings created will be:
"Fred",
"47",
"Fred47"
"ed4"
"ED4"

Please correct me if wrong
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
I thought there are only 2 objects created.i.e at line 14 and 15.
Where is the third object



James, what about the object created on LINE13?
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952


yes line 13 will be created as runtime not at compile time, as String s is not compile time constant.
If it was final String s="Fred", then it would have been created at compile time.

For ex run these codes:



Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Nancy you are wrong, as question is asking about objects created only when this method is called, not total objects, so deduct your compile time objects, count only runtime objects.
nancy gurudatta
Greenhorn

Joined: Jan 02, 2009
Posts: 3
final String s="abc";//created at compile time
String s1=s+"def";//created at compile time as it is constant expression
String s2=s+"def";//created at compile time as it is constant expression
System.out.println(s1==s2);//true

Hi Punit,
Tell Me..
There will be 2 objects created in above code..
Am I right???
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952


In this code 3 objects will be called as
line 12 is : String s="Fred"; not final String s="Fred"; so it is not a compile time constant that's why
line 13 s=s+"47"; will not be created at compile time, it will be created at run time, when method will be invoked using new operator.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
nancy gurudatta wrote:final String s="abc";//created at compile time
String s1=s+"def";//created at compile time as it is constant expression
String s2=s+"def";//created at compile time as it is constant expression
System.out.println(s1==s2);//true

Hi Punit,
Tell Me..
There will be 2 objects created in above code..
Am I right???


Ya you are right here, 2 objects will be created here.
Michael Tolenruc
Greenhorn

Joined: May 30, 2005
Posts: 13
Srinivasan Jayakumar wrote:Hello Friends,
i have preparing for SCJP 5.0, i had a doubt the following question
Given:
11. public String makinStrings() {
12. String s = “Fred”;
13. s = s + “47”;
14. s = s.substring(2, 5);
15. s = s.toUpperCase();
16. return s.toString();
17. }
How many String objects will be created when this method is invoked?
A. 1
B. 2
C. 3
D. 4
E. 5
F. 6


So if we're going to change the question to "How many String Objects will be created?" will be the answer is 5?


Roses are red, Violets are blue. Strings are immutable, Wrappers are too. (HFJ)

SCJA 1.0 (86%), SCJP 6.0 (91%)
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
yes.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14352
    
  22

Welcome to JavaRanch.

When you post a question copied from a book, mock exam or other source, you are required to quote your sources. This question has been asked many times before, but next time please mention where you copied the question from.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
Can anyone explain about String objects created at runtime and compile time? I am really getting confused.


Here,
String s3="hel"+"lo"; // is it a runtime/ compile time object?
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Abhi vijay wrote:Can anyone explain about String objects created at runtime and compile time? I am really getting confused.


Here,
String s3="hel"+"lo"; // is it a runtime/ compile time object?


It will be created at compile time Abhi, as it contains "hel" and "lo" both are constant String literal.

If you says

String s="Abhi";
String s3="hel"+"lo "+s;
Then "hello Abhi" will not be loaded at compile time as we are using String s that is not a compile time constant.

But if you say:
final String s="Abhi";
String s3="hel"+"lo "+s;
Then "hello Abhi" will not be loaded at compile time as we are using final String s that is here now a compile time constant.




Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
Punit,



Here in Line1 s4 should be created at compile time, as s1, s2 both are created at compile time???
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

It will be created at compile time Abhi, as it contains "hel" and "lo" both are constant String literal.


I thought hel and lo are compile time literals (literal pool).
And during runtime s3 gets a new object hello :?:
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
James Tharakan wrote:
It will be created at compile time Abhi, as it contains "hel" and "lo" both are constant String literal.


I thought hel and lo are compile time literals (literal pool).
And during runtime s3 gets a new object hello :?:


No s3 will not get new hello, hello will be assigned to it at compile time only. Try this code


If both s5 and s5.intern(), referring to same Object than it will return true otherwise false.

Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Abhi vijay wrote:Punit,



Here in Line1 s4 should be created at compile time, as s1, s2 both are created at compile time???


No abhi s4 will not created at compile time, the reason is that s1 and s2 are not declared final, so they will not be treated as compile time constant.

Run this code if it returns true than s1+s2 executed at compile time and s4 assigned reference from the pool.
If false then s4 is assigned new reference at runtime.

Now run this code. And see the output.


Now tell me what you understand from the codes.
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

EDITED
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952


Now run this code, and tell me what's happening here and why?
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Yeah now i think i was wrong .
Hope i wont run into such a confusion again.
And i have erased my previous post to avoid others getting confused.

I wish String class is not a final class. All this confusion could be avoided.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
James Tharakan wrote:
I wish String class is not a final class. All this confusion could be avoided.


I did not get meaning of this statement.
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

hmm..Nothing much ,carry on with your good work..


Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
James did you run this one ?

James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Yeah , that is clear because of the final modifier,which is compile time constant,so s4 is also done at compile time.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Great
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What the output related to string