Home » Java programming language

Java StrictMath max() method with example

StrictMath Class max() method: Here, we are going to learn about the max() method of StrictMath Class with its syntax and example.
Submitted by Preeti Jain, on December 26, 2019

StrictMath Class max() method

Syntax:

    public static float max(float f1 , float f2);
    public static int max(int i1 , int i2);
    public static long max(long l1 , long l2);
    public static double max(double d1 , double d2);
  • max() method is available in java.lang package.
  • These methods are used to return the maximum one of both the given argument in the method or other words this method returns the largest value of the given two arguments in the method.
  • These methods don't throw an exception.
  • These are static methods, it is accessible with the class name and, if we try to access these methods with the class object then we will not get any error.

Parameter(s):

  • Method accepts two arguments of int / long / float / double that represents the values to find the maximum/largest value.

Return value:

The return type of this method is int / long / float / double – it returns the largest/maximum value from given two arguments.

Note:

  • If we pass NaN as any of the arguments, method returns the NaN.

Example:

// Java program to demonstrate the example 
// of max() method of StrictMath class

public class Max {
    public static void main(String[] args) {
        // variable declarations
        float f1 = -0.0f;
        float f2 = 0.0f;
        float f3 = -0.6f;
        float f4 = 124.58f;

        int i1 = -0;
        int i2 = 0;

        long l2 = 0l;
        long l3 = -2468l;
        long l4 = 12458l;

        double d1 = -0.0;
        double d2 = 0.0;

        // Display previous value of f1,f2,f3 and f4  
        System.out.println("f1: " + f1);
        System.out.println("f2: " + f2);
        System.out.println("f3: " + f3);
        System.out.println("f4: " + f4);

        // Display previous value of i1,i2 
        System.out.println("i1: " + i1);
        System.out.println("i2: " + i2);

        // Display previous value of l1,l2 
        System.out.println("l3: " + l3);
        System.out.println("l4: " + l4);

        // Display previous value of d1,d2
        System.out.println("d1: " + d1);
        System.out.println("d2: " + d2);


        System.out.println();
        System.out.println("max(float,float): ");

        // Here , we will get (0.0) because we are 
        // passing parameter whose value is (-0.0f,0.0f)
        System.out.println("StrictMath.max(f1,f2): " + StrictMath.max(f1, f2));

        // Here , we will get (124.58) and we are 
        // passing parameter whose value is (0.0f,124.58f)
        System.out.println("StrictMath.max(f2,f4): " + StrictMath.max(f2, f4));

        System.out.println();
        System.out.println("max(int,int): ");
        // Here , we will get (0) because we are 
        // passing parameter whose value is (-0,0)
        System.out.println("StrictMath.max(i1,i2): " + StrictMath.max(i1, i2));

        System.out.println();
        System.out.println("max(long,long): ");

        // Here , we will get (12458) and we are
        // passing parameter whose value is (0l,12458l)
        System.out.println("StrictMath.max(l2,l4): " + StrictMath.max(l2, l4));

        System.out.println();
        System.out.println("max(double,double): ");

        // Here , we will get (0.0) because we are 
        // passing parameter whose value is (-0.0,0.0)
        System.out.println("StrictMath.max(d1,d2): " + StrictMath.max(d1, d2));
    }
}

Output

f1: -0.0
f2: 0.0
f3: -0.6
f4: 124.58
i1: 0
i2: 0
l3: -2468
l4: 12458
d1: -0.0
d2: 0.0

max(float,float): 
StrictMath.max(f1,f2): 0.0
StrictMath.max(f2,f4): 124.58

max(int,int): 
StrictMath.max(i1,i2): 0

max(long,long): 
StrictMath.max(l2,l4): 12458

max(double,double): 
StrictMath.max(d1,d2): 0.0






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.