#println with :: vs .

40 messages · Page 1 of 1 (latest)

jade moss
#

Quick question about printing in Java. I'm watching this Spring Boot Tutorial and this guy did String.out::println where I was taught it was String.out.println. Difference is the :: vs . before the println. How did that :: work and when can I use this vs the other?

vague finchBOT
#

This post has been reserved for your question.

Hey @jade moss! Please use /close or the Close Post button 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.

jade moss
#

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 ...

▶ Play video
golden schooner
#

System.out::println is a method reference

#

it is equivalent to something -> System.out.println(something)

jade moss
#

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

golden schooner
#

it's a higher order function,. yeah

#

you are passing the method System.out.println() to forEach

jade moss
#

But instead of System.out.println() I would use System.out::println, right?

golden schooner
#

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

jade moss
#

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?

golden schooner
golden schooner
#

then it would print Hello <first string>, Hello <second string> etc

jade moss
#

Ah then I understood that correctly.

golden schooner
#

with lambdas, you can essentially store a method/code in a variable (or parameter)

jade moss
#

Yeah I'm familiar with lambda expressions. They're like anonymous functions in Javascript and similar in Python

fresh summit
#

In Java they're technically implementations of an interface, but yes

golden schooner
#

the difference is that it extends Object xd

jade moss
#

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?

fresh summit
#

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"

jade moss
#

Yeah I guess. Same as how I would do it in Python.

fresh summit
#

In general, if it exists in the standard library I use it

golden schooner
#

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

fresh summit
jade moss
#

Alright thanks all

vague finchBOT
# jade moss 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.