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

Return knowledge points for java beginners

Oct 09, 2016 am 09:54 AM

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;
	}
}
Copy after login

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;
}
Copy after login

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("我们会说话");
}
Copy after login

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)