[Java] Get multiple values from one return value

While reading the introductory text, I thought, "Isn't it convenient if I can return two or more return values?", So I tried writing while checking. Note so that you don't forget it. (There seems to be a method called Beans and list, but I will study ...)

In conclusion,

I took the method of returning all the values in one array variable or class type variable. In the end, I thought, "Isn't it better to increase the number of methods and return them one by one?"

--When multiple return values are of the same type -** How to return as an array of basic data type ** --When multiple return values are atypical -** How to return as an array of Object type ** -** How to return by class type **

Given the access restrictions, I think the methods are actually more limited.

1. When multiple return values are of the same type

The idea is that if there are multiple return values that you want to return and they are all of the same type, you can put them in the basic data type. You can forcibly insert an int type into a double type, but you don't want to do that, right? I thought, so I'm not thinking about it in that case.

1-1. Return as basic data type array

Sub.java


public class Sub {
    private int A = 100;
    private int B = 200;

    public int[] getAB(){
        int[] array = new int[2];
        array[0] = A;
        array[1] = B;
        return array;
    }
}

Main.java


public class Main {
    public static void main(String[] args){
        Sub sub = new Sub();
        int[] array = sub.getAB(); //Here, the int array is returned from the Sub class.

        System.out.println(array[0]); //output
        System.out.println(array[1]);
    }
}

Execution result.


100
200

2. When multiple return values are atypical

--How to return as Object type with the same idea as above --How to call a field by returning it as a class type

2-1. Return as Object type array

The part that was written as ╩╗int [] above became ╩╗Object []. The idea is that even atypical values can be stored together if it is an Object type array.

Sub.java


public class Sub {
    private int A = 100;
    private double B = 200.0;

    public Object[] getAB() {
        Object[] array = new Object[2];
        array[0] = A;
        array[1] = B;
        return array;
    }
}

Main.java


public class Main {
    public static void main(String[] args) {
        Sub sub = new Sub();
        Object[] array = sub.getAB();

        System.out.println(array[0]);
        System.out.println(array[1]);
    }
}

Execution result.


100
200.0

Is it the best method I've written in this article?

2-2. Return as class type

The idea is that after returning an object, you can call the field of that object with the main method. It can be done, but the drawback is that access cannot be restricted using access modifiers.

Sub.java


public class Sub {
    int circleAreaInt;
    double circleAreaDouble;

    public Sub calcCircleArea(int r) {
        Sub sub = new Sub();
        sub.circleAreaInt = 3 * r * r ;
        sub.circleAreaDouble = 3.14 * r * r;
        return sub;
    }
}

Main.java


public class Main {
    public static void main(String[] args) {
        Sub sub = new Sub();
        sub = sub.calcCircleArea(2);

        System.out.println(sub.circleAreaInt);
        System.out.println(sub.circleAreaDouble);
    }
}

Execution result.


12
12.56

If you pass the radius r of the circle as an argument (int), the method will calculate and return the area of the circle with both the pi (3) of the free generation and the pi (3.14) of the non-clear generation. ..... But I couldn't think of a good example. You don't have to write it this way.

Reference book

[Easy Java 7th Edition] (https://www.amazon.co.jp/%E3%82%84%E3%81%95%E3%81%97%E3%81%84Java-%E7%AC%AC7%E7%89%88-%E3%80%8C%E3%82%84%E3%81%95%E3%81%97%E3%81%84%E3%80%8D%E3%82%B7%E3%83%AA%E3%83%BC%E3%82%BA-%E9%AB%98%E6%A9%8B-%E9%BA%BB%E5%A5%88/dp/4815600848) Pp.251

Recommended Posts

[Java] Get multiple values from one return value
[Java] Get a random value from an array
Try Java return value
When inserting from Java to MySQL, get Auto_Increment_ID (automatic numbering value) as the return value
Note No.5 "HashMap value has multiple values using class" [Java]
Get attributes and values from an XML file in Java
Get country from IP address (Java)
Java arguments, return values and overloads
Get Null-safe Map values in Java
Get caller information from stack trace (java)
Reverse Key from Value in Java Map
[Java] Get tag information from music files
Java: How to send values from Servlet to Servlet
Get history from Zabbix server in Java
Get "2-4, 7, 9" from [4, 7, 9, 2, 3]
[Java] Program example to get the maximum and minimum values from an array
[Java] From two Lists to one array list
Get Value from URL parameter with GetMapping ~ LocalDate ~
How to get Class from Element in Java
Get unixtime (seconds) from ZonedDateTime in Scala / Java
[Android] Get random keys and values from HashMap
[LeJOS] Get EV3 sensor value remotely with Java
[Java] Get KFunction from Method / Constructor in Java [Kotlin]
Try calling synchronized methods from multiple threads in Java
Output the maximum value from the array using Java standard output
[Rails] Save information from one form to multiple tables
[Java] How to get the maximum value of HashMap
Reverse Enum constants from strings and values in Java
Deep Learning Java from scratch 6.2 Initial values of weights