• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question to Cay Horstmann on Lambda expressions

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greetings Cay,

I know that Lambda expressions are a big deal in Java 8, but I'm interesting in knowing why you think this is s "Core" Java item and not an advanced topic for the second volume? It might just be me but I don't even think that Lambda expressions are object-oriented and don't belong in the language at all, but I digress. Please don't take that as a criticism. It's not about you or the book. On the contrary, I began learning about Java in the very first edition of this book, and learned things I found nowhere else. So I am looking forward to working through it. Thanks.

Duncan
 
Marshal
Posts: 79151
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Streams and λs have become “Core” and “basic”. When I started, in Java1.4, the correct way to iterate an array was with a for loop. Then in Java5 the for‑each loop becamse the correct way to iterate an array. Now, if I were teaching, I would say that the correct way to iterate an array is to create a Stream, with IntStream#of or Arrays#stream or similar.
As soon as you start GUIs you will have to look at anonymous classes for Listeners or Handlers; when you are sorting you would start looking at Comparators. Which of these two are you going to prefer?That shows that λs should now be regarded as basic programming which people should learn in primary school their first few weeks of programming.
 
author
Posts: 284
35
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is important to learn lambdas (and method expressions) from the get-go because it makes it easier to learn Java. Here is an even simpler example:

Suppose you need to sort employees by ID. What would you prefer to write?



or



Or, as another simple example, if you want to do some work in another thread, you can write



That's a lot better than making a Runnable.

As for streams, I would agree that it's a bit more advanced. In Core Java, it's in Volume II. That might change if the stream library became a bit more user-friendly, or if many more programmers used it.

Cheers,

Cay

 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Cay Horstmann wrote:. . . That's a lot better than making a Runnable.

Now, I know that no anonymous class is created becaus there is no longer the Foo$999.class file to be seen. But does such code not create a Runnable object behind the scenes?

As for streams, I would agree that it's a bit more advanced. In Core Java, it's in Volume II. That might change if the stream library became a bit more user-friendly, or if many more programmers used it.

Cheers,

Cay

Isn't that “when many more programmers use[d] it”?
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Which of these two are you going to prefer?


If it weren't for the necessary <String> (because the call to reversed() makes type inference a bit wonky):
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:If it weren't for the necessary <String> (because the call to reversed() makes type inference a bit wonky):


Well... Why not this?
I have won.
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And the prize is a cow
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But the idea was to show a λ in use.

I think this thread might well be duplicated in the Java8 forum.
 
Ken Duncan
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:But the idea was to show a λ in use.

I think this thread might well be duplicated in the Java8 forum.



Yes, it does that. I am clearly not as comfortable with Java 8 features as many. However, my objection is not about what code you write to accomplish a task. For my own part, simple, easy-to-understand code is superior to complex/cryptic syntax, but I digress.

However, my essential point initially was that Java is supposed to be object-oriented. As far as I can see, lambda expressions don't belong to any class. They are indeed more like a C function embedded in Java code. I think the object-oriented paradigm is a good one. Lambdas are not OO. They are not a member of a clss I defined. They are not methods of a class I defined. At least anonymous inner classes, though inelegant, are classes. It is not far from creating a Singleton, Final Static class, Final Static method. However, that, at least, is a class from which an object is instantiated (I think). Do lambda expressions cause an object to be instantiated?My objection is primarily, though not exclusively, conceptual, not syntactical. I will be interested to see what this looks like in a stack trace because it does not belong to anything I instantiate, if I understand lambdas correctly. I will learn them because they are now part of Java, but I don't think they should be.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ken Duncan wrote:However, my essential point initially was that Java is supposed to be object-oriented.

What, so it can't be anything else?

As far as I can see, lambda expressions don't belong to any class.

No, but they are based on a class. And moreover, you can write one yourself.
Indeed, functional interfaces and lambdas are simply the formalization of something that was already being done (albeit much more clumsily) before version 8 came out.

They are indeed more like a C function embedded in Java code.

No they're not. C has no execution-time binding.

I think the object-oriented paradigm is a good one.

I agree.

Lambdas are not OO.

No; but does that necessarily make them bad?

They are not a member of a clss I defined. They are not methods of a class I defined.

Only because you haven't written one. They ARE a class - or at least, a lambda expression is based on a class that implements a functional interface (although I believe the compiler is quite clever about stuff like that, so it doesn't actually have to be annotated with @FunctionalInterface) - so you could make one a member of any class you write.

Do lambda expressions cause an object to be instantiated?

They may do. It depends what the method they're based on does.

My objection is primarily, though not exclusively, conceptual, not syntactical. I will be interested to see what this looks like in a stack trace because it does not belong to anything I instantiate, if I understand lambdas correctly.

No, but any stack trace you get will be thrown by a real Java class, which will be a functional interface. In many cases you may not have written it, but this is no different to getting a stack trace from a method in ArrayList or HashMap.

I am clearly not as comfortable with Java 8 features as many.


And that, I suspect, is what's at the heart of your objections. You don't really understand lambdas or "functions", and therefore you can't fathom why anyone would want to use them. Luckily, they remind me of script programming, which is all about applying "additive" functions to streams of data (usually lines of input), so it's not as big a leap for me I suspect.

There's also no requirement to use them, so there's plenty of time to learn them by "osmosis" - maybe by talking to someone who's familiar with functions and can show you some v8 code he or she has written and explain the "why" for you.

HIH

Winston
 
Saloon Keeper
Posts: 15484
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Having higher order functions actually enhances OO. A cornerstone of OO is that instead of asking objects for data and then operating on that data, you tell objects what to do with their private data. Their behavior and data is bundled together.

Being able to pass an operation as a method argument greatly improves this TellDontAsk principle: You literally tell the object what to do, and it can then perform the operation if it's valid.

Besides, treating a function as if it's an object, what's more object oriented than that?!?
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:. . . treating a function as if it's an object . . .

Surely you are telling the compiler to create an object to encapsulate that function.
 
Stephan van Hulst
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure, because functions are still second-class citizens in Java. Functional interfaces were meant to blur the distinction and I believe it's best to treat them as if they're first-class citizens (even if they're not).

Holding on tightly to the idea that lambda's are actually wrapped in an anonymous class is exactly what makes it difficult for people to get familiar with their usage.

If Java allowed you to use the method invocation operator on variables, there would be almost no difference with first-class functions. One could write such a language that compiles to Java byte code.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:One could write such a language that compiles to Java byte code.


And somebody has already, haven't they? - Scala.

Winston
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic