Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Autoboxing AutoUnboxing problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Autoboxing AutoUnboxing problem" Watch "Autoboxing AutoUnboxing problem" New topic
Author

Autoboxing AutoUnboxing problem

Adam Tkaczyk
Greenhorn

Joined: Nov 29, 2007
Posts: 21
Hi, I am going to pass SCJP exam soon. I have some problem to understand some boxing topic.

Why does code
Short x=1;
works fine but

public void doSomth(Short param){
...
}
myObject.doSomth(1);

doesn't.

What is the difference?


SCJP5
Ramesh Ponnada
Greenhorn

Joined: Nov 05, 2004
Posts: 18
myObject.doSomth(1);

-- compiler complains there is no method like doSmith(int).

My guess is when comes to method arguments a number literal is always int, as the compiler will not know whether to cast it to a short or a byte (if you have 2 overloaded methods).


Thanks<br />Ramesh <br />SCJP 5.0 | SCWCD 5.0
maredu ramesh
Greenhorn

Joined: Sep 14, 2007
Posts: 4
hi,

When your passing 1 to method, it will convert 1 to Integer using auto boxing,but it can not find method taking Integer argument,So it will give compile time error,If you change method parameter from Short to Integer then code will compile.
Ram Reddy
Ranch Hand

Joined: Feb 20, 2007
Posts: 88
Default java considers integer literals as ints and real literals as doubles.

so compiler treats '1' as int. So it is specifying there is no method which accepts
1)int or broder types of int
2) Integer or broder types of Integer
3) Var- agrs which accepts one of above two.

Please type cast as myObject.doSomth((short)1);

Now compiler treats as short type argument.
[ November 29, 2007: Message edited by: rami marri ]
Adam Tkaczyk
Greenhorn

Joined: Nov 29, 2007
Posts: 21
Ok, but why compiler can make 2 conversion in code
Short x=1;
I think it will do something like this:
- converts '1', which is an int to short : (short)1;
- box small short to big one : Short

Can Java make two implicit conversions?
Adam Tkaczyk
Greenhorn

Joined: Nov 29, 2007
Posts: 21
I have found the solution.
Short x=1 doesn't work in Sun compiler but works fine in IBM compiler which is in Eclipse IDE which I use.

Sun compiler can't make two implicit conversion, but the IBM one can in variable initialization.
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Originally posted by Adam Tkaczyk:
I have found the solution.
Short x=1 doesn't work in Sun compiler but works fine in IBM compiler which is in Eclipse IDE which I use.

Sun compiler can't make two implicit conversion, but the IBM one can in variable initialization.


This shouldn't happen. Are you sure you're using a 1.5 version of the Sun compiler? The Java Language Specification explicitly describes the mechanism that allows statements like "Short x = 1":

5.2. Assignment Conversion

Assignment conversion occurs when the value of an expression is assigned (�15.26) to a variable: the type of the expression must be converted to the type of the variable.

...

In addition, if the expression is a constant expression (�15.28) of type byte, short, char or int :

* 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.

Note the part I bolded above.
[ November 30, 2007: Message edited by: Kelvin Lim ]

SCJP 5.0
Adam Tkaczyk
Greenhorn

Joined: Nov 29, 2007
Posts: 21
I thought so, but I discovered that the JDeveloper, which I used for test, has Oracle implementation of java compiler (that is implicit setup). In JDeveloper (10.1.3.3) and ocj compiler
code
Short x=1;
does not compile. I don't know why.
Today I know that is good to use Sun IDE for SCJP preparation. Otherwise it is posible to make even simple aspects of language hard to understand because of standard violations.

Thanks for help to everybody.
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Perhaps you're using an older compiler that doesn't support Java 1.5 at all? Check whether it accepts other new Java 1.5 language features, like var-args and generics.

Otherwise, if other Java 1.5 constructs compile fine but the line you stated doesn't, then it's most likely a compiler bug. You probably should see if there's an update or patch that fixes this.
 
jQuery in Action, 2nd edition
 
subject: Autoboxing AutoUnboxing problem
 
Similar Threads
Short initialize
ID:7 SCJP Question of the day !!!
Most specific Method-Overloading??
This abstract method surprised me--Kathy&Bert Book
Problem with JTabbed pane focus