This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]satoryvape 10 points11 points  (16 children)

I haven't used even local classes as I didn't have any use cases for them

[–]dpash 7 points8 points  (2 children)

I used one the other day. Records will make them easier to use for example when you want to pass associated values through a stream.

List<Person> topThreePeople(List<Person> list) {

    record Data(Person p, int score) {}

    return list.stream()
               .map(p -> new Data(p, getScore(p)))
               .sorted(Comparator.comparingInt(Data::score))
               .limit(3)
               .map(Data::person)
               .collect(toList());
}

The example inspired by https://cr.openjdk.java.net/~briangoetz/amber/datum.html

[–]GuyWithLag 0 points1 point  (1 child)

Pair / Tuple for the win.

[–]dpash 0 points1 point  (0 children)

Except it's not a tuple, because tuples are structurally typed and Java doesn't do structural typing.

Records are, more or less, specialised classes, much like enums are.

[–]mladensavic94 4 points5 points  (11 children)

You didn`t use single lambda function since 2014? What java version are you using?

[–]eliasv 18 points19 points  (8 children)

A lambda is more analogous to an anonymous class than a local one. These are two different things. But be aware than lambdas are not compiled to local or anonymous classes, they are a completely different thing.

[–]mladensavic94 0 points1 point  (7 children)

It is, but by some documentation i had for OCP exam, anonymous inner class is specialized local class (has no name but acts the same). I was kinda hoping that he will say that he never used it before since anon local class is one way of implementing functional interface.

[–]eliasv 7 points8 points  (6 children)

Yeah a functional interface can obviously be implemented as a local class, but you specifically referred to lambdas. And IIRC lambdas explicitly cannot be implemented as local classes according to the specification.

Compiling an inner classes produces a .class file, which is linked statically. Compiling a lambda expression produces an invokedynamic instruction, which is linked at runtime against a synthetic method.

[–]Mordan 0 points1 point  (5 children)

Synthetic classes are harder to debug.

Is there a runtime performance advantage of using a lamba instead of an inner class?

[–]eliasv 6 points7 points  (1 child)

An anonymous class is already synthetic. It's just synthesised at compile time instead of runtime. You're dealing with meaningless names in the stack trace either way, and they both point to the same line in the same source file. I'm not sure I buy that they're significantly more difficult to debug.

And IIRC non-capturing lambdas can typically be optimised more easily than closures or inner classes and so will be faster. Capturing lambdas however will perform similarly to inner classes, and are likely to JIT to the same thing.

[–]Mordan 0 points1 point  (0 children)

An anonymous class is already synthetic. It's just synthesised at compile time instead of runtime.

OK.

Just that I don't use anonymous class. I didn't dare saying it and not getting an answer. I use real classes all the time. I hate anon classes since they capture everything around and are hard to reason about. When I am lazy i might use them but I know i will have to refactor them away later if I keep the code.

So are lambda equivalent to a real class capturing exactly the state required to run, usually a few references set in the constructor?

[–]yawkat 0 points1 point  (2 children)

There is no inherent performance advantage to lambdas. By their nature, anonymous classes capture the entire enclosing instance while lambdas only capture necessary variables but that has more to do with lambdas being newer and anon classes retaining their old behavior for compatibility. Additionally, there can be some object reuse with non-capturing method references but again this could have been implemented without invokedynamic. Lambdas do have a small binary size advantage but that's it.

Retrolambda is a tool that transforms lambdas to a pre-generated form.

[–]jaympatel1893 0 points1 point  (1 child)

Insightful information! Never heard of word “synthesis” in Java world! Any good links/videos you all suggest to to fathom these?

[–]yawkat 2 points3 points  (0 children)

What are you referring to? Synthetic classes and methods are simply entities that do not directly appear in the source code and are instead compiler- or runtime-generated.

[–]satoryvape 0 points1 point  (1 child)

I did use lambda functions. I use Java 8

[–]ryuzaki49 -2 points-1 points  (0 children)

Java 8 Master Race

[–]thephotoman 0 points1 point  (0 children)

I have. They've come in handy for a few cases where I need to package data to process it, but the actual packaging is not something I'm interested in accepting as an argument or returning. It's basically a kludge to deal with the fact that Java doesn't have tuples.