Table of Contents
Syntax of Java max()
Examples to Implement Java max() Function
Example #5
Conclusion
Home Java javaTutorial Java max()

Java max()

Aug 30, 2024 pm 03:38 PM
java

The java max() function used to return the maximum of two numerical values. The java max() function is a built-in function in java, which is defined in Java.lang.math class, so to use the max() function in a program the Java.lang.math class must to be import. The max() function accepts two parameters of numerical data types and returns maximum among both numerical parameters; it can be overloaded for different parameters of data types like int, float, double and long. It does not throw any exception.

ADVERTISEMENT Popular Course in this category 3DS MAX ARCHITECTURE - Specialization | 4 Course Series | 3 Mock Tests

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax of Java max()

Following is a syntax:

Syntax :

data_Type max(data_Type x, data_Type y)
Copy after login

The data_types can be int, float, double and long.

Below are the parameters of the max() function:

The x and y are two numerical parameters among which the maximum number is to be return.

Return value: The function’s return value is a max of two numbers, which is the same data_type as that of the parameters pass.

The max() function in java can be overloaded for different parameters of data types, so the following are the syntax of the max() function in java:

public static int max(int x, int y);
Copy after login
public static float max(float x, float y) ;
Copy after login
public static long max( long x, long y );
Copy after login
public static double max(double x, double y);
Copy after login
Note: If parameters are passed as a positive and a negative number, the function returned a positive result. If both parameters are passed as a negative number, then the function returned a lower magnitude result. If parameters are passed as same value, then the function returned the same result. If either of the parameters is passed as an NAN value, then the function returned NAN as a result.

Examples to Implement Java max() Function

Below are the examples:

Example #1

we write the java code to understand the max() function more clearly with the following example where we use the max() function to find the maximum of the two integer number passed by the user, as below.

Code:

import java.lang.Math;
class Demo
{
public static void main( String[] arg )
{
// variable declaration
int x = 1024, y = 2034, result;
// calling max() built in function which is define in Math built in class
result = Math.max(x,y);
System.out.println("The maximum number among "+x+" and "+y+" is "+result+"." );
}
}
Copy after login

Output:

Java max()

Explanation:

As in the above code, the x and y are two variables declare and initialize with 1024 and 2034 values respectively, later, the max() function is called with passing these variables as parameters (int max(int x, int y) overloaded)and hence the result of this function is 2034, which is the maximum number in 1024 and 2034.

Example #2

We write the java code to understand the max() function where we use the max() function to find the maximum of the two double number passed by the user, as below.

Code:

import java.lang.Math;
class Demo
{
public static void main( String[] arg )
{
// variable declaration
double x = 134.78, y = 56.89, result;
// calling max() built in function which is define in Math built in class
result = Math.max(x,y);
System.out.println("The maximum number among "+x+" and "+y+" is "+result+"." );
}
}
Copy after login

Output:

Java max()

Explanation:

As in the above code, the x and y are two variables declare and initialize with 134.78 and 56.89 double values respectively, later, the max() function is called with passing these variables as parameters (double max(double x, double y) overloaded)and hence the result of this function is 134.78, which is the maximum number in 1024 and 2034.

Example #3

We write the java code to understand the max() function where we use max() function to find the maximum of the two integer numbers accepted by the user from the console.

Code:

import java.lang.Math;
import java.util.Scanner;
class Demo
{
public static void main( String[] arg )
{
// variable declaration
int x, y, result;
// accept two numbers of integer type from console
System.out.println( "Enter the Two Numeric value: ");
Scanner sc = new Scanner(System.in);
x = sc.nextInt();
y = sc.nextInt();
result = Math.max(x,y);
//Print the maximum number between x and y
System.out.println("The maximum number among "+x+" and "+y+" is "+result+"." );
}
}
Copy after login

Output:

Java max()

As in the above code, the x and y are two variables that declare and accepted these variable values from the user through the scanner object. Later, the max() function is called with passing these variables as parameters, and hence the result of this function based on the user entered values like 10 and 20 is 20.

Example #4

We write the java code for max() function where we use the max() function to pass two negative values and find the maximum, as below.

Code:

import java.lang.Math;
import java.util.Scanner;
class Demo
{
public static void main( String[] arg )
{
// variable declaration
int x, y, result;
x = -10;
y = -20;
result = Math.max(x,y);
//Print the maximum number between x and y of lower magnitude
System.out.println("The maximum number among "+x+" and "+y+" is "+result+"." );
}
}
Copy after login

Output:

Java max()

As in the above code, the x and y are two variables declare and initialize with negative values -10 and -20 values respectively, later, the max() function is called with passing these variables as parameters and hence the result of this function is -10, which lower magnitude.

Example #5

We write the java code for max() function where we use the max() function to pass positive and negative values and find the maximum, as below.

Code:

import java.lang.Math;
import java.util.Scanner;
class Demo
{
public static void main( String[] arg )
{
// variable declaration
int x, y, result;
x = 10;
y = -20;
result = Math.max(x,y);
//Print the maximum number between x and y
System.out.println("The maximum number among "+x+" and "+y+" is "+result+"." );
}
}
Copy after login

Output :

Java max()

As in the above code, the x and y are two variables declare and initialize with values 10 and -20 values respectively, and the result of this function is 10.

Conclusion

The java max() function which is a built-in function in Java.lang.math class is used to gets the maximum of two numerical values, as we have seen above with an example.

The above is the detailed content of Java max(). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP vs. Python: Core Features and Functionality PHP vs. Python: Core Features and Functionality Apr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP's Impact: Web Development and Beyond PHP's Impact: Web Development and Beyond Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

PHP: The Foundation of Many Websites PHP: The Foundation of Many Websites Apr 13, 2025 am 12:07 AM

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

See all articles