How to return object reference as parameter in Java

Although I have been programming C, C ++ and C # for many years, I am only superficially familiar with Java. Helping my comp Sci son with a Java college project, he had to return references to two objects from a method in Java. I suggested returning it as a function value, and the second as a link. He did not know how to do it. I did a little research and realized that it would be impossible. My question is in Java, what is the general method used when a method should return more than one object reference. Here is a concrete example in my case with sons.

// This method returns references to the head and tail objects from the passed in // linked list. The head object is returned as the function value and the tail is // returned as a parameter. public Static Node GetHeadTail(List list, Node tail) 

I understand that the above does not work in Java, since the tail is a node reference, and in Java this link is passed by value. What is the most common way to handle this in Java? My son's solution was to return an array of 2 node objects for the function value. I said that this is a bad decision because it does not document the value of each element of the array. Another solution would be to create an object containing references to the head and tail. However, in the specific example, this was the main pointer that was of the most interest, and if the object were returned, it would create undesirable coding overhead for the calling method if all they wanted was a voice.

+7
source share
7 answers

In this case, Java programmers usually create a class with two members: head and tail . This will be the return type for the getHeadTail(List list) method.

+2
source

You can only pass by value in Java. The best solution is the second sentence proposed by your son, i.e. Returning an object with a head and tail.

+1
source

Java is always passed by value. The difficult thing can be understood is that Java passes objects as links and these links are passed by value. ( Is Java a "pass by reference" or a "missing value"? )

However, you can do something like this:

 public static void main(String[] args) { Car c = new Car("Blue"); System.out.println(c.getName()); changer(c); System.out.println(c.getName()); } public static void changer(Car c) { c.setName("Red"); } 

Car class.

 public class Car { private String name; public Car(String n) { name = n; } public String getName() { return name; } public void setName(String n) { name = n; } } 

The output will be:

 Blue Red 

Knowing this, you can change what tail points to and still be able to return head .

+1
source

Java makes this interesting thing a similar hybrid between passing by value and passing by reference. In principle, a parameter cannot be changed by a function, but a function may ask the parameter to change itself by calling some method inside it. This answer pretty well explains this.

In response to the question "What is the most common way to deal with this in Java?" your decision to create a class that contains a reference to the head and tail is probably the most common and best practice. If possible, it might even be better to have separate getHead and getTail methods.

+1
source

One slightly less obvious solution: use one of the built-in types, such as Queue or LinkedList, which already has a head and tail.

 LinkedList list = new LinkedList(); head = list.getFirst(); tail = list.getLast(); 

There will be many types like this, depending on your needs. Read the docs .

+1
source

A somewhat hacky method is to use an array as a parameter. Then the function can change the value of one or more elements to "return" it. This is not very good, but it does the job and avoids the need to create special wrapper objects for this purpose.

As a note, Scala solves this problem by letting you return tuples.

+1
source

If you are trying to implement a method that returns multiple values ​​as arguments, you can use something like this:

 public class Future<T> { private T instance; public T get() { return this.instance; } public void set(T value) { this.instance = value; } } 

And use it like this:

 class MyCrazyClass { private static void myCrazyMethod(Future<String> returnVal1, Future<Integer> returnVal2, Future<Float> returnVal3) { returnVal1.set("We are cool!"); returnVal2.set(123); returnVal3.set(321F); } public static void main(String[] args) { Future<String> strRetVal = new Future<>(); Future<Integer> intRetVal = new Future<>(); Future<Float> floatRetVal = new Future<>(); myCrazyMethod(strRetVal, intRetVal, floatRetVal); System.out.println(strRetVal.get()); System.out.println(intRetVal.get()); System.out.println(floatRetVal.get()); } } 

Output:

 We are cool! 123 321.0 
0
source

Source: https://habr.com/ru/post/978248/


All Articles