ID:1 SCJP Question of the day : Wednesday June 9 2010
Post by:Sahil Kapoor
, Ranch Hand
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 :-
Post by:Sahil Kapoor
, Ranch Hand
Question ID 1:
What is the output of the above code ???
Give your reasoning ???
Post by:Sandra Bachan
, Ranch Hand
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:
Post by:Henry Wong
, Sheriff
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.
Post by:Pramod P Deore
, Ranch Hand
I think the answer is
true
false
If magicSunOfficialSecretNumber value is less than 128 then answer will be
true
true
Post by:Sahil Kapoor
, Ranch Hand
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!!!
Post by:Devaka Cooray
, Marshal
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.
Post by:Sahil Kapoor
, Ranch Hand
Yess please do comply to java ranch rules ofcourse.
and by default if no source is mentioned it means " Its our own creation ". Thanks !!!
Post by:Martin Vanyavchich
, Ranch Hand
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?
Post by:Abhinav Yadav
, Ranch Hand
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.
Post by:Sahil Kapoor
, Ranch Hand
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 !!!
Post by:Martin Vanyavchich
, Ranch Hand
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?
Post by:Rajesh Lohani
, Greenhorn
I think the answer will be
true
true
Post by:Lalit Mehra
, Ranch Hand
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 ...
Post by:Rajesh Lohani
, Greenhorn
I also don't understand the explanation of bytes...
please somebody elaborate it...
Post by:Henry Wong
, Sheriff
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
Post by:Abhinav Yadav
, Ranch Hand
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.
Post by:Rajesh Lohani
, Greenhorn
Thanks Abhinav.
Now I got it .....
Post by:Lalit Mehra
, Ranch Hand
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.
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...
Post by:Lalit Mehra
, Ranch Hand
thanks ulf ... you solved my confusion ...
Post by:Abhinav Yadav
, Ranch Hand
although it was a good question to make our concepts solid
Post by:Sahil Kapoor
, Ranch Hand
Did anyone liked my variable name "magicSunOfficialSecretNumber" ??? or it was silly ???
Post by:Lalit Mehra
, Ranch Hand
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
Post by:Sandra Bachan
, Ranch Hand
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......
Post by:suresh dasari
, Ranch Hand
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
Post by:pete reisinger
, Ranch Hand
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.
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.
Post by:Mike Peters
, Ranch Hand
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.
Post by:Jeanne Boyarsky
, Marshal
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
Post by:Mike Peters
, Ranch Hand
Hello Jeanne,
I got it from the journal e-mail.
Sincerely,
Mike
Post by:Unmesh Chowdhury
, Ranch Hand
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.
Post by:Ankit Garg
, Sheriff
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...
Post by:Unmesh Chowdhury
, Ranch Hand
Yes, this question is known to me from the June JavaRanch Journal email. Thanks to all.
Post by:Jeanne Boyarsky
, Marshal
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.
Post by:Unmesh Chowdhury
, Ranch Hand
That's alright. It's a great forum. Thanks.
Post by:autobot
You can thank my dental hygienist for my untimely aliveness. So tiny:
Thread Boost - a very different sort of advertising