• 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

ID:2 SCJP Question of the day : Thursday June 10 2010

 
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After discussion with some savvy guys , i am taking an Initiative to start a thread which has following features :-

1) A question would be asked by an Author of the thread.

2) You are required to answer it with reasoning, and without actually executing it in your compiler because it is for your help.

3) At the end of the day , i request SCJP cleared and other savvy java guys to answer it in their words so that it could help SCJP preparing guys like me.

NOTE:- If you ask a question, i request you to follow the following :-

1) Start New topic and do not post your question here.

2) Please use the format for the Subject line of the Topic or thread as follows:-

i.e. ID: <Your Question number> SCJP Question of the day : <TIME> -----<Your Name>

like , it is my second question and my name is Sahil Rally so in accordance with the above format i used the following Subject line for my topic.

ID:2 SCJP Question of the day : Wednesday June 10 2010 ---Sahil Rally .


NOTE 2: - Make as challenging Questions as you can while reading the topics and i really hope that this Idea would Rock !!!

Thanks !!!

So here goes my Question :-
 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


What is the output , tell with the reason ???
 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
NO one answered yet !!!
Is it so easy !!!
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I does not compile



That shouldn't work. You can't change object reference if it's marked final.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please leave the name out of the subject - it's long enough as it is. What's more, the name is visible wherever the topic title is visible, so it's redundant to begin with.
 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Martin Vanyavchich wrote:I does not compile



That shouldn't work. You can't change object reference if it's marked final.




No dear, you can always initialize once the final variables, yes after



you cannot assign anything to s13.

It would compile !!! Now try !!!

 
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First comparison should be true (s1 and s2 reference the same object).
Other comparisons should be false (s5, s8, s11, s15 refrence different objects, concatenation invokes String constructor)

I fear it's not that simple though ; - )
 
Martin Vanyavchich
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first one is true, that is sure The second and the last one are basicaly the same (whith the exception of the trick I have fallen for , you got me there Sahil) and are both false. The third and fourth ... I'll go with final references beeing as good as string literals, therefore true. As Peter mentioned, concatenation returns a new object, therefore the fourth is false.

To sum up:
true
false
true
false
false
 
Ranch Hand
Posts: 384
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


good one ... I almost fell in the trap with final members playing it the rough way ...

anyways ... the answer as per i think is

1. true // for both are literals
2. false // concatenation invokes constructor
3. true // both are final hence can be treated as constants or literals
4. false
5. false
 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lalit Mehra wrote:

good one ... I almost fell in the trap with final members playing it the rough way ...

anyways ... the answer as per i think is

1. true // for both are literals
2. false // concatenation invokes constructor
3. true // both are final hence can be treated as constants or literals
4. false
5. false




Please elaborate on the explanation for number 3. Why would s8 point to the same object as s1 if s6 and s7 are both final?
 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its all about "Compile time constants"

Since s6 and s7 are both final, hence both s6 and s7 are compile time constants. The sum of two compile time constants is also compile time constant.

Hence s1 and s8 would point to same String literal.

Cheers!!!
 
Ranch Hand
Posts: 63
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

According to me s1 & s2 pointing to the same String objects in String pool.
as Strings are immutable for s1 & s2 , there may not be two String objects that containing "SCJP".
s1 & s2 would refer a same location.

so s1==s2 is true.

in other cases it may not be true.

 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
answer is as follows and with the following reasons
1. true // both strings refer the to String literal pool object "SCJP"
2. false// this is because when one of the operand is non final, then concatenation is done at run time and hence the + operator creates a new string and hence s1 and s5 refer to different objects
3.true // here both are final hence compile time constants hence concatenation is done at compile time and hence the variable s8 refers to the string in the constant pool referred by s1
4.false// here both the strings are non final and hence not the compile time constants hence the concatenation is done at the run time and hence the "+" operator creates the new string
5.false // the same reason that is for the third S.O.P.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1 - 5. false // Strings are immutable in order to compare their values you need to use a.equals(b).
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I comply with the result and reasoning which is submitted by Prasad Kharkar for this excellent question. More Details
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic