0

Many questions about the differences between int and Integer in java (one example). Still, I couldn't find any reference why int is forbidden as a template type argument:

import java.util.ArrayList;

class Main {
    public static void main(String[] args) {
        // ArrayList<Integer> a = new ArrayList<Integer>();
        ArrayList<int> a = new ArrayList<int>();
        a.add(2);
        a.add(4);
        System.out.println(a);
    }
}

Here is the compilation error I get:

$ javac Main.java
Main.java:6: error: unexpected type
        ArrayList<int> a = new ArrayList<int>();
                  ^
  required: reference
  found:    int
Main.java:6: error: unexpected type
        ArrayList<int> a = new ArrayList<int>();
                                         ^
  required: reference
  found:    int
2 errors

When I use an Integer instead (commented line) everything is fine

OrenIshShalom
  • 5,974
  • 9
  • 37
  • 87

0 Answers0