Method in Java

Java method is a collection of statements that are grouped together to perform an operation. 

public class Method {
    public static void main(String[] args) {

        addTwo(true, 3);
        addTwo(false, 3);
    }

    public static int addTwo(boolean calculate, int num) {
        if (calculate) {
            System.out.println("+2: " + (num + 2));
            return num + 2;
        } else {
            System.out.println("+0: " + num);
            return num;
        }
    }
}
+2: 5
+0: 3

Leave a Reply

Your email address will not be published.

ANOTE.DEV