Java – is it a best practice to create an unused reference to the value returned by the called method?

I have a friend whose teacher thinks this is a good habit:

public void enterAnythingToContinue(){
    String junk = in.nextLine();
}

I googled this, but couldn't find an explanation, although as expected, other users' method; Mainly teachers

It doesn't matter if you do, it's just a clear and pedagogical question?

Even if you don't have a definite answer, any input will be appreciated

Solution

It can point out the contradiction of methods in Java You can use this method like this

String junk = in.nextLine();

Or that

in.nextLine();

Maybe your teacher will emphasize a "function" rather than a "void return type" If you assign a value, the reader will know that you are using a method that returns a value

When you use a modern IDE, I don't see any advantages

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>