how to return array, arraylist, object from a method in java

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.


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


C Returning char and String value from a method in java

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


B. Java method to return double array from a Java method


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


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.


E Return two dimensional array in java


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.