Arnaud Aliès
Arnaud Aliès

Reputation: 1092

Java compile warning unsafe condition

I recently started programming in java I made this code: https://github.com/mouuff/JavaMD5cracker

The code work but I get this creppy warning:

C:\Users\mou\Desktop\JavaMD5cracker-master>javac icrackmd5.java
Note: .\brute.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

sounds like the compiler find this line (brute.java : l 26) unsafe or I don't know...

if (tries > (int)pows.get(lenght-1))

can someone help me with that?

Upvotes: 0

Views: 183

Answers (3)

Bruno Vieira
Bruno Vieira

Reputation: 3904

It's just that you are making a cast operation for int in another object. It's an unchecked operation since the compiler can't check if what you are returning by pows.get(length-1) can really be converted to an int.

If you trust (I mean really trust) your code and knows that a cast for int can always be done you can let it that way, it's just a compiler warning.

Meanwhile, give a look at the Math object. Maybe there's an easy and safer way to do what you are trying to accomplish.

EDIT

Changing this:

Hashtable pows = new Hashtable();

To:

Hashtable<Integer,Integer> pows = new Hashtable<Integer,Integer>();

Will get rid of your compiler notes.

Upvotes: 1

Jesper
Jesper

Reputation: 206846

The class brute contains this:

Hashtable pows = new Hashtable();

It's using the raw type Hashtable. Generics should have been used here. Also, it should have used HashMap instead of the legacy collection class Hashtable.

Map<Integer, Integer> = new HashMap<Integer, Integer>();

The cast would then be unnecessary:

if (tries > pows.get(lenght-1)){

(Note that 'lenght' is spelled wrongly, it should have been 'length').

Besides that, the member variables should have been private, and the code does not conform to the de-facto coding standards that most of the world uses (class names should start with an upper-case character, variable names should not contain underscores).

Upvotes: 1

PermGenError
PermGenError

Reputation: 46428

its because of your hashtable declaration in brute.java

Hashtable pows = new Hashtable();

when the compiler executes this line

if (tries > (int)pows.get(lenght-1))

it doesnt know what the type of the element from the pows is.

change your hashtable declaration using generics

Hashtable<Integer, Integer> pows = new Hashtable<Integer,Integer>();

Upvotes: 2

Related Questions