5

I have recently updated my java to the 8th version and trying some Lambdas but it's giving error.

My Code

String[] st = {"ak","ss","dd"};
Arrays.asList(st).forEach(data -> System.out.println(data));

The output of the console is

PS E:\> javac .\Lambdas.java
.\Lambdas.java:11: error: illegal start of expression
 Arrays.asList(st).forEach(data -> System.out.println(data));
 ^
1 error
PS E:\> java -version
java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b15)
Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)

This is strange because I have java 8 as seen above but my code in not compiling.

Is there anything I am doing wrong?

ROMANIA_engineer
57.1k30 gold badges211 silver badges207 bronze badges
asked Sep 2, 2015 at 6:33
13
  • 2
    @Aʟᴀɢᴀʀᴏs: it is in Java8, when using Lambda expressions. Unless there is a space between - and > Commented Sep 2, 2015 at 6:35
  • 3
    just a shot in the dark... but could you try javac -version Commented Sep 2, 2015 at 6:36
  • 1
    Can you please check that javac is the same version too? javac -version Commented Sep 2, 2015 at 6:36
  • 1
    Works fine for me ! Please post output of javac -version . Commented Sep 2, 2015 at 6:36
  • 3
    @codegasmer, you posted the version of java, not javac Commented Sep 2, 2015 at 6:51

1 Answer 1

3

It's the compiler issue. In my environment variable I was pointing towards the old java 7 version. After changing it to 8 the code compiled.

Sotirios Delimanolis
281k62 gold badges718 silver badges744 bronze badges
answered Sep 2, 2015 at 7:10
Sign up to request clarification or add additional context in comments.

1 Comment

I suppose you're referring to the JAVA_HOME environment variable

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.