search

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)

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);
public static float max(float x, float y) ;
public static long max( long x, long y );
public static double max(double x, double y);
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+"." );
}
}

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+"." );
}
}

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+"." );
}
}

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+"." );
}
}

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+"." );
}
}

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
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
Is Java Platform Independent if then how?Is Java Platform Independent if then how?May 09, 2025 am 12:11 AM

Java is platform-independent because of its "write once, run everywhere" design philosophy, which relies on Java virtual machines (JVMs) and bytecode. 1) Java code is compiled into bytecode, interpreted by the JVM or compiled on the fly locally. 2) Pay attention to library dependencies, performance differences and environment configuration. 3) Using standard libraries, cross-platform testing and version management is the best practice to ensure platform independence.

The Truth About Java's Platform Independence: Is It Really That Simple?The Truth About Java's Platform Independence: Is It Really That Simple?May 09, 2025 am 12:10 AM

Java'splatformindependenceisnotsimple;itinvolvescomplexities.1)JVMcompatibilitymustbeensuredacrossplatforms.2)Nativelibrariesandsystemcallsneedcarefulhandling.3)Dependenciesandlibrariesrequirecross-platformcompatibility.4)Performanceoptimizationacros

Java Platform Independence: Advantages for web applicationsJava Platform Independence: Advantages for web applicationsMay 09, 2025 am 12:08 AM

Java'splatformindependencebenefitswebapplicationsbyallowingcodetorunonanysystemwithaJVM,simplifyingdeploymentandscaling.Itenables:1)easydeploymentacrossdifferentservers,2)seamlessscalingacrosscloudplatforms,and3)consistentdevelopmenttodeploymentproce

JVM Explained: A Comprehensive Guide to the Java Virtual MachineJVM Explained: A Comprehensive Guide to the Java Virtual MachineMay 09, 2025 am 12:04 AM

TheJVMistheruntimeenvironmentforexecutingJavabytecode,crucialforJava's"writeonce,runanywhere"capability.Itmanagesmemory,executesthreads,andensuressecurity,makingitessentialforJavadeveloperstounderstandforefficientandrobustapplicationdevelop

Key Features of Java: Why It Remains a Top Programming LanguageKey Features of Java: Why It Remains a Top Programming LanguageMay 09, 2025 am 12:04 AM

Javaremainsatopchoicefordevelopersduetoitsplatformindependence,object-orienteddesign,strongtyping,automaticmemorymanagement,andcomprehensivestandardlibrary.ThesefeaturesmakeJavaversatileandpowerful,suitableforawiderangeofapplications,despitesomechall

Java Platform Independence: What does it mean for developers?Java Platform Independence: What does it mean for developers?May 08, 2025 am 12:27 AM

Java'splatformindependencemeansdeveloperscanwritecodeonceandrunitonanydevicewithoutrecompiling.ThisisachievedthroughtheJavaVirtualMachine(JVM),whichtranslatesbytecodeintomachine-specificinstructions,allowinguniversalcompatibilityacrossplatforms.Howev

How to set up JVM for first usage?How to set up JVM for first usage?May 08, 2025 am 12:21 AM

To set up the JVM, you need to follow the following steps: 1) Download and install the JDK, 2) Set environment variables, 3) Verify the installation, 4) Set the IDE, 5) Test the runner program. Setting up a JVM is not just about making it work, it also involves optimizing memory allocation, garbage collection, performance tuning, and error handling to ensure optimal operation.

How can I check Java platform independence for my product?How can I check Java platform independence for my product?May 08, 2025 am 12:12 AM

ToensureJavaplatformindependence,followthesesteps:1)CompileandrunyourapplicationonmultipleplatformsusingdifferentOSandJVMversions.2)UtilizeCI/CDpipelineslikeJenkinsorGitHubActionsforautomatedcross-platformtesting.3)Usecross-platformtestingframeworkss

See all articles

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

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools