- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Can a method return multiple values in Java?
You can return only one value in Java. If needed you can return multiple values using array or an object.
Example
In the example given below the calculate() method accepts two integer variables performs the addition subtraction, multiplication and, division operations on them stores the results in an array and returns the array.
public class ReturningMultipleValues { static int[] calculate(int a, int b){ int[] result = new int[4]; result[0] = a + b; result[1] = a - b; result[2] = a * b; result[3] = a / b; return result; } public static void main(String args[]){ Scanner sc = new Scanner(System.in); System.out.println("Enter the value of a: "); int a = sc.nextInt(); System.out.println("Enter the value of b: "); int b = sc.nextInt(); int[] result = calculate(a, b); System.out.println("Result of addition: "+result[0]); System.out.println("Result of subtraction: "+result[1]); System.out.println("Result of multiplication: "+result[2]); System.out.println("Result of division: "+result[3]); } }
Output
Enter the value of a: 20 Enter the value of b: 10 Result of addition: 30 Result of subtraction: 10 Result of multiplication: 200 Result of division: 2
- Related Articles
- How can we return multiple values from a function in C#?
- How to return multiple values to caller method in c#?
- How can we return multiple values from a function in C/C++?
- How to return 2 values from a Java method
- Can the main method in Java return a value?
- Can we return this keyword from a method in java?
- How do we return multiple values in Python?
- Can we change return type of main() method in java?
- Find and return array positions of multiple values JavaScript
- Provider values() method in Java
- How can we return null from a generic method in C#?
- How to return an array from a method in Java?
- Can a try block have multiple catch blocks in Java?
- Can we overload a method based on different return type but same argument type and number, in java?
- Can we fetch multiple values with MySQL WHERE Clause?

Advertisements