• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem in SCJP kathy sierra Book  RSS feed

 
Randika Isuru
Ranch Hand
Posts: 38
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,, first of all i thanks all of you for help me to get answer to my problem. In my first post i get clear answer for my problem. For that thanks again all of member in this forum. and also thanks all of admins in this forum you are doing such a great work for beginners like me.

and this is my second problem. this problem related to SCJP kathy sierra book chapter 3: page number 245 -246

In page number 245 there is a code like this



The book tell tell this example Produces the output like this..

different object
meaningfully equal

this is OK there is a no doubt...

page number 246 there is a code something like this...



book tell us to above code produce output something like this...

Same Object
meaningfully equal


this is also OK.

but when i run this below my code i got output is

different object
meaningfully equal
meaningfully equal


what is the wrong i'm doing? where is the "Same Object" statement
or there are there anything i'am misunderstand? please can anyone help me? to get clear this problem

This is my code...


 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's a difference between your second and third code snippet: in the second one, you use the value 10 for i3 and i4, and in the third one, you use the value 1000.

First of all, you have to understand that autoboxing is used here. That means that the compiler automatically translates:

to this:

The second part is that the method Integer.valueOf(...) handles some values specially. If the value you pass to it is between -128 and +127, it will not create a new Integer object, it will instead return an existing Integer object from a cache. If the value is outside this range, it will create a new Integer object with the specified value.

That means that if you do Integer.valueOf(10) you'll always get the same Integer object back - that's why i3 and i4 in your second code snippet refer to the same object.

But if you do Integer.valueOf(1000) you'll get a new, distinct Integer object each time you call the method - that's why i and i1 in your first snippet, and i, i2, i3 and i4 in your third snippet all refer to different Integer objects.

This kind of question has been asked and discussed many times before on the forums. If you do a search, you'll find a lot of answers and discussions.
 
Randika Isuru
Ranch Hand
Posts: 38
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh!! actually i'am very sorry for not searching past problem related to this problem. before the post this problem.. in next time i 'll do..

thank you very much Jesper de Jong for your help and actually now i get clear understand about that because of you

yes i use the value of 1000 for my code because i think value is not problem for my output. for that reason i hope my output should be get "same Object" as well..

but now i know my thought was wrong. value the problem in this case..

Thank you..
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote: . . . If you do a search, you'll find a lot of answers and discussions.
You also find that the behaviour of values >127 is not defined; there is a user option which can cause those values to be cached like -128…+127.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!