Java Beginner return Knowledge point

Source: Internet
Author: User

return-English literal translation return, return the meaning;

In Java, return represents the meaning of the returned data. In Java, you typically define a method that implements a function.

Package Sreturn;

public class Sreturn {

public int Sreturn (int a, int b) {

int C = a + B;

return C;

}

}

The previous section of the code implements the addition c=a+b; returns an int of type C. In other words, this method implements a simple addition function, which is implemented to return a value of C (note that returning only gives the user a data type that can be used, not the output system). If there is no return to the result of this addition, then our code and method are meaningless.

Here are three examples that return different data types:

1.public String Sreturn2 () {

String name = "Happy";

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 do I use return? With a return every time? What is the time when you don't use return?


There are two ways to use return in Java:

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

2. End the execution of the program;//A return statement that represents the end of the statement execution.


The second example is as follows:

if (a>4) {

Return

}

else{

System.out.println ("xxxx");

}

When the program does not need to return a value, we need to use the Void keyword as follows:

public void Speaking () {

System.out.println ("We will speak");

}

This example uses the Void keyword, we do not need to return any value, it is just a method, we use this method will output "We can talk." When the Void keyword appears in the program, it does not need to be returned or return.

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

Thank you for the evaluation points and pros.

Java Beginner return Knowledge point

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.