How do i print out a set of integers in an args array. I intend to have an output of: The numbers are 1, 2, 3, 4, 5
System.out.println("The numbers are ");
for(int i=0; i< args.length;i++)
System.out.print(args[i] + " ";
The compiler gives me an error of “i cannot be resolved to a variable” When i resolve it with “int b = 0;” it gives me a single integer, rather than the integers in args
As of Java 5 you can also skip
ientirely:Additionally, I’d like to advocate using curly braces even for one-statement blocks.
It’s easier to read and less likely to change the behavior by adding a statement (like some log statement).
Example, a naive
pow()method (checks skipped for simplicity):Orignal code:
Now I’d like to log the result in each step:
Suddenly, I get a wrong result for
power > 2, since nowresult *= base;is only executed once (it is now outside the loop).