简体   繁体   English

<E>语法在Java中意味着什么?

[英]What does the <E> syntax mean in Java?

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? <E>表示什么? 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. 我依稀记得箭头括号与矢量有关,但基于上面的代码,我感觉它与枚举有关。

Any assistance or clarification would be greatly appreciated. 任何协助或澄清将不胜感激。 Thank you. 谢谢。

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. Java编译器将强类型检查应用于通用代码,并在代码违反类型安全性时发出错误。 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 

Here <E> denotes the type parameter of Node class .The type parameter defines that it can refer to any type (like String, Integer, Employee etc.). 这里<E>表示Node类的类型参数.type参数定义它可以引用任何类型(如String,Integer,Employee等)。 Java generics have type parameter naming conventions like following: Java泛型具有类型参数命名约定,如下所示:

  1. T - Type T型
  2. E - Element E - 元素
  3. K - Key K - 钥匙

  4. N - Number N - 数字

  5. V - Value 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 Node class 'E' will reference the String type and class will be look like following 对于上面的场景,在后台, Node类'E'将引用String类型,类将如下所示

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-interface https://www.javatpoint.com/generics-in-java https://www.journaldev.com/1663/java-generics-example-method-class-interface https://www.javatpoint.com/generics-in-java

Node imply 'Generics' class. 节点暗示'泛型'类。

Refer : http://docs.oracle.com/javase/tutorial/java/generics/types.html 请参阅: http//docs.oracle.com/javase/tutorial/java/generics/types.html

The is the java syntax way of indicating a Generic Type. 这是指示泛型类型的java语法方式。 Essentially this just means that it can be a Node object that can take on any type at all. 从本质上讲,这只意味着它可以是一个可以采用任何类型的Node对象。

you should check this out for a good turorial on java generics 你应该检查这一点 ,以获得java泛型的良好版面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM