I know that the String can be converted to a long using Long.parseLong(String) method and Long(String) constructor.
String str="12356";
Long myvar= Long.parseLong(str);
Long myvar2 = new Long(str);
Both of them gives same output. Value of myvar and myvar2 is same. I would like to know which one gives better performance and when to use parseLong
and when to use new Long(String s)
.
The java. lang. Long. parseLong(String s) method parses the string argument s as a signed decimal long.
The parseLong() method of Java Long class is used to parse the given string argument as a signed long in the radix which is represented by the second argument.
This method returns the long represented by the string argument in the specified radix.
There are many methods for converting a String to a Long data type in Java which are as follows: Using the parseLong() method of the Long class. Using valueOf() method of long class. Using constructor of Long class.
The difference is
parseLong
returns a primitivenew Long()
will always create a new objecctnew Long
will always create a new object, whereas parseLong
doesn't.
I advise you to go through the implementation of each one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With