Topics Index

IntegerDemo - Java Example Program

class IntegerDemo
    public static void main(String arg[])
        Integer i = 9;
        System.out.println(Integer.getInteger("hai")); // LINE A
        System.out.println("To Binary: " + Integer.toBinaryString(i)); // LINE B
        System.out.println("To Hexadecimal: " + Integer.toHexString(i)); // LINE C
        System.out.println("To Octal: " + Integer.toOctalString(i)); // LINE D
        System.out.println("To specify the sign: " + Integer.signum(-i)); // LINE E
        System.out.println("Returns a String object: " + i.toString()); // LINE F
        System.out.println("Decimal equivalent to radix: " + Integer.toString(16, 2)); // LINE G
        System.out.println("Converts String object to Integer: " + Integer.valueOf("16")); // LINE H    

To Binary: 1001
To Hexadecimal: 9
To Octal: 11
To specify the sign: -1
Returns a String object: 9
Decimal equivalent to radix: 10000
Converts String object to Integer: 16


In the above program we have used the method which are explained in the above table. At LINE A the output is null, Since the given property has no corresponding Integer value. At LINE B, LINE C, and LINE D we are converting the i value to Binary, Hexadecimal and Octal values. At LINE E we are getting the sign of the given Integer value. At LINE F we are converting i to String object. At LINE G decimal equivalent String object of 16 using radix is printed. At LINE H we are converting a String object to Integer object.

  • At LINE A change the parameter hai to and see the output. The output will be 32, Since the Integer value of the property is 32.
  • At LINE E change the parameter to any positive value other than -i and see the output. The output will be 1, Since the passed parameter is a positive value.
  • At LINE G change the parameter (16, 2) to (16, 8) and see the output. The output will be 20, Since the value 16 is converted to the corresponding radix 8.

© meritcampus 2019

All Rights Reserved.

Open In App