• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

confusing stuff in wrapper classes

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone, there is some very confusing stuff going on in the initial chapters






if we have a method that takes Byte argument:
then while ###1 is allowed, BUT



there are few more in wrappers, please suggest are we required to remember all this?? its to confusing, no fixed logic.

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ash Gill wrote:



The first and last don't have to be explained -- as you mentioned that it is as expected. The second and third is happening because the Float class provides a constructor that takes a double primitive variable.

Henry
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ash Gill wrote:



The first and second is happening due to a rule in the Java language specification regarding compile time constants. It is related to assignment conversions (section 5.2) and it applies to byte, short, char, and int. Interesting, this rule doesn't apply to long, float, or double, so doesn't work for your previous set of questions.

As for the third, the Byte class doesn't have a constructor that takes an int parameter.

Henry
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ash Gill wrote:
if we have a method that takes Byte argument:
then while ###1 is allowed, BUT



The rule regarding compile time constants (which I mentioned previously) related to assignment conversions (section 5.2) does not have an equivalent rule related to method invocation conversions (section 5.3).

Henry
 
Greenhorn
Posts: 6
Hibernate Eclipse IDE Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ash,

the most important rule to remember is that floating-point literals are double by default.




so @ line 1 : 22.0 is double and can't be assigned to float variable.
@ line 2 : if you checked the Float() constructor in API you'll find that it has one version that accepts float value and another overloaded version that accepts double value

@ line 4 : the value (22.0) is double by default , and double can't be implicitly narrowed to float.

it'll work if there is explicit casting :



as for the method that takes byte or Byte argument
as mentioned in java language specifications :

method invocation doesn't include implicit narrowing of integer constants; so you can't call a method that takes a byte/Byte argument using integer literal.

i hope this clears your confusion
 
Ash Gill
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot henry and mahmoud. the logic is clear, i will just have to practice and remember now
 
Ranch Hand
Posts: 62
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Doubt :
Here , in the following :

its ok that 22.0 is double by default. But, when it gets compiled, anyhow,any one of the constuctors of Float( one with float argument or other with double argument) should be called to create a wrapper object, doesn't it? Since, 22.0 is double , it should be matched with the Float(double) . Then why is it not getting compiled? Actually what happens when this type of line is compiled?

for the following line also , compiler is giving error... why ?


 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prashanth Patha wrote:its ok that 22.0 is double by default. But, when it gets compiled, anyhow,any one of the constuctors of Float( one with float argument or other with double argument) should be called to create a wrapper object, doesn't it? Since, 22.0 is double , it should be matched with the Float(double) . Then why is it not getting compiled? Actually what happens when this type of line is compiled?



The Java compiler doesn't use "any one of the constructors of Float". It follows very specific rules for autoboxing -- which is defined by the Java language specification. Specifically, this line....



Gets converted to this (via boxing)....




Henry
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prashanth Patha wrote:for the following line also , compiler is giving error... why ?




This second case gets converted to ....



Henry
 
Prashanth Patha
Ranch Hand
Posts: 62
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Henry Wong ,
one more Doubt:

There are only two constructors in Java API for Double wrapper class :

1) Double(double value)
2) Double(String s)

Then, when we write something like this :



here, we are passing float literal to Double constructor. But their is no constructor for matching with that.Then , how is it getting executed ? Actually , for assignment statements, float literals will be implicitly widened to double... is their anything same for this constructors ?
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prashanth Patha wrote:
here, we are passing float literal to Double constructor. But their is no constructor for matching with that.Then , how is it getting executed ? Actually , for assignment statements, float literals will be implicitly widened to double... is their anything same for this constructors ?



Yes. See section 5.3 (Method Invocation Conversion) of the Java Language Specification.

 
Prashanth Patha
Ranch Hand
Posts: 62
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Henry Wong........
reply
    Bookmark Topic Watch Topic
  • New Topic