If
All but the most trivial computer programs need to make decisions. They need to test some condition and operate differently based on that condition. This is quite common in real life. For instance you stick your hand out the window to test if it’s raining. If it is raining then you take an umbrella with you. If it isn’t raining then you don’t.
All programming languages have some form of an if
statement that allows you to test conditions. In the previous code we should have tested whether there actually were command line arguments before we tried to use them.
All arrays have lengths and we can access that length by referencing the variable arrayname.length
. (Experienced Java programmers will note that this means that the array is an object which contains a public member variable called length.) We test the length of the args
array as follows:
// This is the Hello program in Java
class Hello {
public static void main (String args[]) {
/* Now let's say hello */
System.out.print("Hello ");
if (args.length > 0) {
System.out.println(args[0]);
}
}
}
Compile and run this program and toss different inputs at it. You should note that there’s no longer an ArrayIndexOutOfBoundsException if you don’t give it any command line arguments at all.
What we did was wrap the System.out.println(args[0])
statement in a conditional test, if (args.length > 0) { }
. The code inside the braces, System.out.println(args[0])
, now gets executed if and only if the length of the args array is greater than zero. In Java numerical greater than and lesser than tests are done with the > and < characters respectively. We can test for a number being less than or equal to and greater than or equal to with <= and >= respectively.
Testing for equality is a little trickier. We would expect to test if two numbers were equal by using the = sign. However we’ve already used the = sign to set the value of a variable. Therefore we need a new symbol to test for equality. Java borrows C’s double equals sign, ==, to test for equality.
It’s not uncommon for even experienced programmers to write == when they mean = or vice versa. In fact this is a very common cause of errors in C programs. Fortunately in Java, you are not allowed to use == and = in the same places. Therefore the compiler can catch your mistake and make you fix it before you run the program.
All conditional statements in Java require boolean values, and that’s what the ==, <, >, <=, and >= operators all return. A boolean is a value that is either true or false. Unlike in C booleans are not the same as ints, and ints and booleans cannot be cast back and forth. If you need to set a boolean variable in a Java program, you have to use the constants true
and false
. false
is not 0 and true
is not non-zero as in C. Boolean values are no more integers than are strings.
Experienced programmers may note that there was an alternative method to deal with the ArrayIndexOutOFBoundsException involving try
and catch
statements. We’ll return to that soon.