---
title: "Write a Java program to find the largest number in an array."  
description: "Write a Java program to find the largest number in an array."  
author: "Ravi Vishwakarma"  
published: 2024-07-18  
updated: 2024-07-18  
canonical: https://www.mindstick.com/interview/33948/write-a-java-program-to-find-the-largest-number-in-an-array  
category: "java"  
tags: ["java", "javac"]  
reading_time: 3 minutes  

---

# Write a Java program to find the largest number in an array.

## Example 1,

```java
public class Main {
    public static int findLargest(int[] array) {
        // Check if the array is empty
        if (array == null || array.length == 0) {
            throw new IllegalArgumentException("Array must not be empty");
        }

        // Initialize the largest number with the first element of the array
        int largest = array[0];

        // Iterate through the array to find the largest number
        for (int num : array) {
            if (num > largest) {
                largest = num;
            }
        }

        return largest;
    }

    public static void main(String[] args) {
        int[] array = {1, 4, 5, 7, 3, 9, 2};

        int largestNumber = findLargest(array);
        System.out.println("The largest number in the array is: " + largestNumber);
    }
}
```

## Example -2 Using the Predefined functions

```java
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] array = {1, 4, 5, 7, 3, 9, 2, 10, 0};

        // Sort the array in ascending order
        Arrays.sort(array);

        // Print the last element of the sorted array, which is the largest
        System.out.println(array[array.length - 1]);
    }
}
```

**Read more**\
[**Retrieving and displaying data from a RESTful API in Knockout**](https://www.mindstick.com/articles/335932/retrieving-and-displaying-data-from-a-restful-api-in-knockout-js)

[**Java program to find all pairs of elements in an array whose sum is equal to a given number.**](https://www.mindstick.com/interview/33947/java-program-to-find-all-pairs-of-elements-in-an-array-whose-sum-is-equal-to-a-given-number)

[**How you would reverse a string in Java without using the StringBuilder or StringBuffer classes.**](https://www.mindstick.com/interview/33946/how-you-would-reverse-a-string-in-java-without-using-the-stringbuilder-or-stringbuffer-classes)

## Answers

### Answer by Ravi Vishwakarma

## Example 1,

```java
public class Main {
    public static int findLargest(int[] array) {
        // Check if the array is empty
        if (array == null || array.length == 0) {
            throw new IllegalArgumentException("Array must not be empty");
        }

        // Initialize the largest number with the first element of the array
        int largest = array[0];

        // Iterate through the array to find the largest number
        for (int num : array) {
            if (num > largest) {
                largest = num;
            }
        }

        return largest;
    }

    public static void main(String[] args) {
        int[] array = {1, 4, 5, 7, 3, 9, 2};

        int largestNumber = findLargest(array);
        System.out.println("The largest number in the array is: " + largestNumber);
    }
}
```

## Example -2 Using the Predefined functions

```java
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] array = {1, 4, 5, 7, 3, 9, 2, 10, 0};

        // Sort the array in ascending order
        Arrays.sort(array);

        // Print the last element of the sorted array, which is the largest
        System.out.println(array[array.length - 1]);
    }
}
```

**Read more**\
[**Retrieving and displaying data from a RESTful API in Knockout**](https://www.mindstick.com/articles/335932/retrieving-and-displaying-data-from-a-restful-api-in-knockout-js)

[**Java program to find all pairs of elements in an array whose sum is equal to a given number.**](https://www.mindstick.com/interview/33947/java-program-to-find-all-pairs-of-elements-in-an-array-whose-sum-is-equal-to-a-given-number)

[**How you would reverse a string in Java without using the StringBuilder or StringBuffer classes.**](https://www.mindstick.com/interview/33946/how-you-would-reverse-a-string-in-java-without-using-the-stringbuilder-or-stringbuffer-classes)


---

Original Source: https://www.mindstick.com/interview/33948/write-a-java-program-to-find-the-largest-number-in-an-array

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
