What does the <E> syntax mean in Java?

JavaSyntax

Java Problem Overview


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.

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.

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

Java Solutions


Solution 1 - Java

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:

  • 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
    

Solution 2 - Java

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.). Java generics have type parameter naming conventions like following:

  1. T - Type

  2. E - Element

  3. K - Key

  4. N - Number

  5. V - Value

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

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.

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

Solution 3 - Java

Solution 4 - Java

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.

you should check this out for a good turorial on java generics

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiond.lanza38View Question on Stackoverflow
Solution 1 - JavablacktideView Answer on Stackoverflow
Solution 2 - JavaSHBView Answer on Stackoverflow
Solution 3 - Javacode2useView Answer on Stackoverflow
Solution 4 - JavaWillBDView Answer on Stackoverflow