Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / October 2006

Tip: Looking for answers? Try searching our database.

Beginner's Question: "*.java uses unchecked or unsafe operations"

Thread view: 
sunbin - 03 Oct 2006 17:01 GMT
Hi all

I have read the net and notice this problem is caused by the version of
compiler between 1.5 and 1.4.

I tried to use the follow codes to overcome the warning msg. "uses unchecked
or unsafe operations" but not able to. Please advice. Thanks.

[code]

public class XXX
{
   private ArrayList <Customer> CustDB;
   .....
   .....
   public static void main(String[] args)
   {
       ......
       ......
       ......
       FileInputStream readFile = new FileInputStream("cust.dat");
      ObjectInputStream read = new ObjectInputStream(readFile);

       CustDB = (ArrayList<Customer>)read.readObject();
   }
}

[/code]

I tried to typecast the read object, but yet the warning still exists.
May I know where have I did wrong?
Oliver Wong - 04 Oct 2006 14:56 GMT
> Hi all
>
[quoted text clipped - 27 lines]
> I tried to typecast the read object, but yet the warning still exists.
> May I know where have I did wrong?

   Specifically, change the cast to:

CustDB = (ArrayList<?>)read.readObject();

   you'll probably have to add some more casts later on in your code in
addition to making this change.

   In general, don't supply generic type arguments in cast expressions.

   - Oliver
sunbin - 04 Oct 2006 18:49 GMT
>    Specifically, change the cast to:
>
> CustDB = (ArrayList<?>)read.readObject();
>
>    you'll probably have to add some more casts later on in your code in
> addition to making this change.

Thanks for the reply.

I dont quite get what you mean. I am precisely doing what your post listed

CustDB = (ArrayList<?>)read.readObject();

but the warning msg just won't get away.

>    In general, don't supply generic type arguments in cast expressions.

Sorry I do not know what's generic, mind to explain?
Oliver Wong - 04 Oct 2006 19:58 GMT
>>    Specifically, change the cast to:
>>
[quoted text clipped - 10 lines]
>
> but the warning msg just won't get away.

   Are you sure? In your original post, you said you wrote:

CustDB = (ArrayList<Customer>)read.readObject();

   But I'm telling you to write:

CustDB = (ArrayList<?>)read.readObject();

   Notice that I don't pass "Customer" as a generic type argument.

>>    In general, don't supply generic type arguments in cast expressions.
>
> Sorry I do not know what's generic, mind to explain?

   To put it informally, generic type arguments are the stuff that appear
between the angle brackets in your code. If you don't understand how they
work, it's probably best not to use them at all (remove everything between
the angle brackets, and the angle brackets themselves), so the code would
look like:

CustDB = (ArrayList)read.readObject();

   instead of

CustDB = (ArrayList<Customer>)read.readObject();

   You'll get warnings about not using generics, but you should just ignore
them, since you don't know how to use generics yet.

   - Oliver
Tor Iver Wilhelmsen - 04 Oct 2006 18:54 GMT
>     Specifically, change the cast to:
>
> CustDB = (ArrayList<?>)read.readObject();

Or add a @SuppressWarnings("unchecked") annotation to the method.
sunbin - 04 Oct 2006 19:04 GMT
> Or add a @SuppressWarnings("unchecked") annotation to the method.

Well how do I add that to my codes... sorry i cannot understand the
@suppress thingy.
Tor Iver Wilhelmsen - 04 Oct 2006 19:40 GMT
> Well how do I add that to my codes... sorry i cannot understand the
> @suppress thingy.

Annotations is another of the new JSE 5 features, like the generics
you already use.

http://www.oracle.com/technology/pub/articles/hunter_meta.html
sunbin - 05 Oct 2006 14:07 GMT
>> Well how do I add that to my codes... sorry i cannot understand the
>> @suppress thingy.
[quoted text clipped - 3 lines]
>
> http://www.oracle.com/technology/pub/articles/hunter_meta.html

Hey thanks. I got manage to suppress the warning message.
Thomas Hawtin - 04 Oct 2006 22:17 GMT
> I tried to use the follow codes to overcome the warning msg. "uses unchecked
> or unsafe operations" but not able to. Please advice. Thanks.

>        ObjectInputStream read = new ObjectInputStream(readFile);
>
>         CustDB = (ArrayList<Customer>)read.readObject();

Here's an answer I prepared earlier:

http://groups.google.com/group/comp.lang.java.programmer/tree/browse_frm/thread/
3c05ddbbe4eaaba4#doc_585daaa682e4e563


Tom Hawtin


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.