Home Segments Top Top Previous Next

399: Mainline

Because a conditional-operator expression, unlike an if statement, produces a value, you can place it inside another expression. In the following, for example, a conditional-operator expression appears inside a display expression, solving the duplication problem encountered in Segment 395:

public class Demonstrate { 
 public static void main (String argv[]) { 
  int change = 1; 
  System.out.print("The length has changed by "); 
  System.out.print(change); 
  System.out.println(change == 1 ? " minute" : " minutes"); 
 } 
} 
--- Result --- 
The length has changed by 1 minute