Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Method → Return in java


Return in java

Return Type in Java Methods The return type of a method in Java specifies the type of value that a method will return. A method may or may not return a value. One that doesn’t return a value has a return type of void. A method can return a primitive value or an instance of any class. The return type can be any of the eight primitive types defined in Java, a class, or an interface. For example, if a method is declared with an int return type, it will return an integer value when called. Return Statement in Java Methods In Java programming, the return statement is used for returning a value when the execution of the block is completed. The return statement inside a loop will cause the loop to break and further statements will be ignored by the compiler. These return types required a return statement at the end of the method. A return keyword is used for returning the resulted value. The void return type doesn’t require any return statement. If we try to return a value from a void method, the compiler shows an error. Here’s an example of a method with a return type and a return statement:
basic example of return in method public class Main{ public static void main(String args[]){ int sum=addNumbers(2,3); system.out.println(sum); } public static int addNumbers(int a, int b) { int sum = a + b; return sum; } }


In this example, addNumbers is a method that takes two parameters (a and b), adds them together, and returns the result. The return type of the method is int, and the return statement is return sum.

Returning a String

example for returning a string in java public class Main { public static void main(String[] args) { String message = getMessage(); System.out.println(message); } public static String getMessage() { return "Hello, World!"; } }


Hello, World!
In this example, the getMessage method returns a string "Hello, World!".

Returning an Array

Example for returning a array in java public class Main { public static void main(String[] args) { int[] array = getArray(); for (int i : array) { System.out.print(i + " "); } } public static int[] getArray() { int[] array = {1, 2, 3, 4, 5}; return array; } }


1 2 3 4 5
In this example, the getArray method returns an array of integers.

Returning Other Data Types:

In Java, a method can return any data type, including objects. If you want to return multiple values of different data types, you can create a class to hold these values and return an object of this class1.
example to return object in java public class Main { public static void main(String[] args) { Result result = calculate(10, 5); System.out.println("Sum: " + result.sum); System.out.println("Difference: " + result.diff); } public static Result calculate(int a, int b) { int sum = a + b; int diff = a - b; return new Result(sum, diff); } } class Result { int sum; int diff; Result(int sum, int diff) { this.sum = sum; this.diff = diff; } }


Sum: 15 Difference: 5
In this example, the calculate method returns an object of the Result class, which holds the sum and difference of two numbers1.