Is it a conceptual mistake to use the term “function” instead of the “method” in java 7 and java 8?

Would it be confusing to use the interchangeable terms "function" vs "method" in java7 and java8?

I know that java 8 introduced some kind of concept, such as lambda calculus, which allows you to insert small bits of a functional paradigm.

Functions, as in lambda calculus, functional programming.

+5
source share
3 answers

You can probably avoid the interchangeability of functions and methods during informal conversations, but if you want to be precise, remember that:

  • the method is always associated with any object or class, the function is not (although in Java you cannot define functions outside the class or object), Function
  • should always return a value, the method may not return anything ( void return type is not a real value).

In general - now you know that there is a difference between a function and a method, so just keep that in mind and now use the correct term.

+2
source

Speaking of Java-8, you can use the word “function” to mean a functional interface (an interface with a single abstract method), a class that implements such an interface, or an expression such as a functional interface (including an expression or Help lambda method). However, you should not name methods as functions. This becomes even more important than in previous versions of Java, as if you had seen the word “function” in a discussion related to Java-7, you could assume that “method” was meant. But in Java-8, people might think that you are talking about a functional interface, not a method.

So good to say:

The Stream.map method takes a function as a parameter.

or

Interface

A Collector combines four functions: supplier, battery, totalizer and finisher.

or

I am passing an Objects::nonNull function to the Objects::nonNull method.

But the following would look strange:

I get a strange exception when I use the Math.sqrt function.

+3
source

Functions differ from methods regardless of java version. Although with java 8, these terms are now more meaningful. With earlier versions of java, you can also see functions as static methods, since they are not bound to class data.

+1
source

Source: https://habr.com/ru/post/1235282/


All Articles