0

what is the difference between these two? in the first case why it allows int value in ArrayList?

                     ArrayList al = new ArrayList<String>(); 
                     al.add(1);
                     al.add(2);
                     System.out.println(al.get(1)); 
k_23
  • 11
  • 2
  • 4
    You should be using either `ArrayList al = new ArrayList<>();` or `List al = new ArrayList<>();`. – Andy Turner Jun 24 '20 at 19:12
  • without the <> is older java. It doesn't say what is going to be inside the list. So you can put inside whatever you want. Notice that this is bad practice. So as the comment above said. Use the one with <> – João Costa Jun 24 '20 at 19:20
  • 1
    @JoãoCosta I would say more strongly than *older* Java, it's *15-year-old* Java (Generics were introduced in 2005 with Java 5). – Andy Turner Jun 24 '20 at 19:35

0 Answers0