How to define a petabyte constant in Java

1 Answer

0 votes
public class PetabyteExample {

    /**
       In Java, the largest integer type is 'long' (64-bit signed).
       Its maximum value is about 9.22 × 10^18.

       A petabyte fits comfortably:

         1 PiB = 2^50  = 1,125,899,906,842,624 bytes
         1 PB  = 10^15 = 1,000,000,000,000,000 bytes

       So both can be stored safely in a long.
    */

    // Binary petabyte (pebibyte), using a bit shift.
    // 1L << 50 = 2^50 bytes.
    public static final long PETABYTE_PIB = 1L << 50;

    // Decimal petabyte (SI petabyte), using a readable numeric literal.
    // Java allows underscores for digit grouping.
    public static final long PETABYTE_PB = 1_000_000_000_000_000L;

    public static void main(String[] args) {
        System.out.println("1 PiB = " + PETABYTE_PIB + " bytes");
        System.out.println("1 PB  = " + PETABYTE_PB + " bytes");
    }
}


/*
run:

1 PiB = 1125899906842624 bytes
1 PB  = 1000000000000000 bytes

*/

 



answered May 2 by avibootz
...