Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it conceptual mistake to use term "function" instead of "method" in java 7 and java 8?

Will this cause confusion to use interchangeably terms "function" vs "method" in java7 and java8?

I know that java 8 introduced some concept like lambda calculus, which allows inserting small bits of functional paradigm to be used.

Functions as in lambda calculus, functional programming.

like image 976
ERJAN Avatar asked Nov 05 '15 09:11

ERJAN


3 Answers

When speaking about Java-8 you may use the word "function" to refer to the functional interface (interface with single abstract method), the class implementing such interface or the expression of the functional interface type (including lambda expression or method reference). You should not however call methods as functions. This become even more important than in previous Java versions as if you see the word "function" in Java-7 related discussion, you can assume that the "method" was meant. But in Java-8 people may think that you are speaking about functional interface, not about method.

So it's ok to say:

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

Or

A Collector interface aggregates four functions: supplier, accumulator, combiner and finisher.

Or

I pass the Objects::nonNull function to the Stream.filter method.

But the following would sound confusing:

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

like image 117
Tagir Valeev Avatar answered Sep 28 '22 11:09

Tagir Valeev


You probably may get away with interchanging function and method during non formal talk but if you want to be precise keep in mind that:

  • method is always associated with some object or class, function is not (although in Java you can't define functions outside class or object),
  • function must always return a value, method might not return anything (void return type is not a real value).

In general - you now know that there's a difference between a function and a method so just keep it in mind and use proper term from now on.

like image 38
matt Avatar answered Sep 28 '22 10:09

matt


Functions are different from methods irrespective of java version. Though with java 8 these terms are more meaningful now. With earlier versions of java also, you can see functions as the static methods as these are not bound to the class data.

like image 37
javdev Avatar answered Sep 28 '22 10:09

javdev