473,395 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

"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 4326
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

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

Similar topics

3
by: Markus Elfring | last post by:
I want to allow to display the contents of the directory "/home/ftp/incoming/news". Is the setting "safe_mode_include_dir" (http://de.php.net/manual/en/features.safe-mode.php) the right one to...
2
by: Eric | last post by:
Hi, I'm getting this error message : createSession(java.lang.String, java.lang.String, org.omg.CORBA.IntHolder, B.SessionHolder,org.omg.CORBA.StringHolder) in B.BOperations cannot be applied...
0
by: Steven Buroff | last post by:
I can't seem to get the @SuppressWarnings("unchecked") to work. Here is my test program. public class Tryit { @SuppressWarnings({"unchecked"}) public <T> T doit(Class<T> clazz){ T val =...
3
by: Stephen Gennard | last post by:
Hello, I having a problem dynamically invoking a static method that takes a reference to a SByte*. If I do it directly it works just fine. Anyone any ideas why? I have include a example...
3
by: tlemcenvisit | last post by:
Hello I'm looking for the VC++.NET equivalent of "unsafe" instruction. unsafe is a VC#.NET instruction Thanks
1
by: bill salkin | last post by:
How can I implement set operations in VB.NET. For example, {1,2,3,4} union {3,4,5,6} = {1,2,3,4,5,6} {1,2,3,4} minus {0,3} = {1,2,4} {1,2,3,4,5,6,7} intersect {0,2,7} = {2,7} Bill
2
by: Anders Borum | last post by:
Hello! I was looking at marking objects with a changed state, once properties have been changed. The reason behind this is, that I would like to enlist such objects for processing in a...
2
by: shengmin.ruan | last post by:
i need to use the native code(unmanaged code),and i have to use void*,so i use the "unsafe" symbol then i meet the trouble c++code: SC_InitAsk sca; memset(&sca, 0, sizeof(sca));...
0
by: mailforpr | last post by:
Hi. Let me introduce an iterator to you, the so-called "Abstract Iterator" I developed the other day. I actually have no idea if there's another "Abstract Iterator" out there, as I have never...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.