-3

Scenario: A method addList in class expects a List of Integer objects.
main method sends an ArrayList argument containing objects of multiple types, not just Integer.

Result: Call to method addList is successful and it is also possible to retrieve the objects stored in the ArrayList and print them.

Question: Should not we expect that on run-time when the list is received in the called method, its content will be validated and it will result in some exception?

However, in addList method, if I am trying to insert a non-Integer object in the ArrayList, it fails at compile time only.

Does this conclude that Diamond operator constraint is only while inserting the objects in a Collection?

Please note, erasure-in-generics is different question and doesn't provide answer to this question.

package com.rnd.nirav;

import java.util.ArrayList;
import java.util.List;

public class OverRidingTest {

public static void main(String[] args) {

    List list = new ArrayList<>();
    list.add(Integer.valueOf(1));
    list.add("Nirav");
    list.add(Float.valueOf(1.1f));

    addList(list);

}

public static void addList(List<Integer> list) {
    list.add("Khandhedia"); // This fails as expected.
    System.out.println("List size = " + list.size());
    Iterator itr = list.iterator();
    while(itr.hasNext())
    {
        System.out.println("Element is " + itr.next());
    }
}

}
Community
  • 1
  • 1
Nirav Khandhedia
  • 191
  • 1
  • 4
  • 17

2 Answers2

-1

I recommend you a quick pass through an Oracle tutorial. Basically Generics are a hint for the compiler and nothing more. They are evaluated at compile-time.

-1

ok if you are talking about that diamond operator, it is called as generics

Generics are evaluated at compile time and not at run time

why generics ??

the only reason is generics provide type safety consider following example

public static void addContent() {
        List<Integer> list = new LinkedList<>();
        for (int i = 0; i < 10; i++) {
            list.add(new Integer(i));
        }

    }

Why we Actually need generics??

So that our content can't be mutated like you do not want to add or mix salt to sugar and here you cannot add any other type of object and so becomes easy to access and process(like its easy to access one type of objects). eg.

for (Integer i : list) {
            System.out.println(i);
        } 

What that diamond shaped operator does?

It provides type safety at compile time like helps to stop while adding salt to sugar like

list.add("hello"); //not possible

Why it doesn't supports int , float,char but only wrapper class?

Because java revolves around objects only and we all know int,float and char types do not have object

one more thing you will notice <? extends/super SomeType> ,whats that

Its simple inheritance and polymorphism.

Why your code is not checking for one particular type

its because you haven't bounded your code to any type and thus you can add any object to collection

Edit 1 : Your code is not failing its working perfectly

Edit :

What ? represents ?? ? represents Object class when used as Type<?> ,means you cannot add anything but objects of Object class only.

bananas
  • 1,176
  • 2
  • 19
  • 39