Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it good practice to create an unused reference to the value returned by a called method?

I have a friend whose teacher considers this good practice:

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

I googled this, but didn't find an explanation for it, though as expected there were other users of this method; mainly teachers.

Could it be that it doesn't matter if you do it or not, and that it's just a matter of clarity and pedagogy?

Even if you don't have a definite answer, any input is appreciated.

like image 850
user1972264 Avatar asked Jul 06 '13 09:07

user1972264


People also ask

Does Java support call by reference?

Java does not support call by reference because in call by reference we need to pass the address and address are stored in pointers n java does not support pointers and it is because pointers breaks the security. Java is always pass-by-value.

Does Java return by reference?

Java is Pass by Value and Not Pass by Reference.

Is Java call by value or call by reference stackoverflow?

Object references are passed by value The reason is that Java object variables are simply references that point to real objects in the memory heap. Therefore, even though Java passes parameters to methods by value, if the variable points to an object reference, the real object will also be changed.

Does java pass by value or pass by reference*?

Java is always pass by value and not pass by reference .


1 Answers

It could point to the ambivalence of methods in Java. You can use this method like this

String junk = in.nextLine();

or that

in.nextLine();

Maybe your teacher will emphasize on a "function" rather to a "procedure" (void return type). If you assign a value the reader knows you are using a method that returns a value.

I don't see any advantage while you are using modern IDEs.

like image 194
PeterMmm Avatar answered Sep 22 '22 01:09

PeterMmm