Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I write an anonymous function in Java?

Is it even possible?

like image 349
aarona Avatar asked May 02 '10 23:05

aarona


People also ask

How do you make an anonymous function?

Anonymous Function is a function that does not have any name associated with it. Normally we use the function keyword before the function name to define a function in JavaScript, however, in anonymous functions in JavaScript, we use only the function keyword without the function name.

What is the purpose of anonymous function in Java?

It is an inner class without a name and for which only a single object is created. An anonymous inner class can be useful when making an instance of an object with certain “extras” such as overriding methods of a class or interface, without having to actually subclass a class.

What is anonymous function or class in Java?

Anonymous functions: a function that isn't bound to a name/variable. In Java it means a function that doesn't belong to a class, but rather one that's implemented as defined by an implementation in a functional interface.

What is anonymous in Java?

Anonymous classes enable you to make your code more concise. They enable you to declare and instantiate a class at the same time. They are like local classes except that they do not have a name. Use them if you need to use a local class only once.


2 Answers

if you mean an anonymous function, and are using a version of Java before Java 8, then in a word, no. (Read about lambda expressions if you use Java 8+)

However, you can implement an interface with a function like so :

Comparator<String> c = new Comparator<String>() {     int compare(String s, String s2) { ... } }; 

and you can use this with inner classes to get an almost-anonymous function :)

like image 142
chris Avatar answered Sep 27 '22 22:09

chris


Here's an example of an anonymous inner class.

System.out.println(new Object() {     @Override public String toString() {         return "Hello world!";     } }); // prints "Hello world!" 

This is not very useful as it is, but it shows how to create an instance of an anonymous inner class that extends Object and @Override its toString() method.

See also

  • JLS 15.9.5 Anonymous Class Declarations

Anonymous inner classes are very handy when you need to implement an interface which may not be highly reusable (and therefore not worth refactoring to its own named class). An instructive example is using a custom java.util.Comparator<T> for sorting.

Here's an example of how you can sort a String[] based on String.length().

import java.util.*; //...  String[] arr = { "xxx", "cd", "ab", "z" }; Arrays.sort(arr, new Comparator<String>() {     @Override public int compare(String s1, String s2) {         return s1.length() - s2.length();     }            }); System.out.println(Arrays.toString(arr)); // prints "[z, cd, ab, xxx]" 

Note the comparison-by-subtraction trick used here. It should be said that this technique is broken in general: it's only applicable when you can guarantee that it will not overflow (such is the case with String lengths).

See also

  • Java Integer: what is faster comparison or subtraction?
    • Comparison-by-subtraction is broken in general
  • Create a Sorted Hash in Java with a Custom Comparator
  • How are Anonymous (inner) classes used in Java?
like image 29
polygenelubricants Avatar answered Sep 27 '22 22:09

polygenelubricants