Is it required to specify type of data to both diamond operators while creating object?

Tags: ,



I am creating an object of ArrayAdapter in Java(Android Studio) is it required to add String Data type to both sides inside the diamond operators? can anyone explain

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<"here">(this,android.R.layout.simple_list_item_1,numbersInChars);

Answer

No, you do not need to specify it on the right side. However, it won’t cause any harm if you specify there. Please read more about the diamond at https://docs.oracle.com/javase/tutorial/java/generics/types.html. Given below is an example from the same page:

Box<Integer> integerBox = new Box<>();

As you can see Integer has not been specified on the right side. However, if you wish, you can write it as follows as well:

Box<Integer> integerBox = new Box<Integer>();


Source: stackoverflow