Popular Tutorials
Start Learning JavaCertification Courses
Created with over a decade of experience and thousands of feedback.
Python online compiler
JavaScript online compiler
SQL online compiler
Java online compiler
HTML online compiler
C online compiler
C++ online compiler
C# online compiler
PHP online compiler
Swift online compiler
Kotlin online compiler
TypeScript online compiler
Go online compiler
Rust online compiler
Scala online compiler
Dart online compiler
R online compiler
Ruby online compiler
Java Program to Find Largest Element of an Array
In this program, you'll learn to find the largest element in an array using a for loop in Java.
To understand this example, you should have the knowledge of the following Java programming topics:
Example: Find the largest element in an array
public class Largest {
public static void main(String[] args) {
double[] numArray = { 23.4, -34.5, 50.0, 33.5, 55.5, 43.7, 5.7, -66.5 };
double largest = numArray[0];
for (double num: numArray) {
if(largest < num)
largest = num;
}
System.out.format("Largest element = %.2f", largest);
}
}
Output
Largest element = 55.50
In the above program, we store the first element of the array in the variable largest.
Then, largest is used to compare other elements in the array. If any number is greater than largest, largest is assigned the number.
In this way, the largest number is stored in largest when it is printed.
Also Read:
Did you find this article helpful?