logo

Metoda Java Math max() cu Exemple

The java.lang.math.max() funcția este o funcție încorporată în Java care returnează maximum două numere. Argumentele sunt luate în int, double, float și long. Dacă un număr negativ și un număr pozitiv este transmis ca argument, atunci rezultatul pozitiv este generat. Și dacă ambii parametri trecuți sunt negativi, atunci numărul cu magnitudinea mai mică este generat ca rezultat.

Sintaxă:



șir în matrice java
 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>

Valoare returnată: Funcția returnează maximum două numere. Tipul de date va fi același cu cel al argumentelor.

Mai jos sunt date exemple ale funcției 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));> >}> }>

>

>

Ieșire:

 12.456>

șirul este gol




// 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));> >}> }>

img css align

>

>

Ieșire:

 23>




egalitatea obiectelor în java
// 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));> >}> }>

>

>

Ieșire:

 -23>