4

I am trying to learn Java8 and have tried following example.

I am getting a compilation error for this code. Can you please help me to resolve this issue.

public class Lambdas {

    public static void main(String[] args) {
        System.out.println("Result Of Comparision" + () -> Integer.compare("First".length(), "Second".length()));
    }
}
8
  • 2
    Why are you trying to add (concatenate) a string and a lambda expression? What are you trying to do? The compiler message is pretty clear. Commented May 20, 2017 at 5:59
  • 1
    Can you clarify exactly what you're trying to print here? As @Misha said, printing a lambda expression makes no sense whatsoever. Commented May 20, 2017 at 6:25
  • @JoeC As he said, He's learning it and lets assume he has reasons to play with this piece of code. Commented May 20, 2017 at 7:24
  • @RajaAnbazhagan we cannot possibly help without knowing what the code is supposed to do. Commented May 20, 2017 at 9:11
  • 1
    He's trying to print the result of Integer.compare() using lambda expression. It's clear and I'm not really sure why it's hard to understand. He even understands he's wrong. but wants to know why? Which is good. Please dont shut doors for people without understanding. Commented May 20, 2017 at 9:20

2 Answers 2

2

a lambda expression must have a target type that is a functional interface.

A lambda expression is compatible in an assignment context, invocation context, or casting context with a target type T if T is a functional interface type (§9.8) and the expression is congruent with the function type of the ground target type derived from T.

you can make your code compile by cast lambda expression to a special functional interface. e.g: IntSupplier.

class Lambdas {

    public static void main(String[] args) {
        System.out.println("Result Of Comparision" 
        + (IntSupplier)() -> Integer.compare("First".length(), "Second".length()));
    }
}

But then print lambda itself not the result you expected. so you need call the functional interface method to get the result.

class Lambdas {

    public static void main(String[] args) {
        System.out.println("Result Of Comparision" 
         + ((IntSupplier)() -> Integer.compare("First".length(), "Second".length()))
         .getAsInt());
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

"A functional interface" is not the same as "a @FunctionalInterface". Everywhere you write the second in your answer you should have the first instead.
@AlexeyRomanov thanks for your feedback, sir. we are always used to treat @FunctionalInterface as functional interface. I will fix it.
0

println needs a value to be print. You tried to add a String value to a function, and that is nonsense. A function have no value, a function is a...function, only a function call may produce a value.

9 Comments

This is wrong. In contexts where they are legal lambdas do have values.
@Alexey Romanov: no, lambda expression do not have values. You can convert lambda expressions to an object of an unspecified type implementing a functional interface type having a toString() implementation returning an unspecified, most likely meaningless string. Perhaps that’s what you meant. The lambda expression itself has no value. In the current Java version, it doesn’t even have a type.
@Holger The specification disagrees (in docs.oracle.com/javase/specs/jls/se8/html/…): "The value of a lambda expression is a reference to an instance of a class with the following properties..."
@Alexey Romanov: the section is only describing the “Run-Time Evaluation of Lambda Expressions” in an assignment context, but granted, that wording can indeed mislead into thinking as the instance being the value of the lambda expression and not the result of the assignment. That would require changes, if lambda expressions are supported in different contexts. Still, it’s describing a “value” whose concatenation with a String has an unspecified, meaningless result.
@Holger No, this section applies in all 3 contexts where lambdas are legal. "it’s describing a “value” whose concatenation with a String has an unspecified, meaningless result" In this context it doesn't describe any value, but to say OP's code is illegal because lambdas don't have values is the wrong way around: this lambda doesn't have a value because this isn't a legal context for a lambda.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.