• Post Reply Bookmark Topic Watch Topic
  • New Topic

ArrayList unchecked call warning question  RSS feed

 
Liyana Ghafar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Book: OCA Oracle Certified Associate Java SE 8 Programmer 1 - Study Guide , by Jeanne Boyarsky and Scott Selikoff
page: 130-131
Chapter 3 : Understanding Java Arrays
Subtopic : Using an ArrayList [add()]

The example provided as below:-

Since add() is the most critical ArrayList method you need to know for the exam, we are going to show a few sets of examples for it. Let’s start with the most straightforward case:



add() does exactly what we expect: it stores the String in the no longer empty ArrayList. It then does the same thing for the boolean. This is okay because we didn’t specify a type for ArrayList; therefore, the type is Object, which includes everything except primitives. It may not have been what we intended, but the compiler doesn’t know that.



I tried the example but getting some error :-

warning: [unchecked] unchecked call to add(E) as a member of the raw type ArrayList
    ls.add("hawk");
          ^
  where E is a type-variable:
    E extends Object declared in class ArrayList
1 warning


Is the book example is wrong or I am missing something here? Please help
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16058
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not an error, it's a warning. There's a difference between these two: in case of an error, the compiler stops and does not produce a *.class file; in case of a warning, the compiler continues and you do get a *.class file.

You get this error because you are using the raw type ArrayList. Class ArrayList uses generics. you should normally use generics together with ArrayList to specify the type of objects you are going to store in your ArrayList. For example, if you have an ArrayList to store String objects:

Generics have not been in Java since the beginning, they were added in Java version 5 (which is already long ago). For backward compatibility reasons, it's still possible to leave off the generics, but you should really only do that when you're dealing with old code (from before Java 5). That's what this compiler warning is about - it's telling you that you are using the raw type ArrayList, without generics, and that you should be using generics.

Generics will make your code more type-safe; it gives the compiler more information to check if your program is correct, so it makes it harder for you to make mistakes when working with the ArrayList.
 
Mohammed Sardar.
Ranch Hand
Posts: 193
1
Java jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome To Java Ranch

Apart from warning message are you not getting desired output after execution?
 
Mohammed Sardar.
Ranch Hand
Posts: 193
1
Java jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote: That's not an error, it's a warning. There's a difference between these two: in case of an error, the compiler stops and does not produce a *.class file; in case of a warning, the compiler continues and you do get a *.class file.

You get this error because  you are using the raw type ArrayList.



Not error it seems, just warning.
 
Liyana Ghafar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Jesper de Jong Thank you for your explanations! Now I'm clear about it!

Mohammed Sardar. wrote:Welcome To Java Ranch

Apart from warning message are you not getting desired output after execution?

@Mohammed Sardar ThankYou.Im still learning 
I didn't run the .class file because I thought it is an error so the .class is not updated. After read Jesper reply I tried run the .class file and walla! there is the output 
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!