为什么不返回值?

Xyz*_*Xyz -6 java

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??
    }
}
Run Code Online (Sandbox Code Playgroud)

NPE*_*NPE 5

该方法确实返回一个值(类型String),但您的代码会丢弃它.

t.xyz(); // This calls the method and discards the return value
Run Code Online (Sandbox Code Playgroud)

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

String str = t.xyz();
System.out.println(str);
Run Code Online (Sandbox Code Playgroud)