Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.72 KB | None | 0 0
  1. public class MyType<E> {
  2. class Inner { }
  3. static class Nested { }
  4.  
  5. public static void main(String[] args) {
  6. MyType mt; // warning: MyType is a raw type
  7. MyType.Inner inn; // warning: MyType.Inner is a raw type
  8.  
  9. MyType.Nested nest; // no warning: not parameterized type
  10. MyType<Object> mt1; // no warning: type parameter given
  11. MyType<?> mt2; // no warning: type parameter given (wildcard OK!)
  12. }
  13. }
  14.  
  15. List names = new ArrayList(); // warning: raw type!
  16. names.add("John");
  17. names.add("Mary");
  18. names.add(Boolean.FALSE); // not a compilation error!
  19.  
  20. for (Object o : names) {
  21. String name = (String) o;
  22. System.out.println(name);
  23. } // throws ClassCastException!
  24. // java.lang.Boolean cannot be cast to java.lang.String
  25.  
  26. List<String> names = new ArrayList<String>();
  27. names.add("John");
  28. names.add("Mary");
  29. names.add(Boolean.FALSE); // compilation error!
  30.  
  31. void appendNewObject(List<Object> list) {
  32. list.add(new Object());
  33. }
  34.  
  35. List<String> names = new ArrayList<String>();
  36. appendNewObject(names); // compilation error!
  37.  
  38. static void appendNewObject(List<?> list) {
  39. list.add(new Object()); // compilation error!
  40. }
  41. //...
  42.  
  43. List<String> names = new ArrayList<String>();
  44. appendNewObject(names); // this part is fine!
  45.  
  46. class MyType<E> {
  47. List<String> getNames() {
  48. return Arrays.asList("John", "Mary");
  49. }
  50.  
  51. public static void main(String[] args) {
  52. MyType rawType = new MyType();
  53. // unchecked warning!
  54. // required: List<String> found: List
  55. List<String> names = rawType.getNames();
  56. // compilation error!
  57. // incompatible types: Object cannot be converted to String
  58. for (String str : rawType.getNames())
  59. System.out.print(str);
  60. }
  61. }
  62.  
  63. List aList = new ArrayList();
  64. String s = "Hello World!";
  65. aList.add(s);
  66. String c = (String)aList.get(0);
  67.  
  68. List aNumberList = new ArrayList();
  69. String one = "1";//Number one
  70. aNumberList.add(one);
  71. Integer iOne = (Integer)aNumberList.get(0);//Insert ClassCastException here
  72.  
  73. List<String> aNumberList = new ArrayList<String>();
  74. aNumberList.add("one");
  75. Integer iOne = aNumberList.get(0);//Compile time error
  76. String sOne = aNumberList.get(0);//works fine
  77.  
  78. // Old style collections now known as raw types
  79. List aList = new ArrayList(); //Could contain anything
  80. // New style collections with Generics
  81. List<String> aList = new ArrayList<String>(); //Contains only Strings
  82.  
  83. //raw, not type save can compare with Other classes
  84. class MyCompareAble implements CompareAble
  85. {
  86. int id;
  87. public int compareTo(Object other)
  88. {return this.id - ((MyCompareAble)other).id;}
  89. }
  90. //Generic
  91. class MyCompareAble implements CompareAble<MyCompareAble>
  92. {
  93. int id;
  94. public int compareTo(MyCompareAble other)
  95. {return this.id - other.id;}
  96. }
  97.  
  98. List<String> someStrings = new ArrayList<String>();
  99. someStrings.add("one");
  100. String one = someStrings.get(0);
  101.  
  102. List someStrings = new ArrayList();
  103. someStrings.add("one");
  104. String one = (String)someStrings.get(0);
  105.  
  106. public class Box<T> {
  107. public void set(T t) { /* ... */ }
  108. // ...
  109. }
  110.  
  111. Box<Integer> intBox = new Box<>();
  112.  
  113. Box rawBox = new Box();
  114.  
  115. Box<String> stringBox = new Box<>();
  116. Box rawBox = stringBox; // OK
  117.  
  118. Box rawBox = new Box(); // rawBox is a raw type of Box<T>
  119. Box<Integer> intBox = rawBox; // warning: unchecked conversion
  120.  
  121. Box<String> stringBox = new Box<>();
  122. Box rawBox = stringBox;
  123. rawBox.set(8); // warning: unchecked invocation to set(T)
  124.  
  125. public class WarningDemo {
  126. public static void main(String[] args){
  127. Box<Integer> bi;
  128. bi = createBox();
  129. }
  130.  
  131. static Box createBox(){
  132. return new Box();
  133. }
  134. }
  135.  
  136. WarningDemo.java:4: warning: [unchecked] unchecked conversion
  137. found : Box
  138. required: Box<java.lang.Integer>
  139. bi = createBox();
  140. ^
  141. 1 warning
  142.  
  143. private static List<String> list = new ArrayList<String>();
  144.  
  145. LinkedList list = new LinkedList();
  146. list.add(new MyObject());
  147. MyObject myObject = (MyObject)list.get(0);
  148.  
  149. LinkedList<MyObject> list = new LinkedList<MyObject>();
  150. list.add(new MyObject());
  151. MyObject myObject = list.get(0);
  152.  
  153. private static List<String> list = new ArrayList<String>();
  154.  
  155. List<String> names = new ArrayList<String>();
  156. names.add("John"); // OK
  157. names.add(new Integer(1)); // compile error
  158.  
  159. 1. ArrayList<String> arr = new ArrayList<String>();
  160. 2. ArrayList<String> arr = new ArrayList();
  161. 3. ArrayList arr = new ArrayList<String>();
  162.  
  163. arr.add("hello");// alone statement will compile successfully and no warning.
  164.  
  165. arr.add(23); //prone to compile time error.
  166. //error: no suitable method found for add(int)
  167.  
  168. arr.add("hello"); //alone this compile but raise the warning.
  169. arr.add(23); //again prone to compile time error.
  170. //error: no suitable method found for add(int)
  171.  
  172. arr.add("hello");
  173. arr.add(23); //compiles fine but raise the warning.
  174.  
  175. Set set = new HashSet();
  176. set.add(3.45); //ok
  177.  
  178. Set<Integer> set = new HashSet<Integer>();
  179. set.add(3.45); //NOT ok.
  180.  
  181. public class StrangeClass<T> {
  182. @SuppressWarnings("unchecked")
  183. public <X> X getSomethingElse() {
  184. return (X)"Testing something else!";
  185. }
  186.  
  187. public static void main(String[] args) {
  188. final StrangeClass<String> withGeneric = new StrangeClass<>();
  189. final StrangeClass withoutGeneric = new StrangeClass();
  190. final String value1,
  191. value2;
  192.  
  193. // Compiles
  194. value1 = withGeneric.getSomethingElse();
  195.  
  196. // Produces compile error:
  197. // incompatible types: java.lang.Object cannot be converted to java.lang.String
  198. value2 = withoutGeneric.getSomethingElse();
  199. }
  200. }
  201.  
  202. private static List<String> list = new ArrayList<String>();
  203.  
  204. public class Box<T> {
  205. public void set(T t) { /* ... */ }
  206. // ...
  207. }
  208.  
  209. Box<Integer> intBox = new Box<>();
  210.  
  211. Box rawBox = new Box();
  212.  
  213. public static void main(String[] args) throws IOException {
  214.  
  215. Map wordMap = new HashMap();
  216. if (args.length > 0) {
  217. for (int i = 0; i < args.length; i++) {
  218. countWord(wordMap, args[i]);
  219. }
  220. } else {
  221. getWordFrequency(System.in, wordMap);
  222. }
  223. for (Iterator i = wordMap.entrySet().iterator(); i.hasNext();) {
  224. Map.Entry entry = (Map.Entry) i.next();
  225. System.out.println(entry.getKey() + " :t" + entry.getValue());
  226. }
  227.  
  228. public static void main(String[] args) throws IOException {
  229. // replace with TreeMap to get them sorted by name
  230. Map<String, Integer> wordMap = new HashMap<String, Integer>();
  231. if (args.length > 0) {
  232. for (int i = 0; i < args.length; i++) {
  233. countWord(wordMap, args[i]);
  234. }
  235. } else {
  236. getWordFrequency(System.in, wordMap);
  237. }
  238. for (Iterator<Entry<String, Integer>> i = wordMap.entrySet().iterator(); i.hasNext();) {
  239. Entry<String, Integer> entry = i.next();
  240. System.out.println(entry.getKey() + " :t" + entry.getValue());
  241. }
  242.  
  243. }
  244.  
  245. import java.util.*;
  246.  
  247. public final class AvoidRawTypes {
  248.  
  249. void withRawType() {
  250.  
  251. //Raw List doesn't self-document,
  252. //doesn't state explicitly what it can contain
  253.  
  254. List stars = Arrays.asList("Arcturus", "Vega", "Altair");
  255.  
  256. Iterator iter = stars.iterator();
  257.  
  258. while (iter.hasNext()) {
  259.  
  260. String star = (String) iter.next(); //cast needed
  261.  
  262. log(star);
  263. }
  264.  
  265. }
  266.  
  267. void withParameterizedType() {
  268.  
  269. List < String > stars = Arrays.asList("Spica", "Regulus", "Antares");
  270.  
  271. for (String star: stars) {
  272.  
  273. log(star);
  274. }
  275.  
  276. }
  277.  
  278. private void log(Object message) {
  279.  
  280. System.out.println(Objects.toString(message));
  281.  
  282. }
  283.  
  284. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement