Home > Enterprise >  <E> and <Object> differences and usage
<E> and <Object> differences and usage

Time:04-22

Can someone explain me what are the differences by using E or Object for example in a class for Lists, and their singular usage and definition. I have to use them in LinkedLists to implement methods.

CodePudding user response:

Assuming that E comes from element, i would consider using generics in Collections when the method that compiles says that it took an array of a certain type, and returns an array of the same type.

On the other hand you can't mix oranges and apples. You would be able to add an Object to your String list if you could pass a string list to a method that expects object lists. (And not all objects are strings)

CodePudding user response:

The syntax you've written as <E> refers to Generics.

Generics provide a way to re-use the same code generalized for different data types while still maintaining strict type checks at compile time. For example, as you mentioned, If you'd use a data structure handling elements of type Object, you wouldn't have type safety when adding or getting its elements because any class instance could be passed and retrieved from it. This would lead to error-prone code where elements retrieved from your data structure should be cast first to the expected data type in order to be used or in the worst scenario not being of the right type and knowing this only at run-time with no way to prevent it.

Because of this, Generics aid the developer allowing him/her to establish for each different situation the generic Type Parameter with a specific Type Argument (a non-primitive type). This not only provides much more flexibility and re-usability of your code, but once you've established that a generic Type Parameter E stands for a specific data type (Integer, String or other), then the compiler will make sure that every operation that your object will perform with the Type Parameter E will be compliant with the specific Type Argument you've put in place of E.

public class Test {
    public static void main(String[] args) {
        //Here I can add mixed non-primitive types because they're also instances of Object and therefor compliant to the expected type
        List listObj = new ArrayList();
        listObj.add(Integer.valueOf(5));
        listObj.add("Hello");
        listObj.add(Double.valueOf(3.77));

        //Here I can't retrieve the elements with their right type even though I know I've placed an Integer, a String and a Double in its first, second and third position
        Integer i1 = listObj.get(0);
        String s1 = listObj.get(1);
        Double d1 = listObj.get(2);
        
        
        //Here I'm defining a generic list working only with Integer (for this specific instance)
        List<Integer> listGenInt = new ArrayList<>();
        listGenInt.add(Integer.valueOf(5));
        //If I try to add any other type but Integer I'd get an error at compile time
        listGenInt.add("Hello");
        listGenInt.add(Double.valueOf(3.77));
        
        //Here I can only expect to retrieve elements of the data type I've established for this object during its declaration (Integer)
        Integer i2 = listGenInt.get(0);
        //Here I'd get an error If I'd try read its elements with any data type but Integer
        String s2 = listGenInt.get(1);
        Double d2 = listGenInt.get(2);

        
        //Here I'm defining a generic list working only with String (for this specific instance)
        List<String> listGenStr = new ArrayList<>();
        listGenStr.add("Hello");
        //If I try to add any other type but String I'd get an error at compile time
        listGenStr.add(Integer.valueOf(5));
        listGenStr.add(Double.valueOf(3.77));

        //Here I can only expect to retrieve elements of the data type I've established for this object during its declaration (String)
        String s3 = listGenStr.get(1);
        //Here I'd get an error If I'd try read its elements with any data type but String
        Integer i3 = listGenStr.get(0);
        Double d3 = listGenStr.get(2);
    }
}

Here, there is also a link with examples and further explanations from the official tutorials by Oracle.

https://docs.oracle.com/javase/tutorial/extra/generics/intro.html

  • Related