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 !!!
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.
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 !!!
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.
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" .
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?
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 ...
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.
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
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...
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
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.
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.
Jeanne Boyarsky
,
author & internet detective
staff
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.
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.
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.