• Post Reply Bookmark Topic Watch Topic
  • New Topic

Looks like a Lambda Expression  RSS feed

 
Kevin Simonson
Ranch Hand
Posts: 223
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm reading through some code trying to understand what it's doing, and I came across a method that uses an arrow operator (that is, "->"). Changing the names used, the method is:

Is this legal code for Java 6? It almost looks like a lambda expression, and I thought you couldn't use those until Java 8. If it is legal, what does it mean?

I should say that this is somebody else's checked in code, and it's been running, so it almost has to be correct syntax. But I'm still curious what precisely it does.

Kevin S
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree, it looks very much like a lambda expression. And no, those should not work in Java 6. A few possibilities:

1. The project is really in Java 8. Maybe most of it doesn't use Java 8 features, but some parts do, and it's expected to be run in Java 8.

2. The offending class doesn't actually compile or work in any way. Perhaps other code is using an old jar or class file that contains an old version of this code, and the dysfunctional new version is being bypassed.

3. You're looking at the code in an IDE like IntelliJ which, in some situations, will display an anonymous class (with a single method) as if it were a lambda. In this case, you should be able to click on the code to get it to expand to a more verbose-looking, traditional anonymous class.

4. The class is actually in some other language - Groovy, perhaps?

Can you get the code to run on your machine? Or can you see it run on someone's machine? What sort of build environment does it use - ant, maven, something else? What do you know about how it's built and executed?

I will be interested to hear what you find out about this.

 
Kevin Simonson
Ranch Hand
Posts: 223
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote:I agree, it looks very much like a lambda expression. And no, those should not work in Java 6. A few possibilities:

1. The project is really in Java 8. Maybe most of it doesn't use Java 8 features, but some parts do, and it's expected to be run in Java 8.

2. The offending class doesn't actually compile or work in any way. Perhaps other code is using an old jar or class file that contains an old version of this code, and the dysfunctional new version is being bypassed.

3. You're looking at the code in an IDE like IntelliJ which, in some situations, will display an anonymous class (with a single method) as if it were a lambda. In this case, you should be able to click on the code to get it to expand to a more verbose-looking, traditional anonymous class.

4. The class is actually in some other language - Groovy, perhaps?

Can you get the code to run on your machine? Or can you see it run on someone's machine? What sort of build environment does it use - ant, maven, something else? What do you know about how it's built and executed?

I will be interested to hear what you find out about this.


It was number 3. I'm looking at it in IntelliJ, and when I clicked on the expression with the "->" in it that transformed it into the "more verbose-looking, traditional anonymous class"!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!