Home  >  Article  >  Java  >  Java method return value analysis example

Java method return value analysis example

WBOY
WBOYforward
2023-04-24 21:01:06975browse

1. The return value of a method refers to the result generated by executing the code in a certain method body. The premise is that this approach is likely to produce results.

2. The function of the return value: to receive the result so that it can be used for other operations.

If the method has a return value, the keyword return must be used to return the value, and the return type is the type of the method.

Example

package com.jredu.ch09;
import java.util.Arrays;
import java.util.Scanner;
/**
 * 有返回值的方法
 * 获取排序之后的数组
 * @author Administrator
 *
 */
public class Ch05 {
 
public Scanner input;
public String[] name = new String[5];
 
public Ch05(Scanner input) {
super();
this.input = input;
}
public String[] student(){//返回值类型是一个数组
System.out.println("请输入5个学生的姓名");
for (int i = 0; i < name.length; i++) {
 name[i] = input.next();
}
Arrays.sort(name);//对数组进行排序
System.out.println("****排序后****");
return name;//返回数组name
}
public static void main(String[] args) {
Ch05 d = new Ch05(new Scanner(System.in));
System.out.print(Arrays.toString(d.student())+"\t");
//d.student():调用返回值
//Arrays.toString(d.student()):把数组转换成字符串
}
}

The above is the detailed content of Java method return value analysis example. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete