Conditional execution

The heart of the sort is the condition and the value swapping inside the loop.

if (names[j].compareTo(names[j + 1]) > 0) {
                    final String tmp = names[j + 1];
                    names[j + 1] = names[j];
                    names[j] = tmp;
                }

There is only one conditional command in Java, the if command. It has the following format:

if( condition ) block else block

The meaning of the code structure is quite straightforward. If the condition is true, then the first block is executed, otherwise, the second block is executed. The else keyword, along with the second block, is optional. Creating else and a block after it is optional. If there is nothing to be executed when the condition is false, then we just do not create the else part. If the array element indexed ...

Get Java Projects - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.