Anonymous Arrays in Java are those arrays that are constructed without specifying the size nor assigned a reference. Anonymous arrays are created on-the-fly when needed and cannot be re-accessed (since there is no reference available to the array object), therefore they become eligible for garbage-collection as soon as the code that uses it completes it.
package info.icontraining.core;
public class AnonymousArrays {
   public static void main(String[] args) {
  
      int[] arr1 = {1, 2, 3, 4, 5};
  
      for(int i: arr1) {
         System.out.print(i);
      }
  
      for(int i: new int[]{1, 2, 3, 4, 5} ) {
         System.out.print(i);
      }  
   }
}
No comments:
Post a Comment