fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. Integer[] integers = new Integer[ 12 ] ;
  13. System.out.println( Arrays.toString( integers ) ) ;
  14.  
  15. integers[ 2 ] = 42 ; // Auto-boxing.
  16. integers[ 5 ] = Integer.valueOf( 99 ) ; // Unnecessary, because of auto-boxing.
  17. System.out.println( Arrays.toString( integers ) ) ;
  18.  
  19. boolean slotAtIndex5HoldsObjectRef = ( null != integers[ 5 ] ) ;
  20. System.out.println( slotAtIndex5HoldsObjectRef ) ;
  21.  
  22. boolean slot5Filled = Objects.nonNull( integers[ 5 ] ) ;
  23. System.out.println( slot5Filled ) ;
  24.  
  25. Integer x = Objects.requireNonNullElse( integers[ 5 ] , Integer.valueOf( 101 ) ) ;
  26. Integer y = Objects.requireNonNullElse( integers[ 7 ] , Integer.valueOf( 101 ) ) ;
  27. System.out.println( "x: " + x ) ;
  28. System.out.println( "y: " + y ) ;
  29. }
  30. }
Success #stdin #stdout 0.13s 52184KB
stdin
Standard input is empty
stdout
[null, null, null, null, null, null, null, null, null, null, null, null]
[null, null, 42, null, null, 99, null, null, null, null, null, null]
true
true
x: 99
y: 101