Use generics.

public <T>void foo(T[] array) {
    System.out.println(array.length);
}

This will not work for array of primitive types, such as int[], boolean[], double[],... You have to use their class wrappers instead: Integer[], Boolean[], Double[], ... or overload your method for each needed primitive type separately.

If you are curious you may have a look at a analogical problem (generics and classes vs. primitive types) with Streams here: https://stackoverflow.com/a/23010472/2886891

Answer from Honza Zidek on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ array-class-in-java
Arrays Class in Java - GeeksforGeeks
The Arrays class in java.util is a utility class that provides static methods to perform operations like sorting, searching, comparing, and converting arrays.
Published ย  3 weeks ago
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_arrays.asp
Java Arrays
Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value. To declare an array, define the variable type with square brackets [ ] :
Discussions

java - How can I make a method take an array of any type as a parameter? - Stack Overflow
But it doesn't modify the callers reference type. See my example above, using an array of int. 2014-05-21T21:01:30.493Z+00:00 ... It could be possible using Generics. I you don't know about it I recommend you to read about it in the documentation. http://docs.oracle.com/javase/tutorial/jav... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Java - Creating an array of methods - Stack Overflow
I'm designing a text-based adventure game for a school progress. I have each "level" set up as a class, and each explorable area (node) as a method within the appropriate class. What's messing wit... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Array of function pointers in Java - Stack Overflow
I have read this question and I'm still not sure whether it is possible to keep pointers to methods in an array in Java. If anyone knows if this is possible (or not), it would be a real help. I'm t... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Using an array as the parameter for a method in Java - Stack Overflow
I have been using some methods (functions in C++) to solve some algorithm questions. However, I have found some unexpected things happening depending on the type of parameters I used. When I use an int type variable as a parameter, the method does exactly what I expect. The value of variable b is never changed; the method's result is only applied to variable c. Now here is the problem. When I use an int type "array... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_ref_arrays.asp
Java Arrays Reference
The Java Arrays class (found in java.util), has methods that allow you to manipulate arrays.
๐ŸŒ
Study.com
study.com โ€บ courses โ€บ computer science courses โ€บ computer science 109: introduction to programming
Using Arrays as Arguments to Functions in Java - Lesson | Study.com
November 15, 2021 - You will also learn how you can pass arrays as arguments to methods in Java, and how to return arrays from methods. ... When we pass a simple variable argument to a method (for example, variable of data type integer), that argument is stored in the parameter variable and it has local scope.
๐ŸŒ
Quora
quora.com โ€บ How-do-I-pass-an-array-to-a-function-in-Java
How to pass an array to a function in Java - Quora
Varargs: use type... name to ac ... Passing an array to a function in Java is straightforward: you declare the method parameter as an array type and call the method with the array reference.
๐ŸŒ
Javatpoint
javatpoint.com โ€บ passing-array-to-function-in-java
Passing Array to Function In Java - Javatpoint
Passing Array to Function In Java with java tutorial, features, history, variables, object, programs, operators, oops concept, array, string, map, math, methods, examples etc.
Find elsewhere
๐ŸŒ
Software Testing Help
softwaretestinghelp.com โ€บ home โ€บ java โ€บ how to pass / return an array in java
How To Pass / Return an Array In Java
April 1, 2025 - In this case, most of the time, the functions return null. When arrays are involved it is better to return an empty array instead of null. This is because the method of returning the array will have consistency. Also, the caller need not have special code to handle null values. ... Answer: No! Java is always pass-by-value. Note that Java arrays are reference data types thus, they are non-primitive data types.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ how-to-pass-an-array-to-a-function-in-java
Passing an Array to a Function in Java - GeeksforGeeks
November 13, 2024 - Let function GFG() be called from ... in GFG are called "Formal Parameters". The array to pass can be a one-dimensional(1D) array or a multi-dimensional array such as a 2D or 3D array....
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ specs โ€บ jls โ€บ se7 โ€บ html โ€บ jls-10.html
Chapter 10. Arrays
3 weeks ago - The return type of the clone method of an array type T[] is T[]. A clone of a multidimensional array is shallow, which is to say that it creates only a single new array. Subarrays are shared. All the members inherited from class Object; the only method of Object that is not inherited is its clone method. An array thus has the same public fields and methods as the following class: class A<T> implements Cloneable, java.io.Serializable { public final int length = X ; public T[] clone() { try { return (T[])super.clone(); // unchecked warning } catch (CloneNotSupportedException e) { throw new InternalError(e.getMessage()); } } }
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ java โ€บ arrays class in java
Arrays Class in Java - Scaler Topics
May 5, 2024 - The Java Array class was introduced in Java version 1.2. Its methods are used mostly for manipulating an array, including all major operations like **creating, sorting, searching, comparing, ** etc.
๐ŸŒ
FavTutor
favtutor.com โ€บ blogs โ€บ how-to-return-an-array-in-java
How to Return an Array in Java? (from a Method) | FavTutor
September 26, 2024 - Returning arrays from a function are pretty simple. A method that returns an array needs to have the return type of the function set to the appropriate data type for the array. Here is code for how to return an array in java:
๐ŸŒ
iO Flood
ioflood.com โ€บ blog โ€บ java-array-methods
Java Array Methods Explained: Your Array Utilities Guide
February 27, 2024 - These methods are powerful and versatile, allowing you to perform various operations on arrays with ease. This guide will walk you through the most commonly used Java array methods, from the basics to more advanced techniques. Weโ€™ll cover everything from simple methods like Arrays.sort(), Arrays.fill(), and Arrays.toString(), to more complex ones like Arrays.copyOf(), Arrays.equals(), and Arrays.binarySearch().
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ java โ€บ java arrays
Understanding Array in Java - Scaler Topics
April 8, 2024 - Let's now see how to send arrays back from the function using the return keyword. Let's understand it more clearly with a small Java program. ... Like we defined the data type of a function according to the return type in an array, we also need to define a function with an array data type.
๐ŸŒ
Reddit
reddit.com โ€บ r/javahelp โ€บ passing arrays as arguments for a function
r/javahelp on Reddit: Passing arrays as arguments for a function
July 20, 2022 -

I have a method with the following signature - public static void max(int tt []). I can properly initialize an array by doing this - int [] a = {4242343,23,423,423,4};. However, java gives me an array if I try passing an array to a method like this - max( {43,43,34 });. Is there a reason for this?

๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 408623 โ€บ java โ€บ return-array-method
how to return an array from a method (Beginning Java forum at Coderanch)
November 2, 2007 - You can call a method by value or by reference. one thing here to note that Java always passes arrays and vectors by reference. So, any modification to the array elements in another function would mean that the original array would also be modified similarly. However, returning arrays from a function is pretty simple. A method that returns an array needs to have the return type of the function set to the appropriate data type for the array.