GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes More specific method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "More specific method " Watch "More specific method " New topic
Author

More specific method

Nitin Bhagwat
Ranch Hand

Joined: Sep 09, 2004
Posts: 132
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.



"Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Remember that 1 is an int literal?

(And 1.0 is a double literal)


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Nitin Bhagwat
Ranch Hand

Joined: Sep 09, 2004
Posts: 132
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

Joined: Nov 14, 2004
Posts: 23
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

Joined: Dec 19, 2002
Posts: 229
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

Joined: Aug 03, 2002
Posts: 7729
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

Joined: Sep 09, 2004
Posts: 132
Why following 1 is not integer?
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
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 ]

Mike Gershman
SCJP 1.4, SCWCD in process
 
GeeCON Prague 2014
 
subject: More specific method