Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Autoboxing Assignment - Head First Java  RSS feed

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On page 291 of Head First Java, they discuss autoboxing and specify that references can be assigned to either the wrapped objected or the primitive type.  However, on the picture, they have a double reference defined with Integer and Int types being thrown in.  Is this an error or have I misunderstood the concept?
20190208_145204.jpg
[Thumbnail for 20190208_145204.jpg]
 
Saloon Keeper
Posts: 5485
55
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure exactly what part you're stuck on. In the following only one of these is a reference. What do you mean by a double reference?
 
Marshal
Posts: 62881
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried the code at the top of that page? What happens?
 
Yan Digilov
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, sorry yall, I am talking about the section ABOVE the sharpen your pencil piece.  I mean the one titled "Assignment".  It goes "Double d = x;" and there is an Integer and an int pointing to it...
 
Ranch Hand
Posts: 483
23
AngularJS Eclipse IDE Firefox Browser Java Spring VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Yan Digilov wrote:Ah, sorry yall, I am talking about the section ABOVE the sharpen your pencil piece.  I mean the one titled "Assignment".  It goes "Double d = x;" and there is an Integer and an int pointing to it...


I think taking a step to compile that simple line is not a big deal. Either it's an error or not, compiler is the best one which will tell you about it, it's one of the best core task assigned to it. but it's well likely for a compiler to not caught the errors which will really happen during run time as those are generated dynamically. in that case it's again not a following big step to run that program to be sure about the behaviour of code is, as i expected.
Now experiment the same code with a java as mentioned in the book and think about what really goes behind the scene?

Ask yourself:

1.) if not compiled, why really it didn't compile, what's wrong have you done there...
2.) if compiled but not ran, what's really something that compiler has not really caught. and the most important the error, why does it appear?
3.)if compiled and run successfully,  why can i assign int to a Double reference. what really is going behind the scene? Is there any optimization added by compiler, if so then what are they.

Tell us your views first, then we will see if their are any problems.
 
Campbell Ritchie
Marshal
Posts: 62881
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look through the Java® Language Specification, you will find that boxing conversion is possible, or widening conversion, but not both together. Please check the book's website and see whether any errors have been reported for that page.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!