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) {

static boolean isEmpty(String str) {
    return str.length() == 0;


Write a isNegative function that takes a number and checks if it is negative.


If you got stuck and don't know what to do, you can ask a question in our huge and friendly community
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.