code_junkie

Java: Out with the Old, In with the New

Nov 12th, 2011
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. public static final int CLUBS = 0;
  2. public static final int DIAMONDS = 1;
  3. public static final int HEARTS = 2;
  4. public static final int SPADES = 3;
  5.  
  6. public enum Suit {
  7. CLUBS,
  8. DIAMONDS,
  9. HEARTS,
  10. SPADES
  11. }
  12.  
  13. List l = someList;
  14. Iterator i = l.getIterator();
  15. while (i.hasNext()) {
  16. MyObject o = (MyObject)i.next();
  17. }
  18.  
  19. List<MyObject> l = someList;
  20. for (MyObject o : l) {
  21. //do something
  22. }
  23.  
  24. try {
  25. BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  26. String str = reader.readLine();
  27. reader.close();
  28. }
  29. catch (IOException e) {
  30. System.err.println("error when closing input stream.");
  31. }
  32.  
  33. Scanner reader = new Scanner(System.in);
  34. String str = reader.nextLine();
  35. reader.close();
  36.  
  37. Console reader = System.console();
  38. String str = reader.readLine();
  39.  
  40. public int add(int... numbers){
  41. int sum = 0 ;
  42. for (int i : numbers){
  43. sum+=i;
  44. }
  45. return sum ;
  46. }
  47.  
  48. public int add(int n1, int n2, int n3, int n4) ;
  49.  
  50. public int add(List<Integer> numbers) ;
  51.  
  52. Integer myInteger = 6;
  53. int myInt = myInteger.intValue();
  54.  
  55. Integer myInteger = 6;
  56. int myInt = myInteger;
  57.  
  58. String str = "test " + intValue + " test " + doubleValue;
  59.  
  60. String str = String.format("test %d test %lg", intValue, doubleValue);
  61.  
  62. myframe.getContentPane().add(mycomponent);
  63.  
  64. myframe.add(mycomponent);
  65.  
  66. import static Math.* ;
  67.  
  68. Vector stringVector = new Vector();
  69. stringVector.add("hi");
  70. stringVector.add(528); // oops!
  71. stringVector.add(new Whatzit()); // Oh my, could spell trouble later on!
  72.  
  73. ArrayList<String> stringList = new ArrayList<String>();
  74. stringList.add("hello again");
  75. stringList.add(new Whatzit()); // Won't compile!
  76.  
  77. List list = getTheList();
  78. Iterator iter = list.iterator()
  79. while (iter.hasNext()) {
  80. String s = (String) iter.next();
  81. // .. do something
  82. }
  83.  
  84. List list = getTheList();
  85. for (Iterator iter = list.iterator(); iter.hasNext();) {
  86. String s = (String) iter.next();
  87. // .. do something
  88. }
  89.  
  90. List<String> list = getTheList();
  91. for (String s : list) {
  92. // .. do something
  93. }
  94.  
  95. String s = n + "";
  96.  
  97. String s = String.valueOf(n);
  98.  
  99. // AGGHHH!!!
  100. int[] array = new int[] {0, 1, 2, 3, 4};
  101. for (int i = 0; i < array.length; i++)
  102. {
  103. // Do something...
  104. }
  105.  
  106. // Nice and clean.
  107. int[] array = new int[] {0, 1, 2, 3, 4};
  108. for (int n : array)
  109. {
  110. // Do something...
  111. }
  112.  
  113. int[] src = new int[] {1, 2, 3, 4, 5};
  114. int[] dest = new int[src.length];
  115. System.arraycopy(src, 0, dest, 0, src.length);
  116.  
  117. int[] src = new int[] {1, 2, 3, 4, 5};
  118. int[] dest = Arrays.copyOf(src, src.length);
  119.  
  120. List<String> items = new ArrayList<String>();
  121. items.add("one");
  122. items.add("two");
  123. items.add("three");
  124. handleItems(items);
  125.  
  126. handleItems(Arrays.asList("one", "two", "three"));
  127.  
  128. class Test extends TestCase {
  129. public void testYadaYada() { ... }
  130. }
  131.  
  132. class Test {
  133. @Test public void yadaYada() { ... }
  134. }
  135.  
  136. String s1 = "...", s2 = "...";
  137.  
  138. if (s1.intern() == s2.intern()) {
  139. ....
  140. }
  141.  
  142. String s1 = "...", s2 = "...";
  143.  
  144. if (s1.equals(s2)) {
  145. ....
  146. }
  147.  
  148. public class Enum
  149. {
  150. public static final Enum FOO = new Enum();
  151. public static final Enum BAR = new Enum();
  152. }
  153.  
  154. public enum Singleton {
  155. INSTANCE;
  156.  
  157. public void someMethod() {
  158. ...
  159. }
  160. }
  161.  
  162. public class Singleton {
  163. public static final Singleton INSTANCE = new Singleton();
  164.  
  165. private Singleton() {
  166. ...
  167. }
  168.  
  169. public void someMethod() {
  170. ...
  171. }
  172. }
Add Comment
Please, Sign In to add comment