Menu
Topics Index
...
`

LongDemo - Java Example Program


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.

© meritcampus 2019

All Rights Reserved.

Open In App