Home Java javaTutorial How to use generics in Java functions? Step analysis

How to use generics in Java functions? Step analysis

Apr 26, 2024 pm 09:18 PM
java Generics

Applying generics in Java functions can achieve code reuse. The steps are as follows: Declare a generic type variable, such as <T> represents a generic type variable. Use generic types in functions, such as parameter types or return value types. When making a specific call, specify the generic type actual parameter. For example, when calling printArray, specify the actual parameter String. Generic functions can be reused, for example swap() can be used to swap the positions of elements of different types in an array.

Java 函数中如何应用泛型?步骤解析

#How to apply generics in Java functions? Step analysis

Generics are a powerful tool in the Java language for specifying the type of a function or class when writing code. By using generics, you can create reusable code that works for multiple data types without having to write separate functions or classes for each type.

Steps to apply generics:

  1. Declare a generic type variable: In a function, use angle brackets < > to declare one or more generic type variables. For example:

    public &lt;T&gt; void printArray(T[] arr) {
        // ...
    }
    Copy after login

    Here, T in angle brackets <> is a generic type variable.

  2. Use generic types in functions: In the function body, use generic type variables as function parameters, return value types, or types of local variables. For example:

    public &lt;T&gt; T max(T a, T b) {
        if (a.compareTo(b) &gt; 0) {
            return a;
        } else {
            return b;
        }
    }
    Copy after login
  3. Specify generic type actual parameters: When you call a generic function, you need to specify generic type actual parameters. This will tell the compiler which actual type should be used. For example:

    String[] arr = {&quot;a&quot;, &quot;b&quot;, &quot;c&quot;};
    printArray(arr); // 泛型类型实参为 String
    Copy after login

Practical case:

Let us create a generic function swap() Swap two elements in the array Position of elements:

public &lt;T&gt; void swap(T[] arr, int i, int j) {
    T temp = arr[i];
    arr[i] = arr[j];
    arr[j] = temp;
}
Copy after login

Usage:

Integer[] arr = {1, 2, 3};
swap(arr, 0, 2);

System.out.println(Arrays.toString(arr)); // 输出:[3, 2, 1]
Copy after login

The above is the detailed content of How to use generics in Java functions? Step analysis. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Square Root in Java Square Root in Java Aug 30, 2024 pm 04:26 PM

Square Root in Java

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Perfect Number in Java

Random Number Generator in Java Random Number Generator in Java Aug 30, 2024 pm 04:27 PM

Random Number Generator in Java

Armstrong Number in Java Armstrong Number in Java Aug 30, 2024 pm 04:26 PM

Armstrong Number in Java

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Weka in Java

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Smith Number in Java

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

Java Spring Interview Questions

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Break or return from Java 8 stream forEach?

See all articles