About Question enthuware.ocpjp.v8.2.1871 :

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

Moderator: admin

pavvel
Posts: 12
Joined: Fri Dec 15, 2023 4:00 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1871 :

Post by pavvel »

I understood why the third option works - process(fnames, TestClass::size);
Due to the example below:

Code: Select all

class Main {
    public static void main(String[] args) {
        I i1 = A::m;
    }
    @FunctionalInterface
    interface I{
        void method();
    }
    class A {
        public static void m() {}
    }
}
Last edited by admin on Thu Apr 18, 2024 8:40 am, edited 1 time in total.
Reason: Please put code inside [code] [/code]

namvh1506
Posts: 2
Joined: Mon Jun 19, 2023 9:11 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1871 :

Post by namvh1506 »

I think I got it

Code: Select all

interface Carnivore {
	int eat(Tiger t, List<String> foods);
}

class Tiger {
	public int eat(List<String> foods) {
		return 0;
	}
}

class TestClass1 {
	public static void main(String[] args) {
		Carnivore c = Tiger::eat;
	}
}
In Oracle (https://docs.oracle.com/javase/tutorial ... ences.html) it said
Reference to an Instance Method of an Arbitrary Object of a Particular Type
The following is an example of a reference to an instance method of an arbitrary object of a particular type:

String[] stringArray = { "Barbara", "James", "Mary", "John",
"Patricia", "Robert", "Michael", "Linda" };
Arrays.sort(stringArray, String::compareToIgnoreCase);
The equivalent lambda expression for the method reference String::compareToIgnoreCase would have the formal parameter list (String a, String b), where a and b are arbitrary names used to better describe this example. The method reference would invoke the method a.compareToIgnoreCase(b).

Similarly, the method reference String::concat would invoke the method a.concat(b).
So we have Tiger::eat would invoke the method t.eat(List<String> foods)
~because Carnivore have 2 parameter (Tiger t, List<String> foods)

namvh1506
Posts: 2
Joined: Mon Jun 19, 2023 9:11 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1871 :

Post by namvh1506 »

Method in functional interface must have at least 1 param has class Person to use Person::getAge

Code: Select all

 class Test {
    public static void main(String[] args) {
        Function<Person, Integer> f = Person::getAge;
        f.apply(new Person());
    }
}

class Person {
    private int age;

    public int getAge() {
        return age;
    }
}

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 12 guests