V
V
Vadim Volkov2021-08-19 08:44:44
Java
Vadim Volkov, 2021-08-19 08:44:44

The new method throws a lot of true/false followed by "Exception in thread "main" java.lang.StackOverflowError". How to fix?

I want to write an isNegative() method that takes a number and checks if it is negative/positive.
After compilation, the code gives out something like 5000 false and a thousand more "at Learning6.isNegative(Learning6.java:14)"

What could be the problem?

public class Learning6 {
    public static void main(String[] args) {
        isNegative(-50);

    }

    public static boolean isNegative(int number) {
        if (number < 0) {
            System.out.println("false");
        } else {
            System.out.println("true");
        }
            return isNegative(number);
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Roo, 2021-08-19
@Go_od1Man

The problem is that the method endlessly calls itself without any conditions.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question