unchecked or unsafe operations with JDK 1.5.0

A

Axl

Just thought I'd add this note since I usually search this newsgroup
for answers and didn't find this mentioned. If anyone has any further
comments on this aspect of 1.5.0 please reply.

The "unchecked or unsafe operations" warning can result from a missing
type declaration. Warning looks like so:
Note: Parse.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

Recompiling with -Xlint:unchecked can be confusing. For example using
an ArrayList may result in the following:
Parse.java:21: warning: [unchecked] unchecked call to add(E) as a
member of the raw type java.util.ArrayList
al.add(str);
Parse.java:25: warning: [unchecked] unchecked call to
<T>toArray(T[]) as a member of the raw type java.util.ArrayList
al.toArray(ary);

These lines (21 and 25 in the above example) do not need to change to
eliminate the compiler warnings. Instead change the declaration of the
ArrayList class (substitute <String> with whatever type you wish to
use):
ArrayList<String> al = new ArrayList<String>();


C'est tout,

Axl

Axl Weslowski
aweslowski_@_rpa.com
(remove_the_underscores)
 
M

Mike

I'm getting two errrors with the simple code below:

For line 5 I get " <identifier> expected "
For line 6 I get " ']' expected" and also " <identifier> expected "

What gives?

Thanks

------------------------------------------

1 package com.dlm.testing;

2 import java.util.Arrays;

3 public class Test1 {

4 public String[] names;
5 names = new String[50];

6 names[0]="Jones";

7 }
 
J

John C. Bollinger

Mike said:
I'm getting two errrors with the simple code below:

For line 5 I get " <identifier> expected "
For line 6 I get " ']' expected" and also " <identifier> expected "

What gives?

Thanks

------------------------------------------

1 package com.dlm.testing;

2 import java.util.Arrays;

3 public class Test1 {

4 public String[] names;
5 names = new String[50];

6 names[0]="Jones";

7 }

Lines 5 and 6 are not part of any method (nor of an instance
initializer, but you don't need to worry about those just yet). Try this:

package com.dlm.testing;

import java.util.Arrays;

public class Test1 {

public String[] names = new String[50];

public Test1() {
names[0]="Jones";
}

}

Note the two different ways to initialize an instance variable (or its
members): in the variable's declaration, or in a constructor. You
cannot place bare code in the body of a class definition. (Under what
circumstances would it ever run?)


John Bollinger
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top