De java.lang.math.max() funktion är en inbyggd funktion i Java som returnerar maximalt två tal. Argumenten tas i int, double, float och long. Om ett negativt och ett positivt tal skickas som argument genereras det positiva resultatet. Och om båda parametrarna som skickas är negativa genereras siffran med den lägre magnituden som resultat.
Syntax:
Freddie Mercury
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>
Returvärde: Funktionen returnerar maximalt två tal. Datatypen kommer att vara densamma som för argumenten.
Nedan ges exempel på funktionen max()
// 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));> >}> }> |
>
>
Produktion:
scan.next java
12.456>
java klassdiagram
// 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));> >}> }> |
>
>
Produktion:
23>
exempel på java lambda
// 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));> >}> }> |
>
>
Produktion:
-23>