aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on conversion and autoboxing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question on conversion and autoboxing" Watch "Question on conversion and autoboxing" New topic
Author

Question on conversion and autoboxing

Kate Jackson
Greenhorn

Joined: Jan 12, 2010
Posts: 13


// 1 - fine
// 2 - ERROR: The method test(Short) in the type Car is not applicable for the arguments (int)

How is // 1 possible. Did I miss sth obvious here ? How did 4 (int) get promoted to Short ?

Thanks in advance,
Kate
Rufat Piriyev
Ranch Hand

Joined: Mar 15, 2010
Posts: 31
Kate Jackson wrote:

// 1 - fine
// 2 - ERROR: The method test(Short) in the type Car is not applicable for the arguments (int)

How is // 1 possible. Did I miss sth obvious here ? How did 4 (int) get promoted to Short ?

Thanks in advance,
Kate

Everything is true 4 is a type of int and int can't be boxing to Short
Kate Jackson
Greenhorn

Joined: Jan 12, 2010
Posts: 13
Rufat Piriyev wrote:
Everything is true 4 is a type of int and int can't be boxing to Short


Perhaps I didn't make myself clear.
// 2 is fine with me. I also expected // 1 to be the same (compilation error) but this time 4 is a Short. Now I wonder how did it happen ?

In other words:

If Wrapper parameters only work for Integer method how come wrapper assignments also worked for Byte, Short, Character ?

I would appreciate if anyone eplained that to me, I'm going for SCJP in 2 days.
Innar Made
Greenhorn

Joined: May 12, 2009
Posts: 17
Hi all,

About your variable assignment question:

Java Language Specification explains that you can narrow int to byte, short or char if the int value is constant and in valid range of the target type:

A narrowing primitive conversion may be used if the type of the variable is byte, short, or char, and the value of the constant expression is representable in the type of the variable.
A narrowing primitive conversion followed by a boxing conversion may be used if the type of the variable is :
Byte and the value of the constant expression is representable in the type byte.
Short and the value of the constant expression is representable in the type short.
Character and the value of the constant expression is representable in the type char.

It's good question, why we cannot widen int to Long, or int to Double? Primitive widening from int to long or int to double is legal.

Seems like the compiler boxes the int value to Integer first? Then of cource you cannot widen Integer to Long or Double, cause IS-A fails. The question is why the compiler doesn't do primitive widening first and then box?


And your question about methods:

Passing values to methods is not same as assigning values to variables (i.e Short s = 10;). The method wrap_byte(Byte b) just don't know what to do with an int or Integer. Narrowing doesn't apply here like it applied to assigning values to variables. For this to work you need to explicitly cast int to byte. Now autoboxing can work.

So for this to work you need:


Notice however that this is legal (because int can be widened to long)


I hope this helps. I'm sure somebody can explain more clear and easier than me
Kate Jackson
Greenhorn

Joined: Jan 12, 2010
Posts: 13
Innar Made wrote:
Java Language Specification explains that you can narrow int to byte, short or char ...

I didn't know that. Thanks

Innar Made wrote:
It's good question, why we cannot widen int to Long, or int to Double? ...

Judging by the first case this is probably also determined by JLS but I didn't find it yet.

Thanks,
Kate

Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8815
    
    5
Hey Guys,

Here's a 'thought experiment' that might help explain some of this behavior...

Consider that before Java 5 there was widening but not boxing. So, if you don't want old, working code to break, and you want to add boxing, what rules would you have to create?

hth,

Bert


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Kate Jackson
Greenhorn

Joined: Jan 12, 2010
Posts: 13
Bert Bates wrote:
Consider that before Java 5 there was widening but not boxing ...

There's no doubt about that.

We discuss here the opossite - narrowing from compile time constant int to a Byte/Short/Character member that works, and from int to a Long/Double/Float that does not work.
Rahul Nair
Ranch Hand

Joined: Dec 01, 2007
Posts: 138

Here in above code example.... What is the effect of private and static ?
I am little bit confused about the use of both private static keywords.

Static variable is for the class level then, what is the purpose of making them private ?
Jean-Christian Imbeault
Greenhorn

Joined: Dec 08, 2004
Posts: 19
Bert Bates wrote:Hey Guys,

Here's a 'thought experiment' that might help explain some of this behavior...

Consider that before Java 5 there was widening but not boxing. So, if you don't want old, working code to break, and you want to add boxing, what rules would you have to create?

hth,

Bert


Hi Bert and thanks for taking the time to post a reply. I totally agree that it's better to have us think through an answer to our questions rather than just giving us the answer. We're sure to remember and learn much better that way.

But for this one I really have to say that I just can't figure out why the behaviour in the original question is present.

To prevent old code from breaking you would need:

- widening should always win over boxing when a choice exist (lifted from your book, p.249)

But I can't figure out how this 'rule' creates the conditions that allow an int to be passed to method that expects a Short, but cause a compilation error when you try to declare and initialize a Short using an int.

If Short s = 4; is valid code then I would expect Car().test(4) to be also. But obviously I'm wrong

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Question on conversion and autoboxing