Home  >  Article  >  类库下载  >  Return knowledge points for java beginners

Return knowledge points for java beginners

高洛峰
高洛峰Original
2016-10-09 09:54:422591browse

return-English literal translation returns, meaning to return;

In Java, return means returning data. In Java, a method is usually defined to implement a certain function.

package sreturn;
public class Sreturn {
	public int Sreturn(int a, int b) {
		int c = a + b;
		return c;
	}
}

The above code implements addition c=a+b; what is returned is an int type c. In other words, this method implements a simple addition function, and returns us a value of c after implementation (note that the return only gives the user a usable data type, and does not output the system). If there is no return to return to us the result of this addition, then our code and method will be meaningless.

The following are three examples of returning different data types:

1.public String Sreturn2(){
			String name = "高兴";
			return name;
		}
2.public double Sreturn3(double c,double d){
			double f = c/d;
			return f;
		}
3.public boolean Sreturn4(){
	boolean a= false;
	return a;
}

The above three examples implement different types of return values ​​string, double and Boolean respectively.

So when to use return? Do you use return every time? What does it look like without using return?


There are two ways to use return in java:

1. Return a value of the corresponding type; //If the method declares a certain data type, it must return the same data type.

2. End the execution of the program; //A single return statement indicates the end of execution of the statement.


The second example is as follows:

if(a>4){
	return;
}
else{
	System.out.println("xxxx");
}
    当程序不需要返回值时,我们需要使用void关键字如下:
public void Speaking(){
  System.out.println("我们会说话");
}

This example uses the void keyword. We don’t need to return any value. It is just a method. When we use this method, it will output "we can talk". When the void keyword appears in the program, it means that there is no need to return or return.

The return and void keywords can control the flow of the method.


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn