简体   繁体   中英

Explanation of “Unchecked cast” warning in Java

im only a beginner and I'd like a good soul to help me ;)I got this method and on the line:

( (HashSet<String>) pos[targetPos]).add(word);

it gives me an exception

    (Unchecked cast from Object to HashSet<String>)

I tried to change Object[pos] to String[pos] to be more specific but it then gives me an error on this line: pos[targetPos] = new HashSet<String>();

Type mismatch: cannot convert from HashSet<String> to String

this is the method:

public void add(String word, Object[] root){

    Object[] pos = root;
    int wordIndex = 0;
    int targetPos;

    if(word.length()>=3){
        for(int i = 1; i <=3; i++){
            targetPos = word.charAt(wordIndex) -'a'; //convert a letter into index eg a==0
            if(i==3){
                if(pos[targetPos]==null){
                    pos[targetPos] = new HashSet<String>();
                }
                ( (HashSet<String>) pos[targetPos]).add(word);
                //System.out.println(Arrays.toString(pos));
                break;

            }//end if outer if
            else{
                if(pos[targetPos]==null){
                    pos[targetPos] = new Object[28];
                }
                wordIndex++;
                pos =  (Object[]) pos[targetPos];
            }
        }//end of for
    }

}

The root is

 Object[] root = new Object[28];

The "unchecked cast" message is a warning. The compiler is warning you that it cannot be sure that the explicit cast from Object to HashSet<String> can happen safely during runtime, which means that it is possible that if your array of type Object contains something other than HashSet<String> , you will get a ClassCastException during runtime when the JVM attempts to cast that object into type HashSet<String> . Essentially the compiler is warning you beforehand that you are doing something that is potentially unsafe and it can be a source of problems later.

It is not a good practice to simply use a raw object of arrays. If you are going to be sure that the array is only going to contain HashSet<String> objects, then you should probably type it as such (ie, Set<String>[] ; use the interface instead of the concrete implementation for your type because then you can switch implementations when you want). The only time you should do an explicit cast is when you can be absolutely sure that the object that you are casting is most definitely of the type that you are casting it to. For example, if you have an object that implements an interface, and also assume that you are in some class that is most definitely working with a specific concrete-implementation of that interface. In this case, it is acceptable to cast it from that interface into the concrete type.

Your method should look like this:

public void add(String word, Set<String>[] root){

    Set<String>[] pos = root; //You probably don't even need this since you have "root"
    ...
}

Also, consider using a List of Set<String> instead of an array:

public void add(String word, List<Set<String>> root){

    List<Set<String>> pos = root; //You probably don't even need this since you have "root"
    ...
}

pos[] is defined as an Object array. When you cast it to HashSet<String> later, Java doesn't know that you can do that. That's what an unchecked cast is -- the compiler is warning you that you MIGHT be doing something unsafe.

You can make the warning go away by changing the type of pos to HashSet<String>[] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM