Home  >  Article  >  Java  >  What does return mean in java

What does return mean in java

(*-*)浩
(*-*)浩Original
2019-05-21 21:32:2339111browse

Return in Java means "return, return"; used for methods, it has two functions: 1. Return the value of the type specified by the method (this value is always determined), or it can be an object; 2. The end method terminates the execution of the code behind "return;".

What does return mean in java

The return statement in Java is always closely related to methods and is generally used in methods.

The common functions of return are as follows:

One is to return the value of the type specified by the method (this value is always certain), or It is an object

Example:

public string functionTest(){
    String a = "abc";
    return a;
}

Then this method will return a string with the value abc after being called,

public class Test{
    public static void main(){
        string result = functionTest();
        System.out.println(result);
    }
}

The second usage is the end of the method.
For example, when the code is executed to a certain place, several results will appear, and then one of the results cannot execute the subsequent code. At this time, adding a "return;" can terminate the execution of the subsequent code.

Example:

public class TestReturn { 
    public static void main(String args[]) { 
       TestReturn t = new TestReturn(); 
        t.test1(); 
        t.test2(); 
    } 

    /** 
     * 无返回值类型的return语句测试 
     */ 
    public void test1() { 
        System.out.println("---------无返回值类型的return语句测试--------"); 
        for (int i = 1; ; i++) { 
            if (i == 4) return; 
            System.out.println("i = " + i); 
        } 
    } 

    /** 
     * 有返回值类型的return语句测试 
     * @return String 
     */ 
    public String test2(){ 
        System.out.println("---------有返回值类型的return语句测试--------"); 
        return "返回一个字符串"; 
    } 
}

运行结果: 
---------无返回值类型的return语句测试-------- 

 i = 1 

 i = 2

 i = 3  
---------有返回值类型的return语句测试------- 

返回一个字符串

The above is the detailed content of What does return mean in java. For more information, please follow other related articles on the PHP Chinese website!

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
Previous article:what is java functionNext article:what is java function