• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question about ArrayList  RSS feed

 
Travis Roberts
Ranch Hand
Posts: 54
C++ Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Java Ranchers:

In prepping for my second Java test, I was given the following problem which I got wrong:



The compiler does not recognize ArrayList. So the answer is compiler error.

But when I add "import java.util.*; and compile again, I get the following:

C:\Practice Test Code>javac Xy144.java
Note: Xy144.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

C:\Practice Test Code>javac -Xlint:unchecked Xy144.java
Xy144.java:15: warning: [unchecked] unchecked call to add(E) as a member of the
raw type ArrayList
ref.add("ABC ");
^
where E is a type-variable:
E extends Object declared in class ArrayList
Xy144.java:16: warning: [unchecked] unchecked call to add(E) as a member of the
raw type ArrayList
ref.add("DEF ");
^
where E is a type-variable:
E extends Object declared in class ArrayList
Xy144.java:17: warning: [unchecked] unchecked call to add(E) as a member of the
raw type ArrayList
ref.add("GHI");
^
where E is a type-variable:
E extends Object declared in class ArrayList
3 warnings


Can anyone decifer this? It mentions "raw type" which I do not understand yet. Its references to "E" I assume is something to do with a template meaning that it will accept any data type?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ArrayList is a generic class. You're using it without generics, so you get warnings.

Using a generic class without the generics is possible for backward compatibility with old Java versions (Java 1.4 and older didn't have generics), but you shouldn't do that in new code, so you get warnings. A type that takes type parameters, but without the type parameters, is called a raw type.

you should have declared the list like this:

One other point to note: use the interface List instead of the class ArrayList for the type of the variable. This is a principle called "program to an interface, not an implementation". The rest of the program only needs to know that ref is a List, it doesn't need to know what specific implementation of List you use. Programming to an interface makes it easier to change to another implementation later.
 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In general, if you're going to do this:



...what you're saying is, I'm adding a string to the ArrayList. So why not tell the compiler that you intend to add strings and only strings?



Now the compiler can check whether you're adding the correct type to the list.

Even better, use the interface as the type of your List:

 
Abhay Agarwal
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me help you out

First of all, all you get is warning messages. There is no compilation error in your code.

"Raw type" refers to Generic feature of Java. Generics feature was introduced in Java 5 version. , Collections API (List, Set, Map ets) have been re-written in Java 5 to include Generics feature in it.

When you define ArrayList commonList = new ArrayList(); , you are telling to JVM that any Object (String, Boolean, Integer, Custom Object etc) can go inside my "commonList". There is no check being implied by JVM when you insert objects in "commonList". You can insert 2 String, 1 Integer, 1 Float type values in this list.

When you define ArrayList<String> stringList = new ArrayList<String>(); (to follow design principle - coding to interfaces - List<String> stringList = new ArrayList<String>();) , you are telling to JVM that only String objects can go inside "stringList ". There is check being implied by JVM when you insert objects in "stringList ". You can NOT insert any objects other than Strings object. For example, if you try to insert Integer object in this list, you shall get compilation error.

"E" means Element in Generics terminology. Refer to ArrayList documentation here "http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#add%28E%29". Notice add() method signature. It uses "E" element.

Now coming to your warings, your question may - why do I get warnings , not compilation error for that code that I have written. This is because Java has to maintain backward compatibility with older non-Generics version of Java (4 and below). Therefore, you get warnings which tells you that you are using Java version 5 (or above) but you are not using Generics feature in your code.

Just to start with Generics, refer to this Wiki URL "https://en.wikipedia.org/wiki/Generics_in_Java".
 
Travis Roberts
Ranch Hand
Posts: 54
C++ Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you! That explains a lot. I remember templates from C++ class, it sounds like generics is similiar for Java. Generics is covered in the intermediate Java class so I am sure I'll be tackling that pretty soon.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On the surface, generics looks a little bit like templates in C++, but it's not exactly the same. Watch out that you don't think "I know how templates work in C++, so now I know exactly how generics work in Java!".

In C++, templates are used to generate code, if you use a template type with a specific type parameter then the C++ compiler will fill in the template and generate a specific version of the template with the specific type that you're using it with. That's not how generics work in Java; they are not templates in the sense that the Java compiler will not generate code from the generic class, like the C++ compiler does with a template. In Java, a generic type is just a single class, not a template from which code is generated. If you think the C++ way, you'll sooner or later discover that Java generic types work in a different way than C++ templates.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!