4

Why is an exception thrown when using ?: operator in a Java 8 lambda expression?
When I try to run the following sample code:

import java.util.ArrayList;
import java.util.List;

public class TestClass
{
    public static void main(String[] args)
    {
        List<Foo> foos = new ArrayList<>();
        boolean b = true;
        foos.forEach(foo -> (b ? foo.doSth(1) : foo.doSth(2)));
    }

    @FunctionalInterface
    interface Foo
    {
        public void doSth(int i);
    }
}

I get the following exception:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
at gui.marksDetection.view.TestClass.main(TestClass.java:8)

When replacing the lambda expression with a for-each-loop, or replacing (b ? foo.doSth(1) : foo.doSth(2)) with an if-else-block, everything works fine, so there seems to be a problem with the combination ?: + lambda. However, Eclipse does not mark it as an error.

4
  • Your code doesn't compile, but Java lets you run it anyway and see. When it came to where the compilation problem was, it throws and exception. Your problem is that the syntax is wrong; you have one too many parentheses. Commented Sep 22, 2014 at 9:46
  • "foo ->" what is this in foos.forEach(foo -> (b ? foo.doSth(1) : foo.doSth(2)));. can you describe what is it stand for. Commented Sep 22, 2014 at 9:47
  • @Nateowami: Do you mean using "b ? foo.doSth(1) : foo.doSth(2)" instead of using "(b ? foo.doSth(1) : foo.doSth(2))" -> This throws the same exception. Commented Sep 22, 2014 at 9:54
  • Yes; and while that's not the only problem, having too many parentheses will never work, and make it harder to track the real problem. Commented Sep 22, 2014 at 10:10

2 Answers 2

9

The Ternary statement requires the separate parts to be expressions and not statements

You are not allowed to invoke statements in the separate branches.

For more information see the Java Language specification section 15.25

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for looking it up. Can you also explain why Eclipse doesn't mark it as an error?
The lambda Features seem a little underdeveloped in all IDE's to date. Especially because you didn't use brace Notation for the expression block.
-1

Your code does not compile with javac:

incompatible types: bad return type in lambda expression
missing return value

I suspect this is a bug in eclipse.

6 Comments

This is not a bug.Even I see this error in netbeans
@pd30 Not sure I follow you: the bug is that eclipse should not allow the OP to run the code because it does not compile. Netbeans shows a compile error which is normal.
Do you mean eclipse is able to run the code in spite of the fact there is compilation issue?
@pd30 I don't have eclipse but the question states that the code ran and threw an exception. Since it should not have compiled in the first place I concluded that it is an eclipse bug (and there are numerous bugs in eclipse with Java 8 so it is not really surprising).
@assylias Eclipse will actually let your run code that doesn't compile, but when it gets to the line where there was a problem it throws an exception. (Not that that's necessarily what happened here, but certainly a possibility).
|

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.