TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to JAVA

Java Multiple Return Values

Return multiple values from methods. An Object array or custom object can return multiple values.
Multiple return values. A method is called. It returns 3 values—2 ints and a String. In Java we must use a class instance, an array or other collection to return these values.
With an object array, we can return many different types of elements. But the simplest and best solution involves a class. We can return a class with multiple values.
Object array example. Here we introduce a getSizes method that returns an Object array. It can return any number of elements, and these can have any type.Object Arrays

Result: The getSizes() method returns 3 values—an Int, a String and another Int.

Java program that returns multiple values import java.util.Arrays; public class Program { public static Object[] getSizes() { // Return 3 values from this method. // ... Return ints and strings. Object[] array = new Object[3]; array[0] = 10; array[1] = "cat"; array[2] = 30; return array; } public static void main(String[] args) { // Call method with multiple return values. Object[] sizes = getSizes(); System.out.println(Arrays.toString(sizes)); } } Output [10, cat, 30]
Return class instance. Here we create a class instance inside a method and return that. The computeDog() method returns a Dog object.

Tip: The Dog object has a weight (and Int) and a name (a String). The method returns these two values.

Tip 2: An object instance can be passed to the method, which then just sets fields. This can reduce allocations and promote object reuse.

Class
Java program that returns object with multiple values class Dog { public int weight; public String name; } public class Program { public static Dog computeDog() { // Return multiple values in an object instance. Dog dog = new Dog(); dog.weight = 40; dog.name = "Spark"; return dog; } public static void main(String[] args) { Dog dog = computeDog(); // Print return values. int weight = dog.weight; String name = dog.name; System.out.println(weight); System.out.println(name); } } Output 40 Spark
For many return values, or a variable number of return values, an ArrayList is effective. Return an ArrayList from your method. A type like Integer (or Object) can be used.ArrayListArrayList int, Integer
A summary. In Java objects are meant to encapsulate data. So multiple return values are often placed in objects. With methods and logic, we have object-orientation.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf