Use of the non-parametric return value method in Java
If the method does not contain a parameter, but has a return value, we call it a method with no parameter return value.
For example: The following code, which defines a method named Calsum, has no arguments, but the return value is of type int, performs an operation that calculates the sum of two numbers and returns the result
650) this.width=650; "src=" http://img.mukewang.com/537d6bbd0001b76003970198.jpg "/>
In the Calsum () method, the return value type is type int, so you must return an integer value using return in the method body.
When calling a method with a return value, it is important to note that because the method executes returns a result, the return value is generally received and processed when the return value method is called. Such as:
650) this.width=650; "style=" width:340px; src= "Http://img.mukewang.com/537d6c7f00012f8406380476.jpg"/>
Operation Result: 650) this.width=650; "src=" http://img.mukewang.com/537d6d0000012b1e01290026.jpg "/>
"Small traps" that cannot be overlooked:
1. If the return type of the method is void, the return value cannot be used in the method!
650) this.width=650; "style=" width:340px; src= "Http://img.mukewang.com/537d6fa40001bb3205080103.jpg"/>
2. The return value of a method can have at most one and cannot return multiple values
650) this.width=650; "style=" width:340px; src= "Http://img.mukewang.com/537d6fc800017bda05350165.jpg"/>
3, the type of the method return value must be compatible, for example, if the return value type is int, you cannot return a String value
650) this.width=650; "src=" http://img.mukewang.com/5382a78b000193f704170112.jpg "/>
Code:
Public class HelloWorld {
public static void Main (string[] args) {
& nbsp;
//Create an object named Hello
helloworld hello = new HelloWorld ();
//calls the Calcavg () method of the Hello object and saves the return value in the variable avg
double avg = Hello.calcavg ();
Double sum = hello.calcsum ();
system.out.println ("Average score:" + avg);
System.out.println ("Sum of the two achievements:" + sums);
}
Defines a method that returns a value of type double
Public double Calcavg () {
Double java = 92.5;
Double php = 83.0;
Double avg = (java + php)/2; Calculate average
return value using return
return avg;
}
Public double calcsum () {
Double java = 92.5;
Double php = 83.0;
Double sum = java + php;
return sum;
}
}
Operation Result:
Average score: 87.75
The sum of two achievements is: 175.5
This article is from "Ghost" blog, please make sure to keep this source http://caizi.blog.51cto.com/5234706/1547999
Java Basics---The use of parameterless with return value methods in Java (37)