Forums Register Login

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

+Pie Number of slices to send: Send
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 :-
+Pie Number of slices to send: Send
Question ID 1:



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

+Pie Number of slices to send: Send
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:


+Pie Number of slices to send: Send


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.

+Pie Number of slices to send: Send
I think the answer is
true
false

If magicSunOfficialSecretNumber value is less than 128 then answer will be

true
true
+Pie Number of slices to send: Send
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!!!
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
Yess please do comply to java ranch rules ofcourse.

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

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send

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.
+Pie Number of slices to send: Send
 

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 !!!
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
I think the answer will be
true
true
+Pie Number of slices to send: Send
 

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 ...
+Pie Number of slices to send: Send
I also don't understand the explanation of bytes...
please somebody elaborate it...
+Pie Number of slices to send: Send
 

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

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Thanks Abhinav.
Now I got it .....
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
+Pie Number of slices to send: Send
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...
+Pie Number of slices to send: Send
thanks ulf ... you solved my confusion ...
+Pie Number of slices to send: Send
although it was a good question to make our concepts solid
+Pie Number of slices to send: Send
Did anyone liked my variable name "magicSunOfficialSecretNumber" ??? or it was silly ???
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
 

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......
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Hello Jeanne,

I got it from the journal e-mail.

Sincerely,
Mike
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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...
+Pie Number of slices to send: Send
Yes, this question is known to me from the June JavaRanch Journal email. Thanks to all.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
That's alright. It's a great forum. Thanks.
What are your superhero powers? Go ahead and try them on this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 6566 times.
Similar Threads
ID:5 SCJP Question of the day : Friday June 11 2010
ID:6 SCJP Question of the day : Saturday June 12 2010
K & B, SCJP 5 - Errata: Updated 7/28/06
How I got 94% in the SCJP exam...
ID:2 SCJP Question of the day : Thursday June 10 2010
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 19, 2024 00:17:57.