// program to convert all strings to uppercase and display Iterator itr = al.iterator(); String st; while ( itr.hasNext()) { st = (String) itr.next(); // works provided object in the collection is of type String System.out.println( st.toUpperCase()); }
void displayUpper( Collection c) { Iterator itr = c.iterator(); while ( itr.hasNext()) System.out.println( itr.next().toUpperCase()); }
void display( Collection c) { Iterator itr = c.iterator(); while ( itr.hasNext()) System.out.println( itr.next() ); }
void display( Collection c) { for ( Object obj : c ) System.out.println( obj ); }
void display( Collection c) { for (String s : c ) System.out.println( s.toUpperCase() ); }
int a[] = {10,20,30}; for ( int i : a ) System.out.println( i );
String concatenate(String st[]) { String cs = ""; for ( String s : st ) cs += s; return cs; }
int n[]= {10,20,30}; ArrayList al = new ArrayList(); for ( int i=0; i < n.length ; i ++) al.add ( new Integer( n[i]) ); // explicit boxing
Iterator itr = al.iterator(); Integer obj; int sum =0; while( itr.hasNext()) { obj = (Integer) itr.next(); sum += obj.intValue(); // explicit unboxing }
int n[]= {10,20,30}; ArrayList al = new ArrayList(); for ( int i=0; i < n.length ; i ++) al.add ( n[i] ); // automatic boxing // process data int sum =0; for ( Integer i : al) { sum += i; // automatic unboxing }
In J2SE 1.4 or before we had to refer to each static member explicity using the class name. But in Tiger, once you import static members of the class then static members can be accessed directly.
import static java.lang.Math.*; public class Test { public static void main(String args[]) {int n = -20; System.out.println( abs(n)); // earlier it was Math.abs(n) } }
int sum(int ... values) { int s = 0; for ( int e : values) s += e; return s; }
enum Season { WINTER, SPRING, SUMMER, FALL }
enum Week { MON,TUE,WED,THU,FRI,SAT,SUN } for (Week w : Week.values()) // do something with w
enum Week { MON,TUE,WED,THU,FRI,SAT,SUN } class Test { public static void main(String args[]) { for ( Week w : Week.values()) System.out.println( w + " : " + getWage(w)); } public static int getWage(Week w) { switch(w) { case MON : return 120; case TUE : case WED : return 100; case THU : case FRI : return 90; case SAT : return 130; case SUN : return 150; } } } // end of class