• 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:1 SCJP Question of the day : Wednesday June 9 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>
like , it is my first question and my name is Sahil Rally so in accordance with the above format i used the following Subject line for my topic.

ID:1 SCJP Question of the day : Wednesday June 9 2010


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
Question ID 1:



What is the output of the above code ???
Give your reasoning ???

 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your idea rocks and as a fellow aspiring SCJP, would love to participate.

I will try to answer your question, without using compiler. Please let me know how I scored. My answers are commented out in the code below:


 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Question ID 1: hint... the Integer.valueOf(int) method is the method, used internally, when the compiler generates code for autoboxing. So... the answers here will be the same, as if autoboxing was used.

 
Ranch Hand
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the answer is
true
false

If magicSunOfficialSecretNumber value is less than 128 then answer will be

true
true
 
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
Please do not tell SUN peple that i know their "magicSunOfficialSecretNumber" .

Pramod P Deore : Yours Answer is Partially True.

Sandra Bachan : Your Answer is Wrong.

Henry Wong : I request you sir to devote some time in explaining the reason of the answers as i love your explanations and like me many other SCJP aspirants too must.


To all people : Please do post questions like me whenever you encounter any good question and please do comply with the Format as i explained above in order to preserve consistency. Thanks !!!



Keep trying !!!
Cheers!!!
 
Sheriff
Posts: 7134
1360
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sahil Rally wrote:Please do post questions like me whenever you encounter any good question....


+1 But don't forget to QuoteYourSources if a question is not yours.
 
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
Yess please do comply to java ranch rules ofcourse.

and by default if no source is mentioned it means " Its our own creation ". Thanks !!!

 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Something similar was discused in another thread not long ago. Pramod P Deore is right.

Pramod P Deore wrote:I think the answer is
true
false

If magicSunOfficialSecretNumber value is less than 128 then answer will be

true
true



I would just add, that (k == l) returns true if magicSunOfficialSecretNumber is within [-128, 127]


p.s. can there be more than one question of the day?
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Martin Vanyavchich

I would just add, that (k == l) returns true if magicSunOfficialSecretNumber is within [-128, 127]



I understood the ans to the 1st part i.e i==j is true but since value of SUNSECRETNO=143 which is greater than 128 then how does this helps in making a faulty comparison,and show (k==l) is false but answer should be true as both k and l hold the value 143.
 
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

I would just add, that (k == l) returns true if magicSunOfficialSecretNumber is within [-128, 127]



That what was missing in Pramod's answer.

p.s. can there be more than one question of the day?



I would really appreciate if someone comes up with his own questions. Ya We can have many on one day .
But please do comply the format rules and also please increment your Question ID by 1.



None has given the reason of answers yet....Though answer is Completely correct by "Martin Vanyavchich" .

Thanks Cheers !!!
 
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
I can't find the thread that had the explanation I think it was an older thread referenced in one of the newer ones. Is the answer somehow related to byte range?
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the answer will be
true
true
 
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

Martin Vanyavchich wrote:I can't find the thread that had the explanation I think it was an older thread referenced in one of the newer ones. Is the answer somehow related to byte range?



I agree ... there is not enough explanation given ... although the answer is true, false

it is somehow related to byte i know as one of my friends stated above... but i can't figure it out ...
 
Rajesh Lohani
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I also don't understand the explanation of bytes...
please somebody elaborate it...
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lalit Mehra wrote:

Martin Vanyavchich wrote:I can't find the thread that had the explanation I think it was an older thread referenced in one of the newer ones. Is the answer somehow related to byte range?



I agree ... there is not enough explanation given ... although the answer is true, false

it is somehow related to byte i know as one of my friends stated above... but i can't figure it out ...



Do a search in the forums on "integer cache". That is the feature which effects this, with autoboxing, and of course with the valueOf(int) method.

And the answer is ... true, followed by either true or false. Basically, the specification defines the range where the integer cache must take place. It doesn't define what will happen if it is outside the range. This means that it is theoretically possible to cache more than specification. In fact, that is what happens with the Sun JVM -- it caches the Long object which is not required by the spec. And it has a -XX switch to allow you to increase the range to cache.

Henry

 
Abhinav Yadav
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whenever boxing is applied , only one wrapper object exists in the program for primitive values of( boolean,byte,char and int or short):
1).boolean values true or false,
2).a byte,
3).a char in range \u0000 to \u007f,
4),and an int or short value in the range -128 and 127.

so for eg if a and b refer to two wrapper objects that box same value which is among one of those mentioned above ,then a==b is always true,i.e object and reference equality will give same result..
Let us take an example,

but in case if we violate one of the above 4 points ,for eg here let us take point 4,
and do

Thus this proves the result.
 
Rajesh Lohani
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Abhinav.
Now I got it .....
 
Lalit Mehra
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
Well Thanks, but don't you think since both k and l have the same value they will be truncated to same value after autoboxing too.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See http://faq.javaranch.com/java/JavaIntermediateFaq#integerAutoBoxing for an explanation.
 
Rajesh Lohani
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both have the same values but both are treated as different object(Because range is outside -128 to 127) as Abhinav stated above.
== compare only for the objects so return false and
in case of equals, it checks for only value and both having same value so it returns true...
 
Lalit Mehra
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
thanks ulf ... you solved my confusion ...
 
Abhinav Yadav
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
although it was a good question to make our concepts solid
 
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
Did anyone liked my variable name "magicSunOfficialSecretNumber" ??? or it was silly ???
 
Lalit Mehra
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

Sahil Rally wrote:Did anyone liked my variable name "magicSunOfficialSecretNumber" ??? or it was silly ???





it is a good one until the sun officials do not complain
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Martin Vanyavchich wrote:Something similar was discused in another thread not long ago. Pramod P Deore is right.

Pramod P Deore wrote:I think the answer is
true
false

If magicSunOfficialSecretNumber value is less than 128 then answer will be

true
true



I would just add, that (k == l) returns true if magicSunOfficialSecretNumber is within [-128, 127]


p.s. can there be more than one question of the day?




OK, now this makes sense. I didn't realize Integer.valueOf() was a method that returned an int, not an Integer object......
 
Ranch Hand
Posts: 121
Mac Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is really a good question, I am confused alot,
finally I got it.
as per my understanding if the value exceeds integer range value, it is stored in a different object, hence the weird result
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sandra Bachan wrote:

Martin Vanyavchich wrote:Something similar was discused in another thread not long ago. Pramod P Deore is right.

Pramod P Deore wrote:I think the answer is
true
false

If magicSunOfficialSecretNumber value is less than 128 then answer will be

true
true



I would just add, that (k == l) returns true if magicSunOfficialSecretNumber is within [-128, 127]


p.s. can there be more than one question of the day?




OK, now this makes sense. I didn't realize Integer.valueOf() was a method that returned an int, not an Integer object......



Integer.valueOf(int) returns Integer obect, but it caches frequently requested values (-128, 127), so numbers in this range (if they are equal) will return the same object.

java Integer 1.5.0 api

If I understand it correctly.
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is:
true
false

Basically, the behavior of the == or equals(Object o) is absolutely depended on the programmer who has written the class.

The equals(Object o) method in the boxed primitive is implemented as that it will return true if the two boxed primitive objects are logically equal.

And the == operator of the boxed primitive is implemented as usual, that is, if two references are refer to the same boxed primitive object on the heap then it returns true, otherwise it returns false. But the implementers of the boxed primitive classes have decided that, the following boxed primitive types: Integer, Short, Character, Byte and Boolean will behave slightly differently if their values are within the certain ranges. For example, for two Integer objects, == operator returns true if the values of these two object within the following range: -128 to 127. This is happened absolutely for using memory efficiently.
 
Ranch Hand
Posts: 67
Eclipse IDE Debian Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is:
true
unknown

My motivation for the second answer is that the language specification allows the interning of a wider range than the minimum range specified in the java language specification. So the SUN JVM may return false, but another JVM is allowed to return true for the second example.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mike, Umesh,
Welcome to JavaRanch!

I noticed that both of you posted in this thread today and have a low post count. Just wondering if you found out about this excellent question of the day series from the Journal or just stumbled upon it.

Thanks,
Jeanne
 
Mike Peters
Ranch Hand
Posts: 67
Eclipse IDE Debian Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Jeanne,

I got it from the journal e-mail.

Sincerely,
Mike
 
Unmesh Chowdhury
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jeanne,
Do you mean Unmesh? Is my reasoning is totally ridiculous? Sorry, this question is not read from any journal. This question is seen here first. My reasoning is reflected from the K&B book. And, what I’ve written is absolutely true for Sun JVM.

Thanks,
u.c.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Unmesh Chowdhury wrote:Do you mean Unmesh?


I'm quite sure Jeanne made a typo in your name so she means Unmesh.

Is my reasoning is totally ridiculous? Sorry, this question is not read from any journal. This question is seen here first.


Jeanne was not questioning your reasoning, she just wanted to know if you came to know about this topic from the journal or not...
 
Unmesh Chowdhury
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, this question is known to me from the June JavaRanch Journal email. Thanks to all.
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unmesh,
Sorry about the typo in your name. I knew someone named Umesh and never anyone named Unmesh so my fingers typed it subconciously.

It is good to know you and Mike found out about this from the Journal. Since it is the first Journal in this format, I'm trying to do some analysis on what worked.
 
Unmesh Chowdhury
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's alright. It's a great forum. Thanks.
 
reply
    Bookmark Topic Watch Topic
  • New Topic