'using methods in java without return statement [duplicate]

public class Main
{
    static int plusMethodInt(int x, int y){
        System.out.println(x+y);
    }
    static double plusMethodDouble(double x, double y){
        System.out.println(x+y);
    }
    public static void main(String[] args){
        plusMethodInt(8, 5);
        plusMethodDouble(4.3, 5.6);
    }
}

Why am I not able to run this snippet without utilizing return statement?



Solution 1:[1]

Your method declaration is not correct. If you do not want to return any value from the method, then it's return type should be void and not int and double ( as you have declared in the method).

That is why your compiler is throwing error. Rectify it to this :

public class Main {
    static void plusMethodInt(int x, int y) {
        System.out.println(x + y);
    }
    static void plusMethodDouble(double x, double y) {
        System.out.println(x + y);
    }
    public static void main(String[] args) {
        plusMethodInt(8, 5);
        plusMethodDouble(4.3, 5.6);
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Zabon