Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the <E> syntax mean in Java?

Tags:

java

syntax

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.

like image 972
d.lanza38 Avatar asked Nov 28 '13 00:11

d.lanza38


People also ask

What is E in Java?

The java. lang. Math. exp() is used to return the Euler's number e raised to the power of a double value. Here, e is an Euler's number and it is approximately equal to 2.718281828459045.

What is E in ArrayList E?

ArrayList <E> list = new ArrayList <> (); The important thing here out is that E here represents an object datatype imagine be it Integer here. The Integer class wraps a value of the primitive type int in an object. An object of type Integer contains a single field whose type is int.

What is E in collection in Java?

The add(E element) of java. util. Collection interface is used to add the element 'element' to this collection. This method returns a boolean value depicting the successfulness of the operation. If the element was added, it returns true, else it returns false.

What is generic type E in Java?

For example, the type java. util. List<E> is a generic type: a list that holds elements of some type represented by the placeholder E . This type has a method named add() , declared to take an argument of type E , and a method named get() , declared to return a value of type E .


1 Answers

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 
like image 68
blacktide Avatar answered Oct 08 '22 20:10

blacktide