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

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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools