1
What is the difference between a method with return and another without return?
Since a method with no return could be just the sum of two numbers, I still have the result of this sum.
1
What is the difference between a method with return and another without return?
Since a method with no return could be just the sum of two numbers, I still have the result of this sum.
3
The non-return method is considered a procedure (Procedure), and the return method is considered function (Function).
The most common is to use as a function and with parameter(s) because a method should do something according to a past information and at the end gives a result. Private methods do this a little less.
Here it is worth highlighting the word result which is the correct term of what you receive, return is the transport procedure, just as there is a difference between parameter and argument, there’s a difference between return and result. Return even every method has, what can exist is a return without a result, but ok we can use the term informally, just understand the correct.
Another impostant point that methods have an implicit parameter called this
(in Java, other languages can give another name), so a method that appears to have no parameters has at least.
There is the static method which does not have this parameter, so it almost always does not make sense to have a static method, which in the background is only a function and is not a method since method always passes messages to the object, without at least one parameter. One of the exceptions to this is the builder which is nevertheless a static method with special characteristics. The builder does not seem to have a return, but has, is that it is already implicit and is always the type of class he will build, can not be a variation of this. So some people prefer a static method using a design pattern Method Factory than using the constructor.
But why is a method with no return usually a mistake? Because it’s common that you want to get a result of that, you don’t want to just have something done and not have a result, that’s using in expressions and not as statements. There are two cases where a non-return method is used:
this
and this is considered bad because it generates side effect, something to be avoided because it is the source of most bugs (on the part of those who know how to program, otherwise other types may be more prevalent). It does not mean that should not use, but should be avoided. A very typical case is the setter
, which is controversial if it should exist, although some manuals say to use.In your example you will almost always make a return method.
Since a method with no return could be just the sum of two numbers, I still have the result of this sum
Here the question is not clear. Do you really have the result? How? To say how you got it is fundamental. It is obtained using a method like the println()
is probably using a screen treatment method next to the business object and this breaks the principle of single responsibility. Not that this can’t be broken in an exercise or something extremely simple, but to train to do certain real things it’s good to practice right in complex systems because that’s what you’ll always do. If you play the result on a class field I start to wonder if this class should really exist, is it not too complex a class with artificial fields just to not have the return on the method? The example given seems to be something unnecessary and a static method with feedback should do the job better. Without context we don’t even know if you’re doing it right or not and we can’t recommend the best for your case because we don’t know what your case is.
If you really want to understand everything I strongly recommend following the links posted, they are important to conceptualize right and not only learn the cake recipe.
0
The difference is that the no-return method works as a service and the method with return as a function.
The return method will bring another part of the code a value or object that can be used. Given your example:
public void somaDois (int a, int b){
System.out.println(a+b);
}
This method will have no return: it will only have the function of summing two numbers. Therefore, the sum result is not being used in any part of the code, it is only being printed in the console.
public int somaDois (int a, int b){
return a+b;
}
With the return of this method, you will get an int type data containing the sum of a+b. You could use it like this:
int resultadoDaSoma = somaDois(1,3);
System.out.println(resultadoDaSoma);
0
A short answer: A method that returns something is needed when you want to fetch some value, such as a method getName, where it is bearing the name of the object.
A "no return" method is used when you intend to change some data in the system but do not want to return anything, for example a method setName. In reality all methods have some return, but methods void return empty, that is, nothing.
Browser other questions tagged java method void
You are not signed in. Login or sign up in order to post.
Welcome! I recommend reading: What’s the difference - A return method and a void method? , Focus on the excerpt : "Face the return method you must return the specified type or a compatibile type, right? Already a void method, returns nothing, it just executes the instructions that are in its scope"
– Luiz Augusto
This has already been answered in C, but I don’t think it helps so much here: https://answall.com/q/38935/101. And look a little further this may be duplicate https://answall.com/q/130990/101, but I’m not sure why the question is not very clear.
– Maniero
Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site
– Maniero