[HD Pg 186, Sec. 8.2.3 - method-selection]

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

[HD Pg 186, Sec. 8.2.3 - method-selection]

Post by OCAJO1 »

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

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 186, Sec. 8.2.3 - method-selection]

Post by admin »

0b101 is not a byte.
If you like our products and services, please help us by posting your review here.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

Re: [HD Pg 186, Sec. 8.2.3 - method-selection]

Post by OCAJO1 »

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 :oops: I wonder, does JVM convert every thing to binary before processing?

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 186, Sec. 8.2.3 - method-selection]

Post by admin »

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.
If you like our products and services, please help us by posting your review here.

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

Re: [HD Pg 186, Sec. 8.2.3 - method-selection]

Post by flex567 »

In the book it says:
Technically, a subclass (or a subtype) is always more specific than a super class (or supertype).
So that means a subtype should be chosen between subtype and supertype, but in the example bellow a super type is chosen??

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);
	
	}
}

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 186, Sec. 8.2.3 - method-selection]

Post by admin »

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.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 40 guests