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


Exercise available only for signed users.

Please sign in with your GitHub account, this is necessary to track the progress of the lessons. If you do not have an account yet, now is the time to create an account on GitHub.