The following code is valid:
List list = new ArrayList();
list.add(1);
This is because compiler make boxing/unboxing automatically.
My question is: can I somehow get compilation (or runtime) error in such situation?
I already try:
1) method that return primitive:
int foo(){
return 2;
}
list.add(method());
2) anonymous class from java 8:
list.add(((Function) (ignored) -> 3).apply(""));
UPD:
We can parametrize List to List<Object>
or even to List<Integer>
the result will be the same - it's work. My question is "how can I fool autoboxing mechanism?"