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()));
}
}
-
2Why are you trying to add (concatenate) a string and a lambda expression? What are you trying to do? The compiler message is pretty clear.Misha– Misha2017年05月20日 05:59:06 +00:00Commented May 20, 2017 at 5:59
-
1Can you clarify exactly what you're trying to print here? As @Misha said, printing a lambda expression makes no sense whatsoever.Joe C– Joe C2017年05月20日 06:25:10 +00:00Commented 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.Raja Anbazhagan– Raja Anbazhagan2017年05月20日 07:24:48 +00:00Commented May 20, 2017 at 7:24
-
@RajaAnbazhagan we cannot possibly help without knowing what the code is supposed to do.Misha– Misha2017年05月20日 09:11:13 +00:00Commented May 20, 2017 at 9:11
-
1He'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.Raja Anbazhagan– Raja Anbazhagan2017年05月20日 09:20:42 +00:00Commented May 20, 2017 at 9:20
2 Answers 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());
}
}
2 Comments
@FunctionalInterface". Everywhere you write the second in your answer you should have the first instead.@FunctionalInterface as functional interface. I will fix it.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
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.String has an unspecified, meaningless result.