[HD Pg 178, Sec. 8.1.1 - creating-a-method]

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

Moderator: admin

Post Reply
raphaelzintec
Posts: 167
Joined: Sun Apr 21, 2024 10:43 am
Contact:

[HD Pg 178, Sec. 8.1.1 - creating-a-method]

Post 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");
    }
Last edited by raphaelzintec on Sat Jun 15, 2024 6:04 am, edited 1 time in total.

raphaelzintec
Posts: 167
Joined: Sun Apr 21, 2024 10:43 am
Contact:

Re: [HD Pg 178, Sec. 8.1.1 - creating-a-method]

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

raphaelzintec
Posts: 167
Joined: Sun Apr 21, 2024 10:43 am
Contact:

Re: [HD Pg 178, Sec. 8.1.1 - creating-a-method]

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

raphaelzintec
Posts: 167
Joined: Sun Apr 21, 2024 10:43 am
Contact:

Re: [HD Pg 178, Sec. 8.1.1 - creating-a-method]

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

raphaelzintec
Posts: 167
Joined: Sun Apr 21, 2024 10:43 am
Contact:

Re: [HD Pg 178, Sec. 8.1.1 - creating-a-method]

Post 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...

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

Re: [HD Pg 178, Sec. 8.1.1 - creating-a-method]

Post 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.

raphaelzintec
Posts: 167
Joined: Sun Apr 21, 2024 10:43 am
Contact:

Re: [HD Pg 178, Sec. 8.1.1 - creating-a-method]

Post by raphaelzintec »

thanks i didnt know this expression

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 9 guests