Top community members
All Wiki Articles Create Wiki Article

In the past we had a dream,
to have place where we could share IT knowledge,
to ask questions without fear that someone will judge us.

Now we are a group of people who make this dream come true. ❤ 💻

If you think sharing knowledge and helping other is valuable.

join our community - Click here

Java - get long max value

0 contributions
12 points

Short solution:

long MAX_VALUE = Long.MAX_VALUE;

System.out.println(MAX_VALUE); // 9223372036854775807

Java Long max value is  9,223,372,036,854,775,807

Short solution to get long min value:

long MIN_VALUE = Long.MIN_VALUE;

System.out.println(MIN_VALUE); // -9223372036854775808

Java Long min value is -9,223,372,036,854,775,808

1. Different ways of how to get Long max value

  • Using build-in Long.MAX_VALUE
  • Using math power eg: (long) Math.pow(2, 63)
  • Using bit shifting eg: -1L >>> 1
  • Using bit shifting and not eg: ~0L >>> 1

Code example with above methods:

public class Example1 {

    public static void main(String[] args) {

        long MAX_VALUE = Long.MAX_VALUE;
        System.out.println(MAX_VALUE);   // 9223372036854775807

        long MAX_VALUE_2 = (long) Math.pow(2, 63);
        System.out.println(MAX_VALUE_2); // 9223372036854775807

        long MAX_VALUE_3 = -1L >>> 1;
        System.out.println(MAX_VALUE_3); // 9223372036854775807

        long MAX_VALUE_4 = ~0L >>> 1;
        System.out.println(MAX_VALUE_4); // 9223372036854775807
    }
}

Output:

9223372036854775807
9223372036854775807
9223372036854775807
9223372036854775807

2. Long overflow example

public class Example2 {

    public static void main(String[] args) {

        long MAX_VALUE = Long.MAX_VALUE;
        long overflow = MAX_VALUE + 100;
        System.out.println(overflow); // -9223372036854775709
    }
}

Output: 

-9223372036854775709

 

0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more