Java compilation time appears to use unchecked or unsafe operation solutions

  • 2020-04-01 03:04:22
  • OfStack

Problems I encountered while writing Java files in editplus.

< img border = 0 id = theimg onclick = window. The open this. (SRC) SRC = "/ / files.jb51.net/file_images/article/201403/20140310164805.jpg? 2014210164937 ">


import java.util.*;
class collection{
    public static void main(String[] args) {
        Collection c1=new ArrayList(25);

        c1.add(new String("one"));
        c1.add(new String("two"));
        String s="three";
        c1.add(s);
        for (Iterator i=c1.iterator();i.hasNext();)
        {

            System.out.println(i.next());
        }
    }
}

Then find the following reason, which is transferred from someone else.


This can occur when compiling Java source files using jdk1.5 or above. (use of unchecked or unsafe operations; Use -xlint :unchecked to recompile.
The reason is that the creation of collection classes in jdk1.5 is somewhat different from that in jdk1.4, mainly due to the addition of generics in jdk1.5, which means that the data in the collection can be checked. Prior to JDK 1.5, if no parameter type was specified, the JDK 1.5 compiler would report an unchecked warning because it could not check whether the given parameters were acceptable, which did not affect the run. When prompted, the compiler can override this warning by specifying a parameter. Or specify a type parameter for it.


List temp = new ArrayList ();
temp.add("1");
temp.add("2");

Modified to


List <String> temp = new ArrayList <String> ();
temp.add("1");
temp.add("2");


Then change the code to


import java.util.*;
class collection{
    public static void main(String[] args) {
        Collection<String> c1=new ArrayList<String>(25);

        c1.add(new String("one"));
        c1.add(new String("two"));
        String s="three";
        c1.add(s);
        for (Iterator i=c1.iterator();i.hasNext();)
        {

            System.out.println(i.next());
        }
    }
}


Related articles: