473,714 Members | 2,615 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

77 New Member
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.jav a -d .; java test.MyArrayLis t
Note: MyArrayList.jav a uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Nov 20 '07 #1
8 4347
r035198x
13,262 MVP
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.jav a -d .; java test.MyArrayLis t
Note: MyArrayList.jav a 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 Recognized Expert MVP
@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 New Member
@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.jav a
MyArrayList.jav a:9: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.Array List
al.add("Jerald" );
^
MyArrayList.jav a:10: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.Array List
al.add("PJerald ");
^
MyArrayList.jav a:11: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.Array List
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 MVP
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.jav a
MyArrayList.jav a:9: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.Array List
al.add("Jerald" );
^
MyArrayList.jav a:10: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.Array List
al.add("PJerald ");
^
MyArrayList.jav a:11: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.Array List
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 New Member
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 MVP
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 New Member
Yep .
k
thanks, r035198x.
regards,
P.Jerald
Nov 21 '07 #8
JosAH
11,448 Recognized Expert MVP
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
1718
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 support additional access rights for a few selected directories?
2
6949
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 to (java.lang.String,java.lang.String, int, B.Session, java.lang.String) It looks like I am passing parameters that are of the wrong type. But
0
13667
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 = (T)"xxx"; return val; } }
3
1312
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 below... --
3
1589
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
4549
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
1732
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 transaction. The example below illustrates a sample pattern, which is fine when exposing simple value types. However, when exposing reference types, it becomes really hard (if not
2
1717
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)); sca.m_Head.m_nType = SC_INIT; sca.m_dwSoftware = 0x12345678; sca.m_hWnd = m_hWnd;
0
2677
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 looked for one on the net (I did browse the boost library though). It doesn't matter right now, anyway. To put it simply, Abstract Iterator is mainly a wrapper class. It helps
0
8706
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9311
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9171
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7947
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6632
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5946
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3156
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2518
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2108
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.