×

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

Misc Operators in Java

In this article, we are going to learn about the misc operators in Java. Misc operators are nothing but the miscellaneous operators. The java programming language supports some of the...

4 minutes read.

Difference Between Thread.start() and Thread.run()

In the Java programming language, the multi-threading concept consists of the start() and run() methods. Thread.start(): The thread's execution is initiated by invoking the start() method. The start() method operates two threads...

4 minutes read.

How to Convert double to int in Java

The double is a larger data type than int. When we assign a larger type value to a variable of smaller type, then we need to perform the explicit conversion....

2 minutes read.

Dangling Else problem in Java

A language interpretation uncertainty is the hanging other issue. The following two types of condition executed code are both possible in programming: 1. if-then-else form 2. if-then form When dealing with the nested...

3 minutes read.

History and Evolution of Java

Java is invented by James Gosling, Patrick Naughton, Chris Warth, Ed Frank, and Mike Sheridan at Sun Microsystems, Inc. in 1991. Java is related to C++, which is inherited from...

3 minutes read.

Bounded buffer problem in Java

The Bounded buffer Problem can also be called a Producer consumer problem. The problem covers two processes—the producer and the consumer—that share a single, fixed-size buffer that serves as a...

4 minutes read.

Lazy Propagation in Segment Tree in Java

The topic of segment trees in Java is continued by the topic of sluggish propagation in segment trees. It is suggested that readers first read through the section tree topic....

4 minutes read.

Stack Program in Java

Stack Program in Java The Stack class is part of the collection framework that inherits the Vector class. Thus, the Stack class can also be called a subclass of the Vector...

5 minutes read.

PriorityBlockingQueue Class in Java

What is the Queue? An abstract data structure like Stacks is a queue. A queue is open on both ends. Data is always pushed to one end, called enqueue, and removed...

4 minutes read.

Arrow Operator in Java

The introduction of arrow functions in ES6 gives you a more precise approach to define JavaScript functions. We can write shorter function syntax thanks to them. Your code will be...

4 minutes read.

BigDecimal toString() in Java

BigDecimal is a Java class that is a part of the java.math package and the java.base module. It implements the ComparableBigDecimal> interface and extends the Number class. The BigDecimal class...

3 minutes read.

Java inheritance with Example

Java inheritance Java inheritance is a mechanism in which a child object acquires all the properties and behaviors of a parent object. It helps in reusing the code and establishes...

7 minutes read.

How to Convert char to int in Java

How to Convert char to int in Java When two variables of different types are involved in the single expression, Java compiler uses built-in library function to convert the variable to...

3 minutes read.

Hybrid Inheritance in Java

The most crucial OOPs concept in Java is inheritance, which enables the transfer of a class's properties to another class. It describes the Is-A relationship generally. We can create a...

3 minutes read.

Sierpinski Number in Java

The Sierpinski triangle—is it a fractal? The Sierpinski Triangle fractals. A self-similar fractal is the Sierpinski triangle. It is made of an equilateral triangle with its residual area successively reduced by...

3 minutes read.

Java Hello World

Let’s start by writing a simple program that prints “Hello World” to the output window. Write the program into any text editor or IDE (Eclipse, Netbeans, etc.) and save the file with the...

2 minutes read.

How to check version of java in Linux

Java is one of the most famous and thoroughly utilized programming tongues from one side of the world to the other. On the off chance that you are a Java...

2 minutes read.

flour pack problem in Java

Create a method called canPack and give it three int parameters: bigCount, smallCount, and objective. The bigCount option indicates the number of large flour bags (5 kilos each). The smallCount argument indicates...

3 minutes read.

Java Swing

Java Swing is the extension of Abstract Windows Toolkit (AWT). Any component designed in Swing will appear the same on any platform. Problems/Disadvantage of Abstract Windows Toolkit (AWT): As we know that...

27 minutes read.

Math fma () method in java

In java, the Math module constitutes of fma () Method in it. This method can be accessed in two ways which can be differentiated by the parameters which are given...

4 minutes read.