Signup/Sign In

Difference between Long.getLong and Long.valueOf in Java?

Posted in Programming   LAST UPDATED: SEPTEMBER 10, 2021

    In this blog, we will dig deep into two methods provided by Long class of Java programming language i.e. getLong and valueOf and will try to understand the difference between the two. The Long class wraps a value of the primitive type long in an object. An object of type Long contains a single field whose type is long.

    Let's go ahead and see what is the use of getLong and valueOf method and what's the major difference between both of them.

    We will cover the topic in below three sections:

    1. Discussion and example of getLong method.

    2. Discussion and example of valueOf method.

    3. When should getLong or valueOf be used.


    Long.getLong method:

    The method is used to determine the long value of the System property with the specified name. We have 3 variants of this method as shown below:

    public static Long getLong(string nm)
    public static Long getLong(String nm, long val)
    public static Long getLong(String nm, Long val)

    All these three are used to determine the long value of the system property with the specified name. Here, the special focus should be given on 'system property'.

    You can't pass and expect the output with any random string value to this method. Only System properties, accessible through the System.getProperty(java.lang.String) method. The string value of this property is then interpreted as a long value using the grammar supported by Long#decode and a Long object representing this value is returned.

    If there is no property with the specified name, if the specified name is empty or null, or if the property does not have the correct numeric format, then null is returned.

    Instead of wasting time in theory, let's jump directly towards practical examples.

    package com.studyTonight;
    
    public class LongClass {
    
        public static void main(String args[]) {
            String str = "java.version";
            
            // It will print 1.8.0_31 as a java version.
            System.out.println(System.getProperty(str));
            
            // It will print null as expected. Refer below for reference:
            // --- If there is no property with the specified name, if the specified name is empty or null,
            // --- or if the property does not have the correct numeric format, then null is returned.
            // --- In our case numeric format is not correct. Hence, it prints null.
            System.out.println(Long.getLong(str));
    
            // It will also prints null because System don't have any property associated with key 'lang'.
            str = "lang";
            System.out.println(Long.getLong(str));
            
            // Now, let us set any property within System using below line.
            System.setProperty("myKey", "11111");
            // Here, Long.getLong will return Long object as we discussed in above theory section.
            Long longObject = Long.getLong("myKey");
            // It will print 11111 because myKey is present in System and can be converted to Long as it has correct numeric format.
            System.out.println(longObject);
        }
    }

    Output:

    1.8.0_31
    null
    null
    11111
    


    Long.valueOf method:

    The method is used to return the Long object initialized with the value provided. It has nothing to do with System properties. This is the key difference between getLong and valueOf method of the Long class. We have 3 variants of the valueOf method as shown below:

    public static Long valueOf(String s)
    public static Long valueOf(String s, int radix)
    public static Long valueOf(long l)

    It will throw a NumberFormatException if the string cannot be parsed as a long.

    Let's see a practical example.

    package com.studyTonight;
    
    public class LongClassValueOf {
    
        public static void main(String[] args) {
    
            String str = "11111";
    
            // It will return Long value of the correctly formatted string. i.e. 11111.
            Long longObject = Long.valueOf(str);
            // The below line will print 11111.
            System.out.println("Value = " + longObject);
    
            String str1 = "abc";
    
            // It will throw NumberFormatException as the string can not be converted to Long value.
            Long longObject1 = Long.valueOf(str1);
            // It will print 11111.
            System.out.println("Value = " + longObject1);
        
        }
    }

    Output:

    Value = 11111
    Exception in thread "main" java.lang.NumberFormatException: For input string: "abc"
        at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
        at java.lang.Long.parseLong(Long.java:589)
        at java.lang.Long.valueOf(Long.java:803)
        at com.studyTonight.LongClassValueOf.main(LongClassValueOf.java:17)
    


    When should getLong or valueOf be used?

    The Long.getLong method should be used when we need the Long object in return for any System provided property value and Long.valueOf should be used when we want to convert any normal string value to long type.

    I hope the blog will help you in deciding whether you should use getLong or valueOf method for your use-case. If you like the blog please let us know with your precious comments. For any concern/queries please feel free to write us. smiley

    You may also like:

    About the author:
    Howdy, I’m Rohit. I’m a software engineer living in Indore, Madhya Pradesh, India. I am a fan of technology, reading, and programming. I’m also interested in fitness and innovation. Sincere By Nature. Cricket Fan By Birth....! :)
    Tags:JavaLongvalueOfgetLong
    IF YOU LIKE IT, THEN SHARE IT
     

    RELATED POSTS