f



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

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? 


0
nospam21 (19047)
10/3/2006 4:01:46 PM
comp.lang.java.help 13339 articles. 0 followers. Post Follow

8 Replies
1814 Views

Similar Articles

[PageSpeed] 55

"sunbin" <noSpam@noSpam.com> wrote in message 
news:eftt69$ivu$1@reader01.singnet.com.sg...
> 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?

    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 


0
owong (6178)
10/4/2006 1:56:33 PM
"Oliver Wong" <owong@castortech.com> wrote in message 
news:l4PUg.12092$N4.4987@clgrps12...

>    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? 


0
nospam21 (19047)
10/4/2006 5:49:35 PM
"Tor Iver Wilhelmsen" <jadedgamer@hotmail.com> wrote in message 
news:uk63gx71q.fsf@hotmail.com...

> 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. 


0
nospam21 (19047)
10/4/2006 6:04:22 PM
"Oliver Wong" <owong@castortech.com> writes:

>     Specifically, change the cast to:
> 
> CustDB = (ArrayList<?>)read.readObject();

Or add a @SuppressWarnings("unchecked") annotation to the method.
0
jadedgamer (368)
10/4/2006 6:36:01 PM
"sunbin" <noSpam@noSpam.com> wrote in message 
news:eg0nsj$lgt$1@reader01.singnet.com.sg...
>
> "Oliver Wong" <owong@castortech.com> wrote in message 
> news:l4PUg.12092$N4.4987@clgrps12...
>
>>    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.

    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 


1
owong (6178)
10/4/2006 6:58:40 PM
"sunbin" <noSpam@noSpam.com> writes:

> 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
0
jadedgamer (368)
10/4/2006 7:20:13 PM
sunbin wrote:
> 
> 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
0
usenet120 (1728)
10/4/2006 9:17:09 PM
"Tor Iver Wilhelmsen" <jadedgamer@hotmail.com> wrote in message 
news:usli3x502.fsf@hotmail.com...
> "sunbin" <noSpam@noSpam.com> writes:
>
>> 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

Hey thanks. I got manage to suppress the warning message. 


0
nospam21 (19047)
10/5/2006 1:07:37 PM
Reply: