14.5: Boolean Methods
- Page ID
- 15246
Methods can return boolean
values, just like any other type, which is often convenient for hiding tests inside methods. For example:
public static boolean isSingleDigit(int x) { if (x > -10 && x < 10) { return true; } else { return false; } }
The name of this method is isSingleDigit
. It is common to give boolean
methods names that sound like yes/no questions. Since the return type is boolean
, the return statement has to provide a boolean expression.
The code itself is straightforward, although it is longer than it needs to be. Remember that the expression x > -10 && x < 10
has type boolean
, so there is nothing wrong with returning it directly (without the if
statement):
public static boolean isSingleDigit(int x) { return x > -10 && x < 10; }
In main
, you can invoke the method in the usual ways:
System.out.println(isSingleDigit(2)); boolean bigFlag = !isSingleDigit(17);
The first line displays true
because 2 is a single-digit number. The second line sets bigFlag
to true
, because 17 is not a single-digit number.
Conditional statements often invoke boolean
methods and use the result as the condition:
if (isSingleDigit(z)) { System.out.println("z is small"); } else { System.out.println("z is big"); }
Examples like this one almost read like English: “If is single digit z, print ... else print ...”.