Page 1 of 1
[HD Pg 178, Sec. 8.1.1 - creating-a-method]
Posted: Sat Jun 15, 2024 5:57 am
by raphaelzintec
hello
plz explain me this cuz chatgpt became extremly dumb even on premium
NOT AMBIGUOUS AND SHOW PRIMITIVE
Code: Select all
m(10, (short) 50);
}
static void m(int i, short s){
System.out.println("primitive");
}
static void m(Integer i, Short s){
System.out.println("Wrapper");
}
AMBIGUOUS ERROR
Code: Select all
m(10, (short) 50);
}
static void m(int i, Short s){ //only changed short to Short
System.out.println("primitive");
}
static void m(Integer i, Short s){
System.out.println("Wrapper");
}
Re: [HD Pg 178, Sec. 8.1.1 - creating-a-method]
Posted: Sat Jun 15, 2024 6:03 am
by raphaelzintec
here is the same story AMBIGUOUS
Code: Select all
m(10, (short) 50, 100l);
}
static void m(int i, short s, Long l){
System.out.println("primitive");
}
static void m(Integer i, Short s, Long l){
System.out.println("Wrapper");
}
Re: [HD Pg 178, Sec. 8.1.1 - creating-a-method]
Posted: Sat Jun 15, 2024 6:15 am
by raphaelzintec
1. First case
we have two methods: one only primitives type and second with only wrapper.
if you call method only with primitives values or only with wrapper class values then no problem for compiler he can make the difference
the problem is when you start mixing, i feel like compiler is happy only when you have ONLY PRIMITIVES OR ONLY WRAPPER other wise he start mixing everything
for example here thanks for sending me only wrapper it's easy to get you need second even though the first has 1 long wrapper:
Code: Select all
Integer integerV = 10;
Short shortV = 50;
Long longV = 100L;
m(integerV,shortV,longV);
}
static void m(int i, short s, Long l){
System.out.println("primitive");
}
static void m(Integer i, Short s, Long l){
System.out.println("Wrapper");
}
Re: [HD Pg 178, Sec. 8.1.1 - creating-a-method]
Posted: Sat Jun 15, 2024 6:18 am
by raphaelzintec
and here is the problem: ambiguous error
compiler doesn't know which one to choose, but from a human logic perspective it's definetaly obvious that the second is the best match
Code: Select all
Integer integerV = 10;
Short shortV = 50;
Long longV = 100L;
m(integerV,shortV,100L);
}
static void m(int i, short s, Long l){
System.out.println("primitive");
}
static void m(Integer i, Short s, Long l){
System.out.println("Wrapper");
}
Re: [HD Pg 178, Sec. 8.1.1 - creating-a-method]
Posted: Sat Jun 15, 2024 6:26 am
by raphaelzintec
compiler is like thinking as this:
- thanks for sending me only primitives i have a method having only primitives
- thanks for sending me only Wrapper i have a method having only Wrapper
- thanks for sending me a mix of Wrapper and primitives cuz i dont care anyway since i have only one method for that even in another order but it's a deal
And finally
Wait, you are sending 2 Wrapper and one Primitive, i have two methodes for that: one having only Wrappers and second having 2 primitives and one Wrapper , i know i should use the first method but i'm just a compiler and i have no logic so i'm lost i dunno which one is the best match.
Because now i'm thinking if i need to use the first method and make autoboxing...
Re: [HD Pg 178, Sec. 8.1.1 - creating-a-method]
Posted: Sat Jun 15, 2024 10:41 pm
by admin
Although the exam does not contain questions that require such complicated scenarios but if you are interested in exact details, please see
section 15.12.2.3 and
section 5.3 of JLS.
You will notice that it talks about matching methods using strict and loose invocation. In your examples, neither of the methods match using strict invocation but both of the methods match using loose invocation. That is why the compiler generates the error.
Re: [HD Pg 178, Sec. 8.1.1 - creating-a-method]
Posted: Sun Jun 16, 2024 4:40 am
by raphaelzintec
thanks i didnt know this expression