In Java we can create methods to do specific work and many times we have to return results from java method.
A java method can return any datatypes or object type from method.
1 Return primitive datatypes from method
A Returning int long float double values from a java method
A. Return a integer value from a Method in java
Lets see a simple program to return an maximum integer from method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | public class MethodReturn1 { public static void main(String[] args) { System.out.println("Finding the maximum of two numbers"); Test t = new Test(); int larger = t.max(5, 6); System.out.println("Larger number is " + larger); } } class Test { int max(int num1, int num2) { if (num1 > num2) { return num1; } else { return num2; } } } |
Output
1 2 | Finding the maximum of two numbers Larger number is 6 |
To return any value from method you have to specify return type of method.
and in method provide a return statement to return specific value from method.
Extending the above program for other data types
B Return a integer, long, float, double values from java method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | public class MethodReturn1 { public static void main(String[] args) { System.out.println("Finding the maximum of two numbers"); Test t = new Test(); int larger = t.max(5, 6); System.out.println("Larger number is " + larger); long larger1 = t.max(7l, 8l); System.out.println("Larger number is " + larger1); float larger2 = t.max(9.0f, 7.0f); System.out.println("Larger number is " + larger2); double larger3 = t.max(12.0, 8.9); System.out.println("Larger number is " + larger3); } } class Test { int max(int num1, int num2) { if (num1 > num2) { return num1; } else { return num2; } } long max(long num1, long num2) { if (num1 > num2) { return num1; } else { return num2; } } float max(float num1, float num2) { if (num1 > num2) { return num1; } else { return num2; } } double max(double num1, double num2) { if (num1 > num2) { return num1; } else { return num2; } } } |
Output
1 2 3 4 5 | Finding the maximum of two numbers Larger number is 6 Larger number is 8 Larger number is 9.0 Larger number is 12.0 |
C Returning char and String value from a method in java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | public class MethodReturn2 { public static void main(String[] args) { System.out.println("Example to return String and characters"); Util t = new Util(); String str = t.concat("Hello", "Friends"); System.out.println("String is " + str); char c = t.getCharacter("Hello Friends", 4); System.out.println("Character is " + c); } } class Util { String concat(String str1, String str2) { return str1 + " " + str2; } char getCharacter(String str1, int position) { char c = '\u0000'; try { char[] charArray = str1.toCharArray(); c = charArray[position]; } catch (ArrayIndexOutOfBoundsException e) { System.out.println("Error " + e); } return c; } } |
2 Returning an Array
A Returning an integer array from a method
Lets see returning a value from method using one example below.
Q: Write a Program to create a method to sort integer array in java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | public class ArrayReturn1 { public static void main(String[] args) { int[] arr = {4, 3, 6, 7, 2, 12, 56, 33, 62, 8, 21, 65, 47, 16, 11, 1, 9}; System.out.println("Array Elements before sort"); for (int a : arr) { System.out.print(" " + a); } ArraySort as = new ArraySort(); int[] sortedArray = as.sort(arr); System.out.println("\nArray Elements after sort Sent Array"); for (int a : arr) { System.out.print(" " + a); } System.out.println("\nArray Elements after sort Received Array"); for (int a : sortedArray) { System.out.print(" " + a); } } } class ArraySort { int[] sort(int[] arr) { int temp; for (int i = 0; i < arr.length; i++) { for (int j = i + 1; j < arr.length; j++) { if (arr[i] > arr[j]) { temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } return arr; } } |
Output
1 2 3 4 5 6 | Array Elements before sort 4 3 6 7 2 12 56 33 62 8 21 65 47 16 11 1 9 Array Elements after sort Sent Array 1 2 3 4 6 7 8 9 11 12 16 21 33 47 56 62 65 Array Elements after sort Received Array 1 2 3 4 6 7 8 9 11 12 16 21 33 47 56 62 65 |
B. Java method to return double array from a Java method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | public class ArrayReturn2 { public static void main(String[] args) { double[] arr = {5.3, 6.7, 5.8, 6.7, 44.3}; System.out.println("Array Elements before multiply"); for (double a : arr) { System.out.print(" " + a); } ArrayUtil as = new ArrayUtil(); double[] sortedArray = as.multiply(arr, 5.0); System.out.println("\nArray Elements after multiply Sent Array"); for (double a : arr) { System.out.print(" " + a); } System.out.println("\nArray Elements after multiply Received Array"); for (double a : sortedArray) { System.out.print(" " + a); } } } class ArrayUtil { double[] multiply(double[] arr, double a) { for (int i = 0; i < arr.length; i++) { arr[i] = arr[i] * a; } return arr; } } |
Output
1 2 3 4 5 6 | Array Elements before multiply 5.3 6.7 5.8 6.7 44.3 Array Elements after multiply Sent Array 26.5 33.5 29.0 33.5 221.5 Array Elements after multiply Received Array 26.5 33.5 29.0 33.5 221.5 |
C Return a String array from a method in Java
Here we will take names in array and sort then in ascending order Lets see How to sort Alphabetically in java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | public class ArrayReturn3 { public static String[] sortNames(String[] userNames) { String temp; for (int i = 0; i < userNames.length; i++) { for (int j = i + 1; j < userNames.length; j++) { if (userNames[i].compareTo(userNames[j]) > 0) { temp = userNames[i]; userNames[i] = userNames[j]; userNames[j] = temp; } } } return userNames; } public static void main(String[] args) { String[] names = {"Ram", "Mohan", "Sohan", "Rita", "Anita", "Nita", "Shyam", "Mukund"}; System.out.println("Names before sort"); for (String n : names) { System.out.print(" " + n); } String[] sortedNames = sortNames(names); System.out.println("\nNames after sort (Sent name)"); for (String n : names) { System.out.print(" " + n); } System.out.println("\nNames after sort (Received name)"); for (String n : sortedNames) { System.out.print(" " + n); } } } |
Output
1 2 3 4 5 6 | Names before sort Ram Mohan Sohan Rita Anita Nita Shyam Mukund Names after sort (Sent name) Anita Mohan Mukund Nita Ram Rita Shyam Sohan Names after sort (Received name) Anita Mohan Mukund Nita Ram Rita Shyam Sohan |
D Return character array from a method in java
Here we will pass a string to a method and convert it in to character array and return that character array to main method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public class ArrayReturn4 { public static char[] getCharArray(String str1) { char[] chars = str1.toCharArray(); return chars; } public static void main(String[] args) { String string = "Java Programming"; char[] chars = getCharArray(string); System.out.println("\nReceived character array is"); for (int i = 0; i < chars.length; i++) { System.out.println("char[" + i + "]=" + chars[i]); } } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | Received character array is char[0]=J char[1]=a char[2]=v char[3]=a char[4]= char[5]=P char[6]=r char[7]=o char[8]=g char[9]=r char[10]=a char[11]=m char[12]=m char[13]=i char[14]=n char[15]=g |
E Return two dimensional array in java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | public class TwoDArrayReturn1 { public static int[][] add(int[][] x) { for (int i = 0; i < x.length; i++) { for (int j = 0; j < x[i].length; j++) { x[i][j] += 3; } } return x; } public static float[][] add(float[][] y) { for (int i = 0; i < y.length; i++) { for (int j = 0; j < y[i].length; j++) { y[i][j] += 3; } } return y; } public static char[][] add(char[][] c) { for (int i = 0; i < c.length; i++) { for (int j = 0; j < c[i].length; j++) { c[i][j] += 3; } } return c; } public static void main(String[] args) { int[][] a = { {1, 2, 3}, {5, 6, 7} }; float[][] f = { {1.0f, 2.0f, 3.4f}, {7.2f, 8.4f, 11.2f} }; char[][] c = { {'a', 'b', 'c'}, {'d', 'e', 'f'} }; int[][] b = add(a); float[][] g = add(f); char[][] ch = add(c); System.out.println("Printing Array b"); for (int i = 0; i < b.length; i++) { for (int j = 0; j < b[i].length; j++) { System.out.print(b[i][j] + " "); } System.out.println(""); } System.out.println("Printing Array g"); for (int i = 0; i < g.length; i++) { for (int j = 0; j < g[i].length; j++) { System.out.print(g[i][j] + " "); } System.out.println(""); } System.out.println("Printing Array ch"); for (int i = 0; i < ch.length; i++) { for (int j = 0; j < ch[i].length; j++) { System.out.print(ch[i][j] + " "); } System.out.println(""); } } } |
Output
1 2 3 4 5 6 7 8 9 | Printing Array b 4 5 6 8 9 10 Printing Array g 4.0 5.0 6.4 10.2 11.4 14.2 Printing Array ch d e f g h i |
F Return List from a method in java
Here we have created List of integers and passed in a method getSubList().
This method generate sub list by checking value of list.
If element is greater that 50 it will add element to new list and return the new list.
On main method we get sublist and print it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | import java.util.ArrayList; import java.util.List; public class ListReturn6 { public static List getSubList(List<Integer> list) { List<Integer> subList = new ArrayList<Integer>(); for (Integer a : list) { if (a > 50) { subList.add(a); } } return subList; } public static void main(String[] args) { List<Integer> intList = new ArrayList<Integer>(); intList.add(24); intList.add(45); intList.add(99); intList.add(44); intList.add(77); intList.add(40); intList.add(87); List<Integer> subList = getSubList(intList); System.out.println("Sub list is"); for (Integer a : subList) { System.out.print(a + " "); } } } |
1 2 | Sub list is 99 77 87 |