lundi 1 novembre 2021

java return if else [duplicate]

package org.otsi.test;

public class Test {

    static int count = 0;

    public static void main(String[] args) {
        System.out.println("Main Started");
        if (count < 3) {
            count++;
            main(null);
            System.out.println("After Main(null)");
        } else {
            System.out.println("In else");
            return;
        }
        System.out.println("Hello World!");

    }

}

Output :

Main Started
Main Started
Main Started
In else
After Main(null)
Hello World!
After Main(null)
Hello World!
After Main(null)
Hello World!

How the return is behaving here? Why Hello World printed thrice?

Aucun commentaire:

Enregistrer un commentaire