<E> 语法在 Java 中是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20255911/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 00:37:27  来源:igfitidea点击:

What does the <E> syntax mean in Java?

javasyntax

提问by d.lanza38

I've quickly googled for an answer but could not not find/think of accurate search parameters.

我很快用谷歌搜索了答案,但找不到/想不出准确的搜索参数。

I am teaching myself Java, but can't seem to find the meaning of a certain syntax.

我正在自学 Java,但似乎无法找到某种语法的含义。

public class Node<E>{
    E elem;
    Node<E> next, previous;
}

What does the <E>signify? I vaguely remember the arrow braces having something to do with vectors but based on the code above I get the feeling it has to do with enumerations.

是什么意思<E>?我依稀记得箭头括号与向量有关,但根据上面的代码,我觉得它与枚举有关。

Any assistance or clarification would be greatly appreciated. Thank you.

任何帮助或澄清将不胜感激。谢谢你。

采纳答案by blacktide

These are called Generics.

这些被称为泛型

In general, these enable types(classes and interfaces) to be parameters when defining classes, interfaces and methods.

通常,在定义类、接口和方法时,这些使类型(类和接口)成为参数。

Using generics give many benefits over using non-generic code, as shown the following from Java's tutorial:

使用泛型比使用非泛型代码有很多好处,如下 Java 教程所示:

  • Stronger type checks at compile time.A Java compiler applies strong type checking to generic code and issues errors if the code violates type safety. Fixing compile-time errors is easier than fixing runtime errors, which can be difficult to find.

    For example:

    // without Generics
    List list = new ArrayList();
    list.add("hello");
    
    // With Generics
    List<Integer> list = new ArrayList<Integer>();
    list.add("hello"); // will not compile
    
  • Enabling programmers to implement generic algorithms.By using generics, programmers can implement generic algorithms that work on collections of different types, can be customized, and are type safe and easier to read.

  • Elimination of casts.

    For example, the following code snippet without generics requires casting:

    List list = new ArrayList();
    list.add("hello");
    String s = (String) list.get(0);
    

    When re-written to use generics, the code does not require casting:

    List<String> list = new ArrayList<String>();
    list.add("hello");
    String s = list.get(0); // no cast
    
  • 在编译时进行更强的类型检查。Java 编译器对泛型代码应用强类型检查,并在代码违反类型安全时发出错误。修复编译时错误比修复运行时错误更容易,后者很难发现。

    例如:

    // without Generics
    List list = new ArrayList();
    list.add("hello");
    
    // With Generics
    List<Integer> list = new ArrayList<Integer>();
    list.add("hello"); // will not compile
    
  • 使程序员能够实现通用算法。通过使用泛型,程序员可以实现适用于不同类型集合的泛型算法,可以自定义,并且类型安全且易于阅读。

  • 消除铸件。

    例如,以下没有泛型的代码片段需要强制转换:

    List list = new ArrayList();
    list.add("hello");
    String s = (String) list.get(0);
    

    当重写为使用泛型时,代码不需要强制转换:

    List<String> list = new ArrayList<String>();
    list.add("hello");
    String s = list.get(0); // no cast
    

回答by WillBD

The is the java syntax way of indicating a Generic Type. Essentially this just means that it can be a Node object that can take on any type at all.

这是指示泛型类型的 java 语法方式。本质上,这只是意味着它可以是一个可以采用任何类型的 Node 对象。

you should check thisout for a good turorial on java generics

你应该检查一下这个关于java泛型的好教程

回答by code2use

回答by SHB

Here <E>denotes the type parameter of Nodeclass .The type parameter defines that it can refer to any type (like String, Integer, Employee etc.). Java generics have type parameter naming conventions like following:

这里<E>表示Node类的类型参数。类型参数定义了它可以引用任何类型(如String、Integer、Employee等)。Java 泛型具有如下类型参数命名约定:

  1. T - Type
  2. E - Element
  3. K - Key

  4. N - Number

  5. V - Value

  1. T - 类型
  2. E - 元素
  3. K - 键

  4. N - 数字

  5. V - 值

For example take the following scenerio

例如采取以下场景

public class Node<E>{
   E elem;
   Node<E> next, previous;
}
class Test{
   Node<String> obj = new Node<>();
}

For the above scenerio, in background the Nodeclass 'E' will reference the Stringtype and class will be look like following

对于上面的场景,在后台节点类“E”将引用字符串类型,类将如下所示

public class Node<String>{
   String elem;
   Node<String> next,previous;
}

Not only String you can also use Integer,Character,Employee etc as a type parameter.

不仅 String 您还可以使用 Integer、Character、Employee 等作为类型参数。

You can use generics with Interface,method and constructor too.

您也可以将泛型与接口、方法和构造函数一起使用。

For more about generics visit these:

有关泛型的更多信息,请访问这些:

https://www.journaldev.com/1663/java-generics-example-method-class-interfacehttps://www.javatpoint.com/generics-in-java

https://www.journaldev.com/1663/java-generics-example-method-class-interface https://www.javatpoint.com/generics-in-java