Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

About implicit conversion

 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pls explain the following: This is from K & B book Chapter 3 Self Test:

class ClassTakesShort{
public void takeShort(Short short){
System.out.println(short);
}

class ClassPassesShort{
ClassTakesShort cts = new ClassTakesShort();
cts.takesShort(7);
}

Now the answer provided is that the code does not compile because anexplicit cast to short is required.
I don't get it. I think 7 can fit into a short, isn't it? If the value had been out of range for short I would have understood it
 
Ranch Hand
Posts: 1249
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Suhas Wadadekar:
Pls explain the following: This is from K & B book Chapter 3 Self Test:

class ClassTakesShort{
public void takeShort(Short short){
System.out.println(short);
}

class ClassPassesShort{
ClassTakesShort cts = new ClassTakesShort();
cts.takesShort(7);
}

First of all how can you assume that Short Object can be converted to int type.

If that is typo mistake and it was "short" Now the answer provided is that the code does not compile because anexplicit cast to short is required.
I don't get it. I think 7 can fit into a short, isn't it? If the value had been out of range for short I would have understood it



No it's not implicit convert the 7 into short it ll be int.
And to use that method you have to explicitly convert it to short.
[ July 11, 2006: Message edited by: Ankur Sharma ]
 
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By default, 7 is promoted to an int, and not to a short. Hence, an explicit cast will be needed in your code.
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I remember that some old version of JDK does not need to do the explicit convertion!
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Method invocation conversion does not contain the implicit narrowing primitive conversions that you have in assignment conversion.

That is.

short s = 7; will compile

but if the parameter to a method is of type short, you cannot send an int even if that int is a compile-time constant that could fit in a short.
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Keith is right.

compiler look for methods in this order if it is resolved early.

1. Exact signature method.
2. Widening signature
3. Boxing signature
4. Var-arg signature

But no narrowing. Since 7 is 32 bit int literal. So it first look for int argument. Since your method does not have either int, or Integer, or int...x so you will get compilation error.

Naseem
[ July 11, 2006: Message edited by: Naseem Khan ]
 
Suhas Wadadekar
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all...that was revealing
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic