• 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
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Autoboxing Assignment - Head First Java

 
Greenhorn
Posts: 9
  • 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]
 
Bartender
Posts: 5906
57
Eclipse IDE Firefox Browser MySQL Database VI Editor Java 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: 64680
225
  • 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: 9
  • 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: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • 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: 64680
225
  • 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.
 
Yan Digilov
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

praveen kumaar wrote:

Yan Digilov wrote:
Ask yourself:

1.) if not compiled, why really it didn't compile, what's wrong have you done there...


Tell us your views first, then we will see if their are any problems.



Well it definitely does NOT compile because there is a type mismatch.  I see it reported as an error by some readers, but they have not officially listed it in their erratum for the book.

 
Ranch Hand
Posts: 48
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Yan Digilov wrote: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?

assignment requires runtime check, so an integer object reference is assigned to int which is primitive type, autoboxing and unboxing can be done at while declaring the variable not at the assignment so error at runtime because if it not initialized it contain null
So null is assigned to int variable nullpointerexception
Put j=10;
Change it like this
Int i=j;
I think it works
 
Yan Digilov
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sohail, I was not talking about the "Sharpen Your Pencil" section...
 
praveen kumaar
Ranch Hand
Posts: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

sohail wrote:assignment requires runtime check, so an integer object reference is assigned to int which is primitive type, autoboxing and unboxing can be done at while declaring the variable not at the assignment so error at runtime because if it not initialized it contain null
So null is assigned to int variable nullpointerexception ...


Hi sohail, Autoboxing and Unboxing does applies to both case, initialization and assignment. The actual reason for the NullPointerException there is that: compiler produces(optimize) a code that actually looks something like:
so if you will try something like null.someInstanceMethod() it will cause such exception.
And for Autoboxing compiler does transform the code to something like:
You can verify it, if you have some idea about javap command...
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!