#println with :: vs .
40 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @jade moss! Please use
/closeor theClose Postbutton above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Ping me when you reply. Gonna continue watching the Youtube Tutorial. Vid link for reference: https://youtu.be/UgX5lgv4uVM?si=49zi_RgMWt7_hyVr&t=2537
Welcome to this Spring Boot Crash Course! In this video, you'll gain the foundational knowledge of Spring Boot and how to hit the ground running with the Spring Framework. We'll discuss key concepts of the Spring Framework such as Beans, Application Context, Inversion of Control, Dependency Injection, and more. You will learn how to build a new ...
System.out.println() is a method call - it calls the println method which prints something
System.out::println is a method reference
it is equivalent to something -> System.out.println(something)
Ok that's interesting
So its like map function in Python?
No, the forEach is the map() like function.
Just that to call the method reference is inside it
it's a higher order function,. yeah
you are passing the method System.out.println() to forEach
But instead of System.out.println() I would use System.out::println, right?
you can do the following
Consumer<String> p = System.out::println;
p.accept("Hi");//This calls the System.out.println() since p references that
forEach expects a Consumer which it then calls multiple times
If I made a class with a static method like World.greet() where it takes a string as arg to print out Hello {name}. Then I could use this in the forEach as World::greet?
and https://dev.java/learn/lambdas/method-references/ specifically
yes
then it would print Hello <first string>, Hello <second string> etc
Ah then I understood that correctly.
with lambdas, you can essentially store a method/code in a variable (or parameter)
but https://dev.java/learn/lambdas/ explains the topic in more detail
Yeah I'm familiar with lambda expressions. They're like anonymous functions in Javascript and similar in Python
That makes sense
In Java they're technically implementations of an interface, but yes
passing an implementation of an interface is essentially equivalent to passing the code
the difference is that it extends Object xd
I find this to be more explicitly readable
IntBinaryOperator max = (a, b) -> Integer.max(a, b);
Compared to this
IntBinaryOperator max = Integer::max;
Is there a preference in Java community on Lambda expressions?
I use the method if it exists
Code that I didn't write, is less error prone in general, so I prefer "not my code"
Yeah I guess. Same as how I would do it in Python.
In general, if it exists in the standard library I use it
it's a matter of preference
I would personally also use method references
Also there's a special edge case with generic checked exceptions where it's possible to do something with method reference that isn't possible with lambdas
sneakyThrow? Or something else still?
Alright thanks all
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.