English 中文(简体)
Java 通用 T v. Object
原标题:Java generics T vs Object

我想知道以下两种方法声明之间的区别:

public Object doSomething(Object obj) {....}

public <T> T doSomething(T t) {....}

是否有东西可以,但不是其他? 我无法在这个地点其他地方找到这个问题。

最佳回答

脱离情况——没有区别。 t和obj 页: 1 目标

但是,在环境方面,如果你有一个通用类别:

MyClass<Foo> my = new MyClass<Foo>();
Foo foo = new Foo();

然后:

Foo newFoo = my.doSomething(foo);

2. 标有物体的混合代码

Foo newFoo = (Foo) my.doSomething(foo);

两项优势:

  • no need of casting (the compiler hides this from you)
  • compile time safety that works. If the Object version is used, you won t be sure that the method always returns Foo. If it returns Bar, you ll have a ClassCastException, at runtime.
问题回答

这里的区别是,首先,我们具体规定,打电话者必须通过反对(任何一类),并将重新提出另一个目标(任何一类,不一定相同)。

在第二种情况下,所交回的类型将与确定该类别时的类型相同。

Example ex = new Example<Integer>();

在此,我们具体说明哪类T将使我们能够对某类或方法施加更多的限制。 例如,我们可以立即发出<条码>LinkedList<Integer>或LinkedList<Example>。 我们知道,当我们称之为其中一种方法时,我们就会重回Integer或Example例。

这里的主要目标是,电话法可以规定某类物体的类型,而不是依赖类型预测来执行。

Java Generics

* 更新链接。

不同之处在于,采用通用方法,我不需要去做,如果我有错的话,我就会出现汇编错误:

public class App {

    public static void main(String[] args) {

        String s = process("vv");
        String b = process(new Object()); // Compilation error
    }

    public static <T> T process(T val) {

        return val;
    }
}

Using object I always need to cast and I don t get any errors when I do wrong:

public class App {

    public static void main(String[] args) {

        String s = (String)process("vv");
        String b = (String)process(new Object());
    }

    public static Object process(Object val) {

        return val;
    }
}

你们不需要做额外的分类。 首先,你们总是会得到一门 j。 你们需要向你们阶层提出反对。 在第二种情况下,T将被通用签名中定义的类别所取代,不需要进行分类。

无。 但是,在汇编时间时,二者将进行类型核对,以确保参数的类型和返回价值匹配的类型(或属于亚类)达到任何类型的T决心(第一例也是检查类型,但每一物体都是反对的亚类,因此每一类目标都会被接受)。

T是一种通用类型。 剂量可由任何合格的物体代用。 您可援引以下方法:

String response = doSomething(“hello World”);

页: 1

MyObject response = doSomething(new MyObject());

页: 1

<编码> Integer response = doSomething(31);

你可以看到,这里存在多变。

但是,如果宣布返回该国,它就会返回。 目标,除非你打造物品,否则你可以这样做。

in the first case it takes a parameter of any type e.g.string and return a type foo. In the second case it takes a parameter of type foo and returns an object of type foo.

没有什么理由可以认为一般性原则凌驾于他人之上。 Java的目标类型:

  1. Generics is flexible and safe. At the same time, working with Object that requires type-casting is error-prone
  2. Type Casting in Java is slow ref : [1]: https://www.infoworld.com/article/2076555/java-performance-programming--part-2--the-cost-of-casting.html




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签