Java Integer.max() Method: Syntax and Examples, Summaries of Java Programming

Learn about the java integer.max() method and how to use it to find the maximum of two integer values with examples. The method returns the greater of two integers passed as arguments.

Typology: Summaries

2021/2022

Uploaded on 09/27/2022

princesspeach
princesspeach ๐Ÿ‡บ๐Ÿ‡ธ

4.7

(6)

226 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Java Integer.max() โ€“ Examples
In this tutorial, we will learn about Java Integer.max() method, and learn how to use this method to get the
maximum of given two integer values, with the help of examples.
max()
Integer.max(a, b) returns the greater of two integer values: a and b.
Syntax
The syntax of max() method with two integers for comparison as parameters is
where
Paramete r Description
a The first integer to compare.
b The second integer to compare.
Returns
The method returns value of type int.
Example 1 โ€“ max(a, b) : a<b
In this example, we will take two integers in a and b such that a is less than b. W e will find the maximum
of a and b using Integer.max() method. Since a is less than b , max() should return the value of b.
Java Program
Java Integer.max() โ€“ Syntax & Examples
Integer.max(int a, int b)
pf3

Partial preview of the text

Download Java Integer.max() Method: Syntax and Examples and more Summaries Java Programming in PDF only on Docsity!

Java Integer.max() โ€“ Examples

In this tutorial, we will learn about Java Integer.max() method, and learn how to use this method to get the maximum of given two integer values, with the help of examples.

max()

Integer.max(a, b) returns the greater of two integer values: a and b.

Syntax The syntax of max() method with two integers for comparison as parameters is where Parameter Description a The first integer to compare. b The second integer to compare. Returns The method returns value of type int. Example 1 โ€“ max(a, b) : a<b

In this example, we will take two integers in a and b such that a is less than b. We will find the maximum

of a and b using Integer.max() method. Since a is less than b , max() should return the value of b.

Java Program

Java Integer.max() โ€“ Syntax & Examples

Integer.max( int a, int b)

Output Example 2 โ€“ max(a, b) : a=b

In this example, we will take two integers in a and b such that a is equal to b in value. We will find the

maximum of a and b using Integer.max() method. Since a equals b in value, max() returns the value of

a or b.

Java Program Output Example 3 โ€“ max(a, b) : a>b

In this example, we will take two integers in a and b such that a is greater than b. We will find the

maximum of a and b using Integer.max() method. Since a is greater than b , max() should return the

value of a.

Java Program public class Example { public static void main(String[] args){ int a = 5 ; int b = 7 ; int result = Integer.max(a, b); System.out.println("Result of max("+a+", "+b+") = " + result); } } Result of max(5, 7) = 7 public class Example { public static void main(String[] args){ int a = 5 ; int b = 5 ; int result = Integer.max(a, b); System.out.println("Result of max("+a+", "+b+") = " + result); } } Result of max(5, 5) = 5