Menu
Topics Index
...
`
In this topics we will see the method of Integer and Long wrapper classes.

Methods of Integer:
Method Description
static Integer getInteger(String propertyName) Returns the value associated with the environmental property specified by propertyName. A null is returned on failure.
static  String toBinaryString(int num) Returns a String that contains the binary equivalent of num.
static  String toHexString(int num). Returns a String that contains the hexadecimal equivalent of num.
static  String toOctalString(int num) Returns a String that contains the Octal equivalent of num.
static int signum(int i) Returns the signum function of the specified int value.
String toString toString() Returns a String object representing this Integer's value.
static String toString(int i) Returns a String object representing the specified integer.
static String toString(int i, int radix) Returns a string representation of the first argument in the radix specified by the second argument.
static Integer valueOf(int i) Returns a Integer instance representing the specified int value.
static Integer valueOf(String s) Returns an Integer object holding the value of the specified String.
static Integer valueOf(String s, int radix) Returns an Integer object holding the value extracted from the specified String when parsed with the radix given by the second argument.
IntegerDemo
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    
    }
}
OUTPUT

null
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

DESCRIPTION

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.

THINGS TO TRY
  • At LINE A change the parameter hai to sun.arch.data.model and see the output. The output will be 32, Since the Integer value of the property sun.arch.data.model 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.
Long
The following are the Long methods.
Method Description
static decode(String str) throws NumberFormatException Returns a Long object that contains the value specified by the string in str.
boolean equals(Object LongObj) Returns the value of the invoking Long object is equivalent to LongObj. Otherwise, it returns false.
static long parseLong(String str) throws NumberFormatException Returns the <b>long</b> equivalent of the number contained in the string specified by <cw>str using radix 10.
static Long ValueOf(long num) throws NumberFormatException Returns a Long object containing the value passed in num.
static Long ValueOf(String str) throws NumberFormatException Returns a Long object that contains the value specified by the string in str.
LongDemo
class LongDemo
{
    public static void main(String arg[])
    {
        Long l = 1350l;
        System.out.println("Converted to Long Object:" + Long.decode("1200")); // LINE A
        System.out.println("Checks the object and value are equal: " + l.equals(1350l)); // LINE B
        System.out.println("Converted to Long object: " + Long.parseLong("123l")); // LINE C
        System.out.println("Returns a Long Object: " + Long.valueOf(l)); // LINE D
        System.out.println("Converts to Long Object: " + Long.valueOf("4321")); // LINE E
        System.out.println("Returns hash code: " + l.hashCode()); // LINE F    
    }
}
OUTPUT

Converted to Long Object:1200
Checks the object and value are equal: true
Converted to Long object: 123
Returns a Long Object: 1350
Converts to Long Object: 4321
Returns hash code: 1350

DESCRIPTION

At LINE A decode method is used to convert a string to Long object. At LINE B equals method is used to check whether the invoking and passed object are of same type and containing same value. At LINE C parseLong method is used to convert a String object into Long object. At LINE D valueOf method is used to convert a long value to Long Object. At LINE E valueOf method is used to convert a String value to Long Object.

THINGS TO TRY
  • At LINE B remove change the parameter 1350l to 1350 and see the output. The output will be false, because the parameter value 1350 is of type long and is different from Long.
  • At LINE C change the parameter 123 to 123l and see the output. It throws java.lang.NumberFormatException: For input string: "123l" exception. Since it accepts only numbers.

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App