Top community members
All Wiki Articles Create Wiki Article

Welcome to Dirask IT community! ❤ 💻
We are community of people that helps each other.

If you are beginner in IT field, you are more then welcome to ask questions, it will help you to learn faster. We are here to help you.

We are always beginner in something, we just need to remember it along the way.

there are no wrong questions - Ask Question

Java - get long min value

0 contributions
2 points

Short solution:

long MIN_VALUE = Long.MIN_VALUE;

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

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

Short solution to get long max value:

long MAX_VALUE = Long.MAX_VALUE;

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

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

1. Different ways of how to get Long min value

  • Using build-in Long.MIN_VALUE
  • Using Math power() method eg: (long) Math.pow(2, 63) + 1
  • Using bit shifting eg: -1L << 63
  • Using bit shifting and not eg: ~(~0L >>> 1)
public class Example1 {

    public static void main(String[] args) {

        long MIN_VALUE = Long.MIN_VALUE;
        System.out.println(MIN_VALUE);   // -9223372036854775808

        long MIN_VALUE_2 = (long) Math.pow(2, 63) + 1;
        System.out.println(MIN_VALUE_2); // -9223372036854775808

        long MIN_VALUE_3 = -1L << 63;
        System.out.println(MIN_VALUE_3); // -9223372036854775808

        long MIN_VALUE_4 = ~(~0L >>> 1);
        System.out.println(MIN_VALUE_4); // -9223372036854775808
    }
}

Output:

-9223372036854775808
-9223372036854775808
-9223372036854775808
-9223372036854775808

2. Long minimum value overflow example

public class Example2 {

    public static void main(String[] args) {

        long MIN_VALUE = Long.MIN_VALUE;
        long overflow = MIN_VALUE - 100;
        System.out.println(overflow); // 9223372036854775708
    }
}

Output:

9223372036854775708

 

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