import java.math.*;

/* This is a program that demonstrate some of the new methods in Java 8 that deal with unsigned integers.

               The focus is to show (a) the capacity limitation of a Java int or Integer, and

               (b) how to bypass that limitation by converting a signed integer to long or BigInteger.

*/

public class IntegerTest {

 

               public static void main(String args[]) {

 

                              Integer signedInt = new Integer( Integer.MAX_VALUE );

                              System.out.printf("The maximum value of a signed Integer in Java = %,d.\n",

                                             Integer.MAX_VALUE);

                              signedInt++;

                              System.out.printf("After signedInt++, signedInt = %,d.\n", signedInt);

                              System.out.println();

                                            

                              System.out.println("\nTesting the unsigned methods of the Integer class...");

                              Integer int1 = new Integer ( Integer.MAX_VALUE ) + 1;

                              Integer int2 = new Integer ( Integer.MAX_VALUE ) + 2;

                              System.out.printf("When shown as signed integers: int1 = %,d, int2 = %,d.\n", int1, int2);

                              if ( Integer.compareUnsigned (int1, int2) > 0) {

                                             System.out.printf("%,d > %,d.\n", int1, int2);

                              }

                              else if ( Integer.compareUnsigned (int1, int2) < 0) {

                                             System.out.printf("%,d < %,d.\n", int1, int2);

                              }

                              else {

                                             System.out.printf("%,d == %,d.\n", int1, int2);

                              }

                              long long1 = Integer.toUnsignedLong(int1);

                              long long2 = Integer.toUnsignedLong(int2);

                              System.out.printf("When shown as long (unsigned integers): int1 = %,d, int2 = %,d.\n", long1, long2);

                              if ( Long.compareUnsigned (long1, long2) > 0) {

                                             System.out.printf("%,d > %,d.\n", long1, long2);

                              }

                              else if ( Long.compareUnsigned (long1, long2) < 0) {

                                             System.out.printf("%,d < %,d.\n", long1, long2);

                              }

                              else {

                                             System.out.printf("%,d == %,d.\n", long1, long2);

                              }

 

                                             //BigInteger may also be used to handle very large integers.

                              System.out.printf("\nTesting with the class BigInteger...\n");

                              BigInteger bigInt = new BigInteger("1");

                              BigInteger bigInt2 = new BigInteger("2");

                              bigInt2 = bigInt2.pow(32);

                              System.out.printf("bigInt2 = %,d\n", bigInt2);

                              bigInt2 = bigInt2.add(bigInt);

                              System.out.printf("bigInt2 = %,d.\n", bigInt2);

               }

}