Table of Contents
Question content
Solution
Home Java How is this possible in Java generics?

How is this possible in Java generics?

Feb 08, 2024 pm 11:20 PM

php editor Apple will answer for you: In Java generics, the question "How is this possible in Java generics?" is actually possible. Because Java generics allow the use of wildcards to represent undefined types, such as using "?" to represent any type. When we define a generic method or generic class, we can use wildcards to limit parameter types or return value types to achieve some specific functions. Although in some cases, there may be some limitations due to type erasure, with reasonable design and use, we can achieve many seemingly impossible operations in Java generics.

Question content

I just noticed something that is very counterintuitive to me when it comes to Java generics. Let’s take a look at this method:

public static <T> void inspect(T a, T b) { // ... }
Copy after login

The following calls can be made:

inspect(new Integer(3), new String("What? How?"))
Copy after login

I think once the type T is derived, it must be consistent, just like two strings or two integers. This doesn't make much sense, because if I have the following line in my method:

T tmp
Copy after login

What is T?

Can anyone explain?

Solution

The main result is that both Integer and String are implemented from Serialized.

So your code equals:

public static <T extends Serializable> void inspect(T a, T b) {
    System.out.println(a + "_" + b);
}
Copy after login

If changed to blow code, only valid in Integer or Number subclasses.

public static <T extends Number> void inspect(T a, T b) {
    System.out.println(a + "_" + b);
}
Copy after login

Here's a better example:

public class MyTest {

    @Test
    public void demo() {
        inspect(new FirstSon("a"), new SecondSon("b"));
    }

    public <T> void inspect(T a, T b) {
        System.out.println(a + "_" + b);
    }


    interface Parent {
    }

    static class FirstSon implements Parent {
        private String name;

        public FirstSon(String name) {
            this.name = name;
        }
    }

    static class SecondSon implements Parent {
        private String name;

        public SecondSon(String name) {
            this.name = name;
        }
    }
}
Copy after login

The above is the detailed content of How is this possible in Java generics?. 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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

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)