Can somone help me understand this | SoloLearn: Learn to code for FREE!

+5

Can somone help me understand this

I have give up trying understand how this even works and cant find anything to help me understand can anyone help me step by step please 😔 Does isEqual() method store the value in a field or somthing? I'm not getting how test() returns the boolean how is it able to see what's passed to equals method? 🤯 https://code.sololearn.com/cKRHO8DHaPwd/?ref=app

3/5/2021 1:54:51 PM

Nobody

18 Answers

New Answer

+6

the value passed in Predicate.isEqual is stored inside the returned object (assigned to 'str')...

+2

Predicate.isEqual is a constructor (or call a constructor) and return a new object wich is holding the string passed as argument, and (at least) one method 'test'... inside 'test' method of Predicate.isEqual object, the value passed as argument is available, so it's checked against 'test' argument... boolean is returned by 'test' method ^^

+2

Simple way to think about it. When you call the isEqual(var) method It returns the following s->var.equals(s) This lambda expression is the body for test(s) boolean test(s){ return var.equals(s) ^ '------- var here is the string you passed to the IsEquals method }

0

visph im still not getting it 😔 how is there an objevt without using lambda?

0

visph woah I think my brain died 😆 is it possible to create an example code that does this so I can see how it works. In my mind I'm thinking how is test which is the abstract method of interface getting hold of the argument passed to a static method isequal method are there fields involved in the interface because I can see any

0

abstract methods are implemented in final classes...

0

visph is the annoumous class the final class that implemented isEquals? or did test become isEquals?

0

there's no 'anonymous' classes... all the more hidden classes ^^ the final class is the one used to build the object returned by Predicate.isEqual, so the object have a 'test' method implemented...

0

visph Are you able to create an example code I'm so confused because I thought there was an inner annomus object created which was stored to str

0

OOP: class = blueprint object = instance of class the class is stored outside of instances (but is linked and shared betweens instance objects)

0

visph I know the difference, I know that you cant create an instance of interfaces which made me think that an annoumous inner object is created and stored in str variable

0

str store a Predicate object with a 'test' method wich use the value passed in Predicate object: there's no needs to have a hidden inner object, but it could be implemented in that way (even there's no real reasons to do so) ^^

0

visph so when I called the static isEqual on the predicate interface did that implemt the test method ? I'm just trying to figure out how test was able to compare both objects..

0

public interface Predicate<T> { boolean test(T t); static <T> Predicate<T> isEqual(Object target) { return object -> target.equals(object); } } class IsItJava implements Predicate<String> { public boolean test(String s) { return "Java".equals(s); } } public class Program { public static void main(String[] args) { // Predicate<String> str = object -> "Java".equals(object); IsItJava str = new IsItJava(); System.out.println(str.test("Java") ); } }

0

very simple you just create an object of predicate and give it the value. the object remembers this value you passed to when creating it. when you envoke test method. then you are using a method of that object which you created before. the method test will do the job for you so you can define target value once and test it how many times you need.

-1

Compare the string you passed with function u called

-1

-3

Can anyone please tell me why range slider is not working Help me! https://code.sololearn.com/W5UHHJVz70nV/?ref=app