Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

Long: selecting minimum and maximum values and type bounds

Long.max and Long.min select the greater or smaller of two long values. Long.MIN_VALUE and Long.MAX_VALUE define the type's bounds, rather than a comparison result.

Version: Java SE 21; repository runtime, production runtime not attested

Syntax

public static long max(long a, long b)
public static long min(long a, long b)
public static final long MIN_VALUE
public static final long MAX_VALUE

Long.max(a, b) returns the greater of two long values, while Long.min(a, b) returns the smaller. Both methods are static, accept two long arguments, and return a long; equal arguments produce that shared value. Ordering is signed: 7L is greater than -12L.

Long.MIN_VALUE is -9223372036854775808 (−2^63), and Long.MAX_VALUE is 9223372036854775807 (2^63−1). These are public static final long constants, not methods: use them without parentheses. They define the complete range of long, independently of program data.

Do not confuse selecting between two arguments with the type's bounds. Long.min(a, b) does not automatically return Long.MIN_VALUE; it returns the smaller argument. The same distinction applies to Long.max and Long.MAX_VALUE.

Examples

public class Main {
    public static void main(String[] args) {
        long a = -12L;
        long b = 7L;
        System.out.println(Long.max(a, b));
        System.out.println(Long.min(a, b));
        System.out.println(Long.MIN_VALUE);
        System.out.println(Long.MAX_VALUE);
    }
}

Result

7
-12
-9223372036854775808
9223372036854775807

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources