为什么它 return 不是一个值?

Why doesn't it return a value?

public class Test {

    public String xyz(){
        String name="stack";
        return name;
    }

    public static void main(String[] args) {

        Test t=new Test();
        t.xyz(); //this should stack isn't it??
    }
}

该方法 return 一个值(String 类型),但您的代码丢弃了它。

t.xyz(); // This calls the method and discards the return value

如果要查看 return 值,请将其分配给变量并打印出来:

String str = t.xyz();
System.out.println(str);