search
HomeJavaHow is this possible in Java generics?

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) { // ... }

The following calls can be made:

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

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

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);
}

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);
}

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;
        }
    }
}

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
This article is reproduced at:stackoverflow. If there is any infringement, please contact admin@php.cn delete

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use