Create a boolean array " prime [ 0 . . n ] " and initialize all entries as true . A value in prime [ i ] will finally be false if i is Not a prime .
boolean [ ] prime = new boolean [ n + 1 ] ; Arrays . fill ( prime , true ) ; for ( int p = 2 ; p * p <= n ; p ++ ) {