Win a copy of React Cookbook: Recipes for Mastering the React Framework this week in the HTML Pages with CSS and JavaScript 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:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

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.
 
It's feeding time! Give me the food you were going to give to this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic