Skip to Content
Java Projects - Second Edition
book

Java Projects - Second Edition

by Peter Verhas
August 2018
Intermediate to advanced
524 pages
14h 45m
English
Packt Publishing
Content preview from Java Projects - Second Edition

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Java 11 Cookbook - Second Edition

Java 11 Cookbook - Second Edition

Nick Samoylov, Mohamed Sanaulla

Publisher Resources

ISBN: 9781789131895Supplemental Content