Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Got warnings from javac -Xlint:unchecked

Tags:

java

generics

I got 2 warnings after trying to use the -Xlint switch. How can I solve these warnings?

test:quadrantRDBTemplate mymac$ javac -Xlint:unchecked -cp mysql-connector-java-5.0.jar:iucbrf.jar *.java 
QuadrantSystemRDB.java:271: warning: [unchecked] unchecked call to put(K,V) as a member of the raw type java.util.HashMap
    argMap.put(xKey, new UniformDistribution(-1, 1));
              ^
QuadrantSystemRDB.java:272: warning: [unchecked] unchecked call to put(K,V) as a member of the raw type java.util.HashMap
    argMap.put(yKey, new UniformDistribution(-1, 1));
              ^
2 warnings
like image 770
karikari Avatar asked Feb 25 '11 07:02

karikari


People also ask

How do you get rid of warnings in Java?

Use of @SuppressWarnings is to suppress or ignore warnings coming from the compiler, i.e., the compiler will ignore warnings if any for that piece of code. 1. @SuppressWarnings("unchecked") public class Calculator { } - Here, it will ignore all unchecked warnings coming from that class.

How do I compile without warnings?

You can make all warnings being treated as such using -Wno-error. You can make specific warnings being treated as such by using -Wno-error=<warning name> where <warning name> is the name of the warning you don't want treated as an error. If you want to entirely disable all warnings, use -w (not recommended).

What is a compile warning?

Compiler warnings are messages produced by a compiler regarding program code fragments to be considered by the developer, as they may contain errors. Unlike compilation errors, warnings don't interrupt the compilation process.

How do I turn off error warnings?

Description. We currently have only two ways to disable compiler warnings-as-errors: either make none of them errors using --disable-warnings-as-errors or disable all errors with a compiler flag. There is no way to disable specific warnings-as-errors from the SCons command line due to the order of the compiler flags.


1 Answers

A raw type is a type that could have a type argument, but hasn't got on. For example Map (and HashMap) can have type parameters K and V for the types of keys and values, respectively.

So you could specify a Map<Integer,String> to indicate that the map contains Integer objects as keys and maps them to String objects.

If you use simply Map then you don't provide that information. Raw types exist mostly for backwards compatibility and there is no reason to use them in new code.

Solution: provide the appropriate types to your Map. According to your code, the V would be UniformDistribution and K would be the type of yKey and xKey.

like image 199
Joachim Sauer Avatar answered Sep 17 '22 20:09

Joachim Sauer