Largest Element in Array in Java
Find the largest number in an array in Java.
Last updated: 11 January 2026
Method 1: Using Loop
Compare each element with max.
Main.javaRun in Compiler →
public class LargestElement {
public static void main(String[] args) {
int[] arr = {25, 11, 7, 75, 56};
// Initialize max with first element
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > max)
max = arr[i];
}
System.out.println("Largest element: " + max);
}
}Output:
Largest element: 75
Explanation
Assume first element is max. If we find a larger element, update max.
Frequently Asked Questions
Try This Program
Copy this code and run it in our free online Java compiler.
Open Java Compiler