Since primitives are not classes, there is no subclass/superclass kind of relation between
them as such but Java does define the subtype relation for them explicitly, which is as
follows:
double >float >long >int >char
and
int >short >byte
Based on the above, you can easily determine which of the following two methods will
be picked if you call
processData((byte) 10);
void processData(int value){ }
void processData(short value){ }
The short version will be picked because short is a subtype of int and is therefore, more
specific than an int.
Question,
Given these methods,
// void processData(int value){ System.out.println("int");}
void processData(char value){ System.out.println("char");}
void processData(short value){ System.out.println("short");}
From following, call 1 caused and call 2 did not cause, a compiler error (no suitable method found) when I commented out the method with int signature.
1. e.processData(0b101);
2. e.processData((byte)5);
Since byte is a subtype of short, shouldn't have call 1 been fine too? I suspect that is because the compiler is widening to int. If so, why would the compiler widen a byte literal to int?
Thanks
[HD Pg 186, Sec. 8.2.3 - method-selection]
Moderator: admin
-
- Posts: 221
- Joined: Mon Nov 26, 2018 2:43 pm
- Contact:
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: [HD Pg 186, Sec. 8.2.3 - method-selection]
0b101 is not a byte.
If you like our products and services, please help us by posting your review here.
-
- Posts: 221
- Joined: Mon Nov 26, 2018 2:43 pm
- Contact:
Re: [HD Pg 186, Sec. 8.2.3 - method-selection]
As I'm banging my head on the desk and wondering if I'm zoning in and out of this dimension as I look at this book I wonder, does JVM convert every thing to binary before processing?
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: [HD Pg 186, Sec. 8.2.3 - method-selection]
Sorry, I should have been more clear. Normally, any integer literal (irrespective of its format i.e. decimal, octal or binary) is considered an int but if due to implicit primitive narrowing conversion, if the number fits into the variable type, a cast is not required to type it to a smaller type, so byte b = 0b101; will work without any cast even though 0b101 is an int. (As per section 5.2 JLS).
Now, regarding the method calls, implicit narrowing doesn't happen for method calls so when you call processData(0b101), it will look for processData(int ) method. processData(short ) is obviously not suitable for an int and so if you comment the int version out, you get an error.
HTH,
Paul.
Now, regarding the method calls, implicit narrowing doesn't happen for method calls so when you call processData(0b101), it will look for processData(int ) method. processData(short ) is obviously not suitable for an int and so if you comment the int version out, you get an error.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 202
- Joined: Mon Apr 02, 2018 8:40 am
- Contact:
Re: [HD Pg 186, Sec. 8.2.3 - method-selection]
In the book it says:
So that means a subtype should be chosen between subtype and supertype, but in the example bellow a super type is chosen??Technically, a subclass (or a subtype) is always more specific than a super class (or supertype).
Code: Select all
public class TestClass{
public void whap(Object o){System.out.println("from Object method"); }
public void whap(String s){ System.out.println("from String method");}
public static void main(String args[]){
CharSequence ch = "hey";
new TestClass().whap(ch);
}
}
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: [HD Pg 186, Sec. 8.2.3 - method-selection]
Not sure what are you expecting. Is CharSequence a String? No, so how can the String version be selected?
"Most specific" rule is used when there are two applicable methods. what(String ) is not applicable here. A String is-a CharSequence. A CharSequence is not a String.
Please read "3. Most specific method - " point again, carefully.
"Most specific" rule is used when there are two applicable methods. what(String ) is not applicable here. A String is-a CharSequence. A CharSequence is not a String.
Please read "3. Most specific method - " point again, carefully.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 40 guests