[Java] Bad return type in lambda expression: int cannot be converted to boolean


If you are using functional interface using Lambda expression in your code and you get the below error, that's because your expression does not return the datatype which the Function Interface method expects,

Example: Functional Interface
package org.code2care;

@FunctionalInterface
public interface Validate {

    public boolean isString(Object object);

}
Example: Lambda expression
package org.code2care;

public class Example {

    public static void main(String[] args) {

        Validate validate = object -> object.toString();
        Validate validate1 = object -> object.toString().length();

        System.out.println(validate.isString("Java"));
    }
}
Error:

/Users/code2care/IdeaProjects/examples/src/org/code2care/Example.java:7:54
java: incompatible types: bad return type in lambda expression
   java.lang.String cannot be converted to boolean|

/Users/code2care/IdeaProjects/examples/src/org/code2care/Example.java:7:58
java: incompatible types: bad return type in lambda expression
   int cannot be converted to booleanHBE|

As you can see the reason for the complication error is isString(Object obj) returns a boolean and not java.lang.String or an int

Java - Bad return type in lambda expression - String or int cannot be converted to boolean
Advertisements
Try Out Code2care Dev Tools:

Advertisements



#Android #Java #Linux #Microsoft #Google #Python #MacOS #Notepad++ #Microsoft Teams #CSS #PHP #SharePoint #Html #Linux #C-Program #Bootstrap #jQuery #Sublime #Android-Studio #Facebook #Eclipse #WhatsApp #News #MySQL #Json #HowTos #JavaScript #FTP #S3


Advertisements


Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and Software Developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include Learnings, Tutorials, Trouble-Shooting steps, Video Tutorials, Code Snippets, How Tos, Blogs, Articles, etc.
🎉 We are celebrating the 10th years of Code2care! Thank you for all your support! We hope we made a difference.
We strong support Gender Equality & Diversity.

We stand in solidarity with Ukraine - Make a donation to UNHCR - https://donate.unhcr.org/in/en-in/ukraine-emergency