J
J
javanub2014-05-27 14:53:00
Java
javanub, 2014-05-27 14:53:00

What is the difference between void and return in an example?

Here is an example of code that outputs a value from void to main() and assigns a value to the variable "c". Explain the difference from return then. Only in a simple way ala 2 + 2, so that such examples are not confusing. Thank you.
pastebin.com/SZkNuMgi

Answer the question

In order to leave comments, you need to log in

6 answer(s)
T
trerums, 2014-05-27
@trerums

Hmm, of course you wrote the question clumsily, that you can break the brain. If I understood you correctly, then it is not clear to you why the value appears in the variable "c" despite the fact that you did not use return. The fact is that in this case your "c" is a class field that you assign a value to in the getJazz method, return is not needed here at all, because the method does not return anything, it manipulates the fields that belong to the class. If you wanted to write int b = getJazz() then you would have to write return.

E
Eugene, 2014-05-27
@EvgenijDv

Maybe you will start with something like Pascal, figure out what procedures are and how they differ from functions, and only then, gradually begin to deal with Java and OOP?

N
Nikita Kolosov, 2014-05-27
@Anexroid

The bottom line is that 'void' only means that this method does not return any variable, and return is the operation of returning a value.
'c' in your case is a property of an object, not a return value, and inside a method you are assigning, not returning, a value to an object property

A
AxisPod, 2014-05-28
@AxisPod

I remember a joke:
2 crocodiles are flying, one green, the other to the right.

N
Nibilung, 2014-05-27
@Nibilung

you simply write down the value of the variable "c" with your void method, and then simply display it on the screen. If the method were like this:

int getJazz(int a, int b){
     return c = a + b;
}

then it would be possible to get the variable "c" from this method, i.e. by accessing System.out.println(nn.getJazz(12,12);
If the variable "c" had a private access modifier, then you could not refer to it as in your example.
I'll try to explain on my fingers. Let's take your example.
public class Main{
        public static void main(String[] args){
                Jazz nn = new Jazz();
                nn.getJazz(12, 12); // вызов void метода, который записывает в "c" число 24
                nn.c = 51;  // я могу здесь присвоить "c" другое значение, например 51
                System.out.println(nn.c); // Выводим переменную "c" которая равна 51, а не 24
        }
}

K
klirichek, 2014-05-28
@klirichek

What is the meaning of void?
You simply call:
nn.GetJazz(12,12);
and only then, with the next statement, you print the field nn.c
The first line simply calls a method (in this case, a procedure) that does not return anything (i.e. "returns" void)
And the second line you look at the contents of the c field of your class .
These are DIFFERENT lines; unrelated to each other!
Take away c=a+b and just leave a+b - and that's it, only the sum will be displayed from inside the method.
In exactly the same way, you can also write
sin(c=a+b)
This will mean that the sum of a+b is first calculated, its value will be assigned to c and simultaneously returned as the value of the expression c=a+b.
The sine will be taken from the value of this expression. But it will not be assigned anywhere; just take percent calculations, and the result will be thrown away.
As a result, you will also get the value c. However, the meaning of sine is not :).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question