• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

More specific method

 
Nitin Bhagwat
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Result of following code is "Integer".
Why 1 can be Byte. Why byte is not a "More specific method" in this case?

Thank you in advance.

 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember that 1 is an int literal?

(And 1.0 is a double literal)
 
Nitin Bhagwat
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Barry,

So this means, using premetive values, we can invoke byte and short only after casting to byte or short? We cannot do it without casting?
 
Muralidhar Mandapati
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that in this case if the method
static void m1(int b) {System.out.println("Integer");}
does not exists it would go to the next to it and it would call for Long.
if that as well does not exists it would go to the next to Float and then to Double.

If we wanted to call byte or short or char it must be type casted.

Cheers,
Murali
 
Saket Barve
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, Muralidhar says above is, broadening conversions (e.g. Integer to Long) can be implicit but narrowing conversions (e.g. Integer to Byte) must be specifically cast.

Regards,
Saket
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey you guys, be careful with using the words Long, Double when you mean long, double. There are wrapper classes with these names and confusion could arise. Same goes for Integer, what we are talking about here is the int primitive type, not the Integer wrapper class.
 
Nitin Bhagwat
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why following 1 is not integer?
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the Java Language Specifications, 2nd Edition

A return statement with an Expression must be contained in a method declaration that is declared to return a value (�8.4) or a compile-time error occurs. The Expression must denote a variable or value of some type T, or a compile-time error occurs. The type T must be assignable (�5.2) to the declared result type of the method, or a compile-time error occurs.

.....

5.2 Assignment Conversion
Assignment conversion occurs when the value of an expression is assigned (�15.26) to a variable: the type of the expression must be converted to the type of the variable. Assignment contexts allow the use of an identity conversion (�5.1.1), a widening primitive conversion (�5.1.2), or a widening reference conversion (�5.1.4). In addition, a narrowing primitive conversion may be used if all of the following conditions are satisfied:

* The expression is a constant expression of type byte, short, char or int.
* The type of the variable is byte, short, or char.
* The value of the expression (which is known at compile time, because it is a constant expression) is representable in the type of the variable.


So the reason "return 1;" is legal in a method returning a byte is the special rule allowing narrowing conversions of a constant expression to a byte if the value of the constant expression will fit in a byte.
[ December 15, 2004: Message edited by: Mike Gershman ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic