Lambda behaving differently than anonymous inner class
Sergey Kuksenko
sergey.kuksenko at oracle.com
Wed Mar 26 15:25:37 UTC 2014
it's javac bug. Here is bytecode of desugared lambda method:
private static java.lang.Integer lambda$main$0(java.lang.Integer);
Code:
0: aload_0
1: invokevirtual #9 // Method
java/lang/Integer.intValue:()I
4: iconst_1
5: iadd
6: invokestatic #6 // Method
java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
9: dup
10: astore_0
11: astore_1
12: aload_0
13: areturn
The same bytecode if define the lambda as { return t++; }
On 03/26/2014 07:14 PM, Sam Pullara wrote:
> I'm not sure there is a valid use case but it looks like a bug to me. Does it reproduce if you define the lambda as { return t++; } ? Sam
>
> ---Sent from Boxer | http://getboxer.com
>
> Hello all,
>
>
>
> This e-mail is a follow-up to a question I've posted on StackOverflow:
>
> http://stackoverflow.com/questions/22648079/lambda-behaving-differently-than-anonymous-inner-class
>
>
>
>
>
> I'm relatively new to Java, and decided to pick up on lambda since the past
>
> few days. So I wrote a very simple anonymous inner class and wrote an
>
> equivalent lambda.
>
>
>
> However, the lambda output was different, and it very strongly appears to
>
> be a bug.
>
>
>
> Given:
>
>
>
> interface Supplier<T> {
>
>
>
> T get(T t);}
>
>
>
> Supplier<Integer> s1 = new Supplier<Integer>() {
>
> @Override
>
> public Integer get(Integer t) {
>
> return t++;
>
> }};Supplier<Integer> s2 = t ->
>
> t++;System.out.println(s1.get(2));System.out.println(s2.get(2));
>
>
>
> The output is 2 and 3, NOT 2 and 2, as one would expect.
>
>
>
> More info, including discussion about bytecode is available at the SO link
>
> above.
>
>
>
> I'm also new to this list, so apologies if I've broken any mailing list
>
> etiquette.
>
>
>
> --
> Kind regards,
>
>
>
> Victor Antunes
>
>
>
>
--
Best regards,
Sergey Kuksenko
More information about the lambda-dev
mailing list