DEV Community

jguo
jguo

Posted on

2 2

Java functional vs OO programing

Hi Java guys,
I want to know how you guys use java. My previous job still uses OOP (except simple lambda and stream). Now, I joined Oracle. I found they use functional programing almost everywhere. How do you guys do your work?

Note, when I say functional programming, I am not just talking about simple lambda and stream functions. I especially reference if you use functions in the return type and method parameters.

During my previous job, I tried to avoid using functions in returns and method parameters. Since there are saying that function is not performing that well. I still think that is probably true. Functions are compiled to anonymous classes in behind.

For example,

Functional:

public Supplier<String> someMethod() {
return () -> "Hello!"
}

or

callMethod(() -> "Hello!")
Enter fullscreen mode Exit fullscreen mode

More OOP style.

private Supplier<String> hello = () -> "Hello!";

public Supplier<String> someMethod() {
return hello;

or 

callMethod(hello);

Enter fullscreen mode Exit fullscreen mode

Let me know your ideas.

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (2)

Collapse
 
sarahk profile image
Sarah

If by functional programming you mean the new lib introduced in Java 8 - yes I use it all the time. But you have to still use OOP, are you saying the two are mutally exclusive?

I use lambdas, method references, Consumer, Supplier, Function, Optional types, Streams (collect, reduce, map, filter, etc) and I'm not aware of any performance issues.

Collapse
 
jiayanguo profile image
jguo • Edited

I am not saying they are mutually exclusive. Let's use an example.

Functional:

public Supplier<String> someMethod() {
return () -> "Hello!"
}

or

callMethod(() -> "Hello!")

More OOP style.

private Supplier<String> hello = () -> "Hello!";

public Supplier<String> someMethod() {
return hello;

or 

callMethod(hello);

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay