Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MyType<E> {
- class Inner { }
- static class Nested { }
- public static void main(String[] args) {
- MyType mt; // warning: MyType is a raw type
- MyType.Inner inn; // warning: MyType.Inner is a raw type
- MyType.Nested nest; // no warning: not parameterized type
- MyType<Object> mt1; // no warning: type parameter given
- MyType<?> mt2; // no warning: type parameter given (wildcard OK!)
- }
- }
- List names = new ArrayList(); // warning: raw type!
- names.add("John");
- names.add("Mary");
- names.add(Boolean.FALSE); // not a compilation error!
- for (Object o : names) {
- String name = (String) o;
- System.out.println(name);
- } // throws ClassCastException!
- // java.lang.Boolean cannot be cast to java.lang.String
- List<String> names = new ArrayList<String>();
- names.add("John");
- names.add("Mary");
- names.add(Boolean.FALSE); // compilation error!
- void appendNewObject(List<Object> list) {
- list.add(new Object());
- }
- List<String> names = new ArrayList<String>();
- appendNewObject(names); // compilation error!
- static void appendNewObject(List<?> list) {
- list.add(new Object()); // compilation error!
- }
- //...
- List<String> names = new ArrayList<String>();
- appendNewObject(names); // this part is fine!
- class MyType<E> {
- List<String> getNames() {
- return Arrays.asList("John", "Mary");
- }
- public static void main(String[] args) {
- MyType rawType = new MyType();
- // unchecked warning!
- // required: List<String> found: List
- List<String> names = rawType.getNames();
- // compilation error!
- // incompatible types: Object cannot be converted to String
- for (String str : rawType.getNames())
- System.out.print(str);
- }
- }
- List aList = new ArrayList();
- String s = "Hello World!";
- aList.add(s);
- String c = (String)aList.get(0);
- List aNumberList = new ArrayList();
- String one = "1";//Number one
- aNumberList.add(one);
- Integer iOne = (Integer)aNumberList.get(0);//Insert ClassCastException here
- List<String> aNumberList = new ArrayList<String>();
- aNumberList.add("one");
- Integer iOne = aNumberList.get(0);//Compile time error
- String sOne = aNumberList.get(0);//works fine
- // Old style collections now known as raw types
- List aList = new ArrayList(); //Could contain anything
- // New style collections with Generics
- List<String> aList = new ArrayList<String>(); //Contains only Strings
- //raw, not type save can compare with Other classes
- class MyCompareAble implements CompareAble
- {
- int id;
- public int compareTo(Object other)
- {return this.id - ((MyCompareAble)other).id;}
- }
- //Generic
- class MyCompareAble implements CompareAble<MyCompareAble>
- {
- int id;
- public int compareTo(MyCompareAble other)
- {return this.id - other.id;}
- }
- List<String> someStrings = new ArrayList<String>();
- someStrings.add("one");
- String one = someStrings.get(0);
- List someStrings = new ArrayList();
- someStrings.add("one");
- String one = (String)someStrings.get(0);
- public class Box<T> {
- public void set(T t) { /* ... */ }
- // ...
- }
- Box<Integer> intBox = new Box<>();
- Box rawBox = new Box();
- Box<String> stringBox = new Box<>();
- Box rawBox = stringBox; // OK
- Box rawBox = new Box(); // rawBox is a raw type of Box<T>
- Box<Integer> intBox = rawBox; // warning: unchecked conversion
- Box<String> stringBox = new Box<>();
- Box rawBox = stringBox;
- rawBox.set(8); // warning: unchecked invocation to set(T)
- public class WarningDemo {
- public static void main(String[] args){
- Box<Integer> bi;
- bi = createBox();
- }
- static Box createBox(){
- return new Box();
- }
- }
- WarningDemo.java:4: warning: [unchecked] unchecked conversion
- found : Box
- required: Box<java.lang.Integer>
- bi = createBox();
- ^
- 1 warning
- private static List<String> list = new ArrayList<String>();
- LinkedList list = new LinkedList();
- list.add(new MyObject());
- MyObject myObject = (MyObject)list.get(0);
- LinkedList<MyObject> list = new LinkedList<MyObject>();
- list.add(new MyObject());
- MyObject myObject = list.get(0);
- private static List<String> list = new ArrayList<String>();
- List<String> names = new ArrayList<String>();
- names.add("John"); // OK
- names.add(new Integer(1)); // compile error
- 1. ArrayList<String> arr = new ArrayList<String>();
- 2. ArrayList<String> arr = new ArrayList();
- 3. ArrayList arr = new ArrayList<String>();
- arr.add("hello");// alone statement will compile successfully and no warning.
- arr.add(23); //prone to compile time error.
- //error: no suitable method found for add(int)
- arr.add("hello"); //alone this compile but raise the warning.
- arr.add(23); //again prone to compile time error.
- //error: no suitable method found for add(int)
- arr.add("hello");
- arr.add(23); //compiles fine but raise the warning.
- Set set = new HashSet();
- set.add(3.45); //ok
- Set<Integer> set = new HashSet<Integer>();
- set.add(3.45); //NOT ok.
- public class StrangeClass<T> {
- @SuppressWarnings("unchecked")
- public <X> X getSomethingElse() {
- return (X)"Testing something else!";
- }
- public static void main(String[] args) {
- final StrangeClass<String> withGeneric = new StrangeClass<>();
- final StrangeClass withoutGeneric = new StrangeClass();
- final String value1,
- value2;
- // Compiles
- value1 = withGeneric.getSomethingElse();
- // Produces compile error:
- // incompatible types: java.lang.Object cannot be converted to java.lang.String
- value2 = withoutGeneric.getSomethingElse();
- }
- }
- private static List<String> list = new ArrayList<String>();
- public class Box<T> {
- public void set(T t) { /* ... */ }
- // ...
- }
- Box<Integer> intBox = new Box<>();
- Box rawBox = new Box();
- public static void main(String[] args) throws IOException {
- Map wordMap = new HashMap();
- if (args.length > 0) {
- for (int i = 0; i < args.length; i++) {
- countWord(wordMap, args[i]);
- }
- } else {
- getWordFrequency(System.in, wordMap);
- }
- for (Iterator i = wordMap.entrySet().iterator(); i.hasNext();) {
- Map.Entry entry = (Map.Entry) i.next();
- System.out.println(entry.getKey() + " :t" + entry.getValue());
- }
- public static void main(String[] args) throws IOException {
- // replace with TreeMap to get them sorted by name
- Map<String, Integer> wordMap = new HashMap<String, Integer>();
- if (args.length > 0) {
- for (int i = 0; i < args.length; i++) {
- countWord(wordMap, args[i]);
- }
- } else {
- getWordFrequency(System.in, wordMap);
- }
- for (Iterator<Entry<String, Integer>> i = wordMap.entrySet().iterator(); i.hasNext();) {
- Entry<String, Integer> entry = i.next();
- System.out.println(entry.getKey() + " :t" + entry.getValue());
- }
- }
- import java.util.*;
- public final class AvoidRawTypes {
- void withRawType() {
- //Raw List doesn't self-document,
- //doesn't state explicitly what it can contain
- List stars = Arrays.asList("Arcturus", "Vega", "Altair");
- Iterator iter = stars.iterator();
- while (iter.hasNext()) {
- String star = (String) iter.next(); //cast needed
- log(star);
- }
- }
- void withParameterizedType() {
- List < String > stars = Arrays.asList("Spica", "Regulus", "Antares");
- for (String star: stars) {
- log(star);
- }
- }
- private void log(Object message) {
- System.out.println(Objects.toString(message));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement