Java Math max() metodas su pavyzdžiais

The java.lang.math.max() funkcija yra integruota Java funkcija, kuri grąžina daugiausiai du skaičius. Argumentai imami int, double, float ir long.Jei neigiamas ir teigiamas skaičius perduodamas kaip argumentas, tada generuojamas teigiamas rezultatas. Ir jei abu perduoti parametrai yra neigiami, tada sugeneruojamas mažesnio dydžio skaičius.

Sintaksė:

 dataType max(dataType num1, dataType num2) The datatypes can be int, float, double or long. Parameters : The function accepts two parameters num1 and num2 among which the maximum is returned 

Grąžinimo vertė: Funkcija grąžina daugiausiai du skaičius. Duomenų tipas bus toks pat kaip ir argumentų.

Žemiau pateikiami funkcijos max() pavyzdžiai




// Java program to demonstrate the use of max() function> // when two double data-type numbers are> // passed as arguments> public> class> Gfg {> > > public> static> void> main(String args[])> > {> > double> a => 12.123> ;> > double> b => 12.456> ;> > > // prints the maximum of two numbers> > System.out.println(Math.max(a, b));> > }> }>

Išvestis:

 12.456 




// Java program to demonstrate the use of max() function> // when one positive and one negative> // integers are passed as argument> public> class> Gfg {> > > public> static> void> main(String args[])> > {> > int> a => 23> ;> > int> b = -> 23> ;> > > // prints the maximum of two numbers> > System.out.println(Math.max(a, b));> > }> }>

Išvestis:

 23 




// Java program to demonstrate the use of max() function> // when two negative integers are passed as argument.> public> class> Gfg {> > > public> static> void> main(String args[])> > {> > int> a = -> 25> ;> > int> b = -> 23> ;> > > // prints the maximum of two numbers> > System.out.println(Math.max(a, b));> > }> }>

Išvestis:

 -23