Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 1st, 2012  |  syntax: None  |  size: 1.08 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Are multi-dimensional arrays zero-inited?
  2. public class Foo {
  3.   static int[][] arr;
  4.   public static void bar() {
  5.     arr = new int[20][20];
  6.  
  7.     // in the second run of Foo.bar(), the value of arr[1][1] is already 1
  8.     // before executing the next statement!
  9.     arr[1][1] = 1;
  10.   }
  11. }
  12.        
  13. public class Foo {
  14.   static int[][] arr;
  15.   public static void bar() {
  16.     arr = new int[20][20];
  17.  
  18.     System.out.println("Before: " + arr[1][1]);
  19.     // in the second run of Foo.bar(), the value of arr[1][1] is already 1
  20.     // before executing the next statement!
  21.     arr[1][1] = 1;
  22.     System.out.println("After: " + arr[1][1]);
  23.   }
  24.  
  25.   public static void main(String[] args) {
  26.     bar();
  27.     bar();
  28.   }
  29. }
  30.        
  31. Before: 0
  32. After: 1
  33. Before: 0
  34. After: 1
  35.        
  36. // in the second run of Foo.bar(), the value of arr[1][1] is already 1
  37. // before executing the next statement!
  38.        
  39. public class Foo {
  40.   public static void main(String[] args) throws Exception {
  41.     bar();
  42.     bar();
  43.   }
  44.   static int[][] arr;
  45.   public static void bar() {
  46.     arr = new int[20][20];
  47.     System.out.println(arr[1][1]);
  48.     arr[1][1] = 1;
  49.   }
  50. }