aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes confusing stuff in wrapper classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "confusing stuff in wrapper classes" Watch "confusing stuff in wrapper classes" New topic
Author

confusing stuff in wrapper classes

Ash Gill
Ranch Hand

Joined: Jun 30, 2011
Posts: 71
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.

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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
Mahmoud Abdulmoniem
Greenhorn

Joined: Jul 14, 2010
Posts: 6

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

OCPJP 6 (95%)
Ash Gill
Ranch Hand

Joined: Jun 30, 2011
Posts: 71
thanks a lot henry and mahmoud. the logic is clear, i will just have to practice and remember now
Prashanth Patha
Ranch Hand

Joined: May 06, 2011
Posts: 62

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 ?




Thanks & Regards
Prashanth

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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




This second case gets converted to ....



Henry
Prashanth Patha
Ranch Hand

Joined: May 06, 2011
Posts: 62

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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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

Joined: May 06, 2011
Posts: 62

Thank you Henry Wong........
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: confusing stuff in wrapper classes