The function of return in Java

Source: Internet
Author: User
Tags types of functions

There are two common functions of return:
One is the keyword that is used to return the parameter, and if a method with a return value is executed, it needs to return a parameter, example:
public string FunctionTest () {
String a = "abc";
return A;
}
Then this method is called to return a string with the value ABC, string result = FunctionTest ();

The second usage is that the code executes here to terminate.
For example, when the code executes to a certain place there will be several results, and then one of the results can not execute the subsequent code, when there is a return to terminate the subsequent code execution.
  ------------------------------------------------------------------------------------------------

A class that needs to include a data member (and/or) method.

In Java, methods are all present in the form of functions. A function is a method that can be used as part of an expression in a program's procedural language.

For example, you must have used the

1 2 3 4 5 6 7 classEx1 {public static void main(...){    System.out.println("Hello World!");}}

In the code, PRINTLN is the method, followed by a set of parentheses, where the arguments to the passed-in function are placed in parentheses.

You may already know the above, but what is the return value? For example I now need a function add that adds two integers

1 2 3 4 5 6 7 8 9 Ten One A - class Ex2{//新加的int Add(int input1, int input 2){    return input1+input2;} public static void main(...){    System.out.println("Hello World!");}}

As you can see, I added a return statement in the code.

In this case, the return statement is necessary for the reason I'll talk about it next.

Let's use this add function.

1 2 3 4 5 6 7 8 9 Ten One A - - the - - - + class Ex2{int Add(int input1, int input 2){    return input1+input2;} public static void main(...){    //新加的    Ex2 adder=new Ex2();//建立一个本类的对象    int result1,result2;//建立两个整形来储存结果    //请注意接下来的调用    result1 = adder.Add(3,5);    result2 = adder.Add(3,5)+7;    System.out.println(result1);    System.out.println(result2);}}

As you can see, the results of the output will be 8 and 15. Why is it?

We see that return returns the value of the expression after it to the place where it was called. In this case, 3+5=8, the call to add after return 8 is replaced by 8 (please understand this, the actual situation is more complex), and then assign 8 to RESULT1, the second, the 8 returns, the result of the 8+7 15 is assigned to RESULT2.

Now to answer your four questions.

    1. It is self-evident that it is the bridge between the central and the tuned functions in the connection function call.

    2. Returns a value that conforms to its defined type.

    3. Return this value to the place where this function was called.

    4. If you declare a void function, which means that the function does not return any values, you can of course not write the return.

      However, if you declare other types of functions, it is obviously confusing to not write this statement, which must be written and must have a return value.

 

The function of return in Java

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.