• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

About implicit conversion

 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
I remember that some old version of JDK does not need to do the explicit convertion!
 
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
Thanks all...that was revealing
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!