• 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

Question on widening ,boxing

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi




what does this means int doX(Number n, Number m) { return 4; }....Number is keyword or operator....I havent read about this in K&B.

 
Sheriff
Posts: 7134
1360
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
java.lang.Number class will be on the exam. As I remembered it was mentioned in the K&B book. As from the specification "The abstract class Number is the superclass of classes java.lang.BigDecimal, java.lang.BigInteger, java.lang.Byte, java.lang.Double, java.lang.Float, java.lang.Integer, java.lang.Long, and java.lang.Short."
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to add why the output comes to be 4,3 rather than 2,3?

Thanks
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

reji singh wrote:Just to add why the output comes to be 4,3 rather than 2,3?
Thanks


Because for the short call there are only two matching argument lists:

Var-Args is always last on the list, so we go for the 4.

cheers
Bob
 
reji singh
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bob, I didnt know the var-args priority. However, with your explanantion another doubt arises in my mind ( may be a basic one )

widening + boxing not allowed !!! why is this for short type of primitive. Usually short, char, byte all get widen to int , so method below should have called

int doX(Integer x, Integer y) { return 3; }
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was making some changes to the above code as below:

class Eggs {
int doX(Long x, Long y) { return 1; }
int doX(int... x) { return 2; }
int doX(Integer x, Integer y) { return 3; }
//int doX(Number n, Number m) { return 4; }
public static void main(String[] args) {
new Eggs().go();
}
void go() {
short s = 7;
System.out.print(doX(s,s) + " ");
System.out.println(doX(7,7));
} }

In the above code i was expecting the result as : 3 , 3 (as per the low prioirity of var-args)

but the result is : 2 , 3

Bob can you explain how the priority decides in this case

Thanks
 
Bob Wheeler
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

reji singh wrote:Thanks Bob, I didnt know the var-args priority. However, with your explanantion another doubt arises in my mind ( may be a basic one )
widening + boxing not allowed !!! why is this for short type of primitive. Usually short, char, byte all get widen to int , so method below should have called
int doX(Integer x, Integer y) { return 3; }


Widening is ALWAYS allowed , but not widening + boxing (meaning, first widening and then boxing).
So you can of course wide a byte, short or char to an int. But you CAN'T wide and box a byte, short or char to an INTEGER.
But you can box the short primitive to a Short object and wide it to a number object.

cheers
Bob
 
Bob Wheeler
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kumar Ala wrote:I was making some changes to the above code as below:

class Eggs {
int doX(Long x, Long y) { return 1; }
int doX(int... x) { return 2; }
int doX(Integer x, Integer y) { return 3; }
//int doX(Number n, Number m) { return 4; }
public static void main(String[] args) {
new Eggs().go();
}
void go() {
short s = 7;
System.out.print(doX(s,s) + " ");
System.out.println(doX(7,7));
} }

In the above code i was expecting the result as : 3 , 3 (as per the low prioirity of var-args)
but the result is : 2 , 3



You can't do a conversion from a primitive short to an Integer object. This is only possible through boxing + widening (in this order).
And that is not allowed. The only matching argument list in your code is the var-arg. So we have to use this method.

cheers
Bob

Ps. Please use the code brackets for the code.
 
Greenhorn
Posts: 26
Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

as i compile this code showing error as
---------- javac ----------
Eggs.java:14: reference to doX is ambiguous, both method doX(long...) in Eggs and method doX(int...) in Eggs match
System.out.print(doX(s,s) + " ");
^
1 error

if i remove comment at #1 it is compiling Properly ......
 
Bob Wheeler
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Srikanth Nakka wrote:
as i compile this code showing error as
---------- javac ----------
Eggs.java:14: reference to doX is ambiguous, both method doX(long...) in Eggs and method doX(int...) in Eggs match
System.out.print(doX(s,s) + " ");
^
1 error

if i remove comment at #1 it is compiling Properly ......


Yes, because then the call is not ambiguous any more. Boxing + Widening comes first, Var-Arg latest.

cheers
Bob
reply
    Bookmark Topic Watch Topic
  • New Topic