M
M
mobilkip2022-01-30 14:49:32
Java
mobilkip, 2022-01-30 14:49:32

Why are NOT void methods needed?

I just started learning programming, so I apologize in advance for stupid questions.

Let's take 2 scripts, both have the same result, BUT one is made using int

public class qwert {
    
    public static int xxx(){
        int x = 10+11;
        return x;
    }
    
    public static void main(String[] args) {
        System.out.println(xxx());
    }
}


And the other through void

public class qwert1 {

    public static void xxx(){
        int x = 10+11;
        System.out.println(x);
    }

    public static void main(String[] args) {
        xxx();
    }
}


So the question is - why do we need other return types besides void?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
HemulGM, 2022-01-30
@mobilkip

Maybe in order to pass values ​​to others, no?
Give me back a value from xxx() so I can pass it to another method.
For example, in the yyy(int value)
method Call the yyy() method and pass it the value that you get from xxx() without an additional variable
Or take and get the data of the LocalDateTime.now() method - think, this is also a method and it void

PS
Это откровенно глупый вопрос человека, который второй день видит программирование.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question