For loops using method calls and primitive wrapper objects
Method calls can be used instead of literals comparisons:
[sourcecode language=”java” toolbar=”false”]
public class ForLoopsMethodCalls {
public static void main(String [] arg){
for (int x = intValue(); test(x); x = go (x)){
System. out .println(x);
}
}
public static Boolean test(int x){
return new Boolean(x < 10);
}
public static int intValue(){
return 0;
}
public static int go(int x){
return ++x;
}
}
[/sourcecode]
Wrapper objects can be used where literals are used:
[sourcecode language=”java” toolbar=”false”]
public class ForLoopsWrapperClasses {
public static void main(String [] arg){
for (int x = Integer.valueOf(10); test(x); x = go(x)){
System.out.println(x);
}
}
public static Boolean test(int x){
return new Boolean(x < 10);
}
public static Integer go(int x){
return new Integer(3);
}
}
[/sourcecode]
Leave a Reply