Java: Predicates
Recall the function isInfant
from the previous lesson:
static boolean isInfant(int age) {
return age < 1;
}
isInfant
is a predicate function.
The predicate answers the yes or no question by returning a boolean value.
Predicates in all languages are called in a special way for ease of analysis. In Java, predicates usually begin with the prefix is
or has
:
isInfant
— «is it a baby?»hasChildren
— «do you have children?»isEmpty
— «Is it empty?»hasErrors
— «are there any errors?»
A function can be considered a predicate only if it returns a boolean.
Let's write another predicate function. It takes a string and checks if it is empty. An empty string is the string "" — its length is 0. As we remember, to calculate the length of the string str, you can use the expression str.length().:
public static void main(String[] args) {
System.out.print(isEmpty("Sea"));
}
static boolean isEmpty(String str) {
return str.length() == 0;
}
false
Instructions
Write a isNegative
function that takes a number and checks if it is negative.
Tips
If you got stuck and don't know what to do, you can ask a question in our huge and friendly community