• 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

K&B self test question p260

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given:



What is the result?

The answer is compilation fails because int 7 cannot be implicitly converted to short.

My question is why doesn't the implicit cast works here even where 7 is a hardcoded value.
e.g. in the following code

line 1 and 2 work whereas line 3 is compile error, which is ok because the compiler doews not know what is the value of ii. The implicit cast works in line 2 because 7 is hardcoded and it can fir into short.
Why doesn't the same principle work when an hardcoded int value is passed to a method taking short?
[ February 06, 2007: Message edited by: Kiran Gavate ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer to this lies in the following taken from the Java Language Specification, 3rd Edition:

Method invocation conversions specifically do not include the implicit nar-
rowing
of integer constants which is part of assignment conversion (�5.2). The
designers of the Java programming language felt that including these implicit nar-
rowing conversions would add additional complexity to the overloaded method
matching resolution process (�15.12.2).
Thus, the example:
class Test {
static int m(byte a, int b) { return a+b; }
static int m(short a, short b) { return a-b; }
public static void main(String[] args) {
System.out.println(m(12, 2));// compile-time error
}
}

causes a compile-time error because the integer literals 12 and 2 have type int, so
neither method m matches under the rules of (�15.12.2). A language that included
implicit narrowing of integer constants would need additional rules to resolve
cases like this example.



So even something as simple as new Byte(1) is not allowed because the formal parameter of the Byte constructor is a byte not an int.
[ February 05, 2007: Message edited by: Barry Gaunt ]
 
Kiran Gavate
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for the answer.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic