Home >Java >javaTutorial >Detailed explanation of unary operators in Java language

Detailed explanation of unary operators in Java language

黄舟
黄舟Original
2017-09-21 10:43:482605browse

This article mainly introduces the analysis of unary operator instances in the Java language. Friends in need can refer to it.

Unary operator, also called single operator, unary operator, unary operator, English name: UnaryOperator.

Description: Accepts a parameter of type T, and the return value type is also T.

Source code:


public interface UnaryOperator<T> extends Function<T, T> {  /**
   * Returns a unary operator that always returns its input argument.
   *
   * @param <T> the type of the input and output of the operator
   * @return a unary operator that always returns its input argument
   */  static <T> UnaryOperator<T> identity() {
    return t -> t;
  }
}

Test code:


@Test
  public void test(){
    super.print(UnaryOperator.identity().apply(1));//输出1    
    super.print(UnaryOperator.identity().apply(false));//输出false    
    super.print(UnaryOperator.identity().apply("string"));//输出string  }

Summary

The above is the detailed content of Detailed explanation of unary operators in Java language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn