×

Java Integer max() method

The max() method of Integer class returns the greater of two int values. It returns the same result as by calling Math.max.

Syntax

public static int max(int a, int b)

Parameters

The parameters ‘a’ and ‘b’ represent the first and second operand.

Return Value

This method returns the greater of ‘a’ and ‘b’.

Example 1

public class JavaIntegerMaxExample1 {
    public static void main(String[] args) {
        Integer val1=134;
        Integer val2=567;
        // returns the greater of two values
        int max=Integer.max(val1,val2);
        System.out.println("The greater number between "+val1+" and "+val2+" is "+max);
    }
}

Output

The greater number between 134 and 567 is 567

Example 2

import java.util.Scanner;


public class JavaIntegerMaxExample2 {
    public static void main(String[] args) {
        Scanner scanner= new Scanner(System.in);
        System.out.print("Enter the total number");
        int n=scanner.nextInt();
        System.out.println("Enter "+n+" elements : ");
        int[] a= new int[n];
                for (int i=0;i<n;i++ ){
            a[i]=scanner.nextInt();
        }
        for (int i=0;i<n;i++){
            for (int j=i+1;j<n;j++){
                int max=Integer.max(a[i],a[j]);
                if (a[j]==max){
                    a[j]=a[i];
                    a[i] =max ;
                }
            }
        }
        System.out.print("Descending Order : ");
        for (int i=0;i<n;i++) {
           System.out.print(a[i]+" ");
        }
    }
}

Output

Enter the total number5
Enter 5 elements :
12
56
90
34
76
Descending Order : 90 76 56 34 12

Example 3

import java.util.Scanner;
public class JavaIntegerMaxExample3 {
    public static void main(String[] args) {
        Scanner scanner= new Scanner(System.in);
        System.out.print("Enter the total number : ");
        int n=scanner.nextInt();
        System.out.println("Enter "+n+" elements : ");
        int max=0;
        int[] a= new int[n];
                for (int i=0;i<n;i++ ){
            a[i]=scanner.nextInt();
        }
        for (int i=0;i<n;i++){
            for (int j=i+1;j<n;j++){
                max=Integer.max(a[i],a[j]);
                if (a[j]==max){
                    a[j]=a[i];
                    a[i] =max ;
                }
            }
        }
           System.out.println("The greatest number is "+max+" ");
    }
}

Output

Enter the total number : 6
Enter 6 elements :
12
876
55
678
34
223
The greatest number is 876

Related Topics

Java Boolean hashCode() Method

The hashCode() method of Boolean class returns the hash code for the specified Boolean object or the given Boolean value. Syntax public int hashCode()public int hashCode(boolean value) Parameters The parameter ‘value’ represents the value...

2 minutes read.

How to get ASCII value of char in Java

Introduction: On this application, you will learn how to find and show the ASCII value of char in Java. That is done with the use of type-casting and also everyday...

4 minutes read.

How to Convert Decimal to Binary in Java

How to Convert Decimal to Binary in Java There are two methods to convert Decimal to Binary. Using toBinaryString() method Using user-defined logic Using Integer.toBinaryString() The toBinaryString() is a static method of Integer...

2 minutes read.

Vigesimal in Java

A number system with a base of 20 is known as the vigesimal in Java. A base-20 (base-score) numeric system, often known as a vigesimal system, is centered on the twenty...

4 minutes read.

How to avoid deadlock in java

Deadlock: A deadlock is an event that never going to occur. In java, deadlock is just a part of the multithreading. It is an environment that allows us to run multiple...

4 minutes read.

Relatively Prime in Java

In this article, you will be very well equipped with a knowledge of a relatively prime number and also Java programs to determine whether a given integer is a relatively...

4 minutes read.

Tower of Hanoi Program in Java

Tower of Hanoi Program in Java The Tower of Hanoi program in Java is written to solve a mathematical puzzle, called Tower of Hanoi, where we have three poles and n...

4 minutes read.

Shopping Bill in Java

Java Shopping Bill Here in this program, we are about to create a JAVA class called Products which will have some properties or attributes like prod_name (Product Name ), qty (quantity),...

4 minutes read.

Java Package Keyword

A collection of classes, interfaces, and subpackages in a Java program are referred to as a package. Here, we'll learn in-depth how to make and use user-defined packages. package is a...

6 minutes read.

Advanced Java Viva Questions

One of the more difficult languages available now is Java. Currently, 10 thousand developers worldwide use the programming language, which is rising daily. So, if you're a Java developer, an aspiring...

9 minutes read.

SHA Decrypt in Java

In this section, we will be acknowledged about the decryption of SHA in Java. Java SHA The SHA cryptographic hash algorithm in cryptography outputs the hash value as an approximately 40-digit-long hexadecimal...

4 minutes read.

Fibonacci Series Program in Java

Fibonacci Series Program in Java using Recursion Fibonacci series is a series whose every term is comprised of adding its previous two terms, barring the first two terms 0 and 1....

3 minutes read.

JIT in Java

What is JIT ? JIT stands for " Just in Time Compiler ". It is called "Just in time" because it is called at the very last moment when the interpretation...

5 minutes read.

Why to use Enum in Java?

In this article, you will be acknowledged about the Enum in java, its uses and mainly its purpose. Enum has various functionalities. Each of them will be discussed. Enum In a computer...

3 minutes read.

Java String compareTo() Method

compareTo() method is used to compare the two specified Strings based on the alphabetical order(lexicographical order) of their characters.It returns positive number ,negative number or 0 Syntax: public int compareTo(String anotherString) Parameters: anotherString: the...

2 minutes read.

DatabaseMetaData in Java

The Data about another data is called Meta data. The DatabaseMetaData interface has the meta data of the database present in the system. It consists of database product name, total...

2 minutes read.

Java Program to Add Digits Until the Number Becomes a Single Digit Number

We will develop Java programme that increase a number's digit count in order to reduce it to one. The issue is also known as the digit root problem. Example Consider the case where...

3 minutes read.

Knapsack problem in Java

We have a collection of items in the knapsack problem. Every object has a weight and a value. These things should go in a knapsack. But there is a weight...

3 minutes read.

Creating a Jar file in Java

The JDK's jar (Java Archive) tool offers the ability to produce jar files that can be executed. If you double-click a jar file that is executable, it will call the...

2 minutes read.

String Matches in Java

Firstly we have to know something about String? Strings are a bundle of different characters that are normally used in Java programming language. Strings are regarded as objects in the Java...

3 minutes read.