469,331 Members | 5,955 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,331 developers. It's quick & easy.

"unchecked or unsafe operations." - when this type of exception thrown?

77
Expand|Select|Wrap|Line Numbers
  1. package test;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. public class MyArrayList{
  6.     public static void main(String args[])
  7.     {
  8.         ArrayList al = new ArrayList();
  9.         al.add("Jerald");
  10.         al.add("PJerald");
  11.         al.add(1);
  12.     }
  13. }
  14.  
I compiled my code as follows. It scold some thing that i dont know..

[$ ~]$ javac MyArrayList.java -d .; java test.MyArrayList
Note: MyArrayList.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Nov 20 '07 #1
8 4151
r035198x
13,262 8TB
Expand|Select|Wrap|Line Numbers
  1. package test;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. public class MyArrayList{
  6.     public static void main(String args[])
  7.     {
  8.         ArrayList al = new ArrayList();
  9.         al.add("Jerald");
  10.         al.add("PJerald");
  11.         al.add(1);
  12.     }
  13. }
  14.  
I compiled my code as follows. It scold some thing that i dont know..

[$ ~]$ javac MyArrayList.java -d .; java test.MyArrayList
Note: MyArrayList.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Did you recompile with -Xlint unchecked for the details?
Nov 20 '07 #2
JosAH
11,448 Expert 8TB
@OP: when Sun decided to introduce 'generics' they also decided that some
objects should be part of it: e.g. their Collection framework. This simply implies
that you don't just have an Arraylist anymore, you have an ArrayList that contains
a certain type, even if the type is just an Object. If you don't specify a type for
the objects to be stored in the ArrayList, all the compiler can do is whine at you.

Just because of backwards compatibility it has to accept that you want to store
just an object (or a derivative thereof) in your ArrayList, but it whines at you.

That's what you saw and you have two options:

1) ignore it and go on programming 'old style' which is semantically valid;
2) agnowledge the fact that you're just storing T type objects in an ArrayList.

For 2) you have to specify the type T using that funny <...> notation and you
have to fill in the dots as in:

Expand|Select|Wrap|Line Numbers
  1. ArrayList<String> myList= new ArrayList<String>();
  2.  
It's all about static type checking where the compiler is boss ...

kind regards,

Jos
Nov 20 '07 #3
pjerald
77
@OP: when Sun decided to introduce 'generics' they also decided that some
objects should be part of it: e.g. their Collection framework. This simply implies
that you don't just have an Arraylist anymore, you have an ArrayList that contains
a certain type, even if the type is just an Object. If you don't specify a type for
the objects to be stored in the ArrayList, all the compiler can do is whine at you.

Just because of backwards compatibility it has to accept that you want to store
just an object (or a derivative thereof) in your ArrayList, but it whines at you.

That's what you saw and you have two options:

1) ignore it and go on programming 'old style' which is semantically valid;
2) agnowledge the fact that you're just storing T type objects in an ArrayList.

For 2) you have to specify the type T using that funny <...> notation and you
have to fill in the dots as in:

Expand|Select|Wrap|Line Numbers
  1. ArrayList<String> myList= new ArrayList<String>();
  2.  
It's all about static type checking where the compiler is boss ...

kind regards,

Jos
Oh I feel sorry jos and r035198x, since i signed out and left yesterday, but you people took pain and answered. Dear r035198x, then i compiled my code with -Xlint. It showed me three warnings something like this,

[$ ~]$ javac -Xlint MyArrayList.java
MyArrayList.java:9: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.ArrayList
al.add("Jerald");
^
MyArrayList.java:10: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.ArrayList
al.add("PJerald");
^
MyArrayList.java:11: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.ArrayList
al.add(1);
^
3 warnings

Here with this
1) what is actually unchecked ?
2) should i specify explicitly the type of object which i going to add in the ArrayList( Am i wright jos?) ?

Thanks,
P.Jerald
Nov 21 '07 #4
r035198x
13,262 8TB
Oh I feel sorry jos and r035198x, since i signed out and left yesterday, but you people took pain and answered. Dear r035198x, then i compiled my code with -Xlint. It showed me three warnings something like this,

[$ ~]$ javac -Xlint MyArrayList.java
MyArrayList.java:9: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.ArrayList
al.add("Jerald");
^
MyArrayList.java:10: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.ArrayList
al.add("PJerald");
^
MyArrayList.java:11: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.ArrayList
al.add(1);
^
3 warnings

Here with this
1) what is actually unchecked ?
2) should i specify explicitly the type of object which i going to add in the ArrayList( Am i wright jos?) ?

Thanks,
P.Jerald
Now read Jos' response above.
Nov 21 '07 #5
pjerald
77
Now read Jos' response above.
i think my question number two is the answer!. ie., before we use ArrayList, We must specify what type of object we are going to store. am i right ?
Nov 21 '07 #6
r035198x
13,262 8TB
i think my question number two is the answer!. ie., before we use ArrayList, We must specify what type of object we are going to store. am i right ?
Yep .
Nov 21 '07 #7
pjerald
77
Yep .
k
thanks, r035198x.
regards,
P.Jerald
Nov 21 '07 #8
JosAH
11,448 Expert 8TB
Note that you have to rethink the statement at line 11 of your op: a1.add(1).
An int is definitely not a String and it won't be 'autoboxed' to a String either.

kind regards,

Jos
Nov 21 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Markus Elfring | last post: by
2 posts views Thread by Eric | last post: by
reply views Thread by Steven Buroff | last post: by
3 posts views Thread by Stephen Gennard | last post: by
3 posts views Thread by tlemcenvisit | last post: by
1 post views Thread by bill salkin | last post: by
2 posts views Thread by shengmin.ruan | last post: by
reply views Thread by mailforpr | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.