• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What the output related to string

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
3 objects, search this question, it has been asked lots of time.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
5
 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought there are only 2 objects created.i.e at line 14 and 15.
Where is the third object :?:
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
see this link
 
Srinivasan Jayakumar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi nancy gurudatta,
how do you say 5 objects can you explain it
 
nancy gurudatta
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As far as i know Strings created will be:
"Fred",
"47",
"Fred47"
"ed4"
"ED4"

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

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


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


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

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

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?
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



Here in Line1 s4 should be created at compile time, as s1, s2 both are created at compile time???
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

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

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
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
EDITED
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Now run this code, and tell me what's happening here and why?
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmm..Nothing much ,carry on with your good work..


 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
James did you run this one ?

 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic