OCA Java SE 8 Operators/Statements - Java if statement








A Java statement is a complete unit of execution in Java, terminated with a semicolon ;.

The if-then Statement

The if-then statement allows our application to execute a particular block of code if and only if a boolean expression evaluates to true at runtime.

if(booleanExpression) { 
} 

Curly braces required for block of multiple statements, optional for single statement.

For example, the following code uses if statement to check when to output a message.

if(hourOfDay < 11) 
   System.out.println("Good Morning"); 

If the hour of the day is less than 11, then the message will be displayed.

The following code makes the if statement in the code above to a block of code.

if(hourOfDay < 11) { 
  System.out.println("Good Morning"); 
  count++; 
} 

The block allows multiple statements to be executed based on the if-then evaluation.





The if-then-else Statement

Java has an if-then-else statement.

if(hourOfDay < 11) { 
  System.out.println("Good Morning"); 
} else { 
  System.out.println("Good Afternoon"); 
} 

We can even add more branches to the if statement.

if(hourOfDay < 11) { 
  System.out.println("Good Morning"); 
} else if(hourOfDay < 15) { 
  System.out.println("Good Afternoon"); 
} else { 
  System.out.println("Good Evening"); 
} 

In this example, the Java process will continue execution until it encounters an if-then statement that evaluates to true.

If neither of the first two expressions are true, it will execute the final code of the else block.





if Statement vs Boolean Expression

The following lines of code does not compile.

int x = 1; 
if(x) {  // DOES NOT COMPILE 
} 

In Java, 0 and 1 are not considered boolean values.

In the following code the assignment operators is used as if they were equals == operators in if-then statements:

int x = 1; 
if(x = 5) {  // DOES NOT COMPILE 
} 

Ternary Operator

The conditional operator, ? :, otherwise known as the ternary operator, is the only operator that takes three operands and is of the form:

booleanExpression ? expression1  : expression2  

The first operand must be a boolean expression, and the second and third can be any expression that returns a value.

The ternary operation is really a condensed form of an if-then-else statement that returns a value.

For example, the following two snippets of code are equivalent:

int y = 10; 
final int x; 
if(y > 5) { 
  x = 2 * y; 
} else { 
  x = 3 * y; 
} 

Compare the previous code snippet with the following equivalent ternary operator code snippet:

int y = 10; 
int x = (y > 5) ? (2 * y) : (3 * y); 

There is no requirement that second and third expressions in ternary operations have the same data types.

Compare the following two statements:

System.out.println((y > 5) ? 21 : "java2s.com"); 

int animal = (y < 1) ? 9 : "java2s.com";  // DOES NOT COMPILE 

Both expressions evaluate similar boolean values and return an int and a String, although only the first line will compile.

The System.out.println() does not care that the statements are different types, because it can convert both to String.