0

I want to convert a String to Long. But I have found 4 different ways to archive that propouse.

Long.getLong(s) - Determines the long value of the system property with the specified name.

Long.valueOf(s) - Returns a Long object holding the value of the specified String

Long.parseLong(s) - Parses the string argument as a signed decimal long.

new Long(s) - Constructs a newly allocated Long object that represents the long value indicated by the String parameter

Besides that "parseLong()" return a long value and the other 3 return Long object. What are the differences between them, what is the best case of use for them?(when to use them), which one gives better performance?

Thanks in advance.


EDIT :

This gave me the difference between "valueOf(s)" and "new Long(s)" and also found the diference between "valueOf(s)" and "Long.parseLong(s)".

But I still dont get Long.getLong(s) what is used for. What does "Determines the long value of the system property with the specified name" means?


UHDante
  • 577
  • 9
  • 21
  • The javadoc for [`Long.valueOf`](https://docs.oracle.com/javase/7/docs/api/java/lang/Long.html#valueOf(long)) explains "...this method should generally be used in preference to the constructor ..." – khelwood Jul 18 '18 at 12:08
  • If you look at the source code, `Long.parseLong` is basically used by all the other methods. – sp00m Jul 18 '18 at 12:09
  • 2
    `Long.getLong()` is completely different, it's used to read system properties, not to convert the input string to a long. Performance is irrelevant. – Kayaman Jul 18 '18 at 12:10
  • The constructor is deprecated. You should use `parseLong(...)` instead. `Long.getLong(...)` determines the `Long`-value of a System property. – Turing85 Jul 18 '18 at 12:10
  • If you don't know what something does, your first stop should be the Javadoc. [`Long.getLong()`](https://docs.oracle.com/javase/8/docs/api/java/lang/Long.html#getLong-java.lang.String-) is - in my opinion - pretty well documented. – Mark Rotteveel Jul 18 '18 at 20:31

1 Answers1

4

Long.getLong(s) does not convert the string inside to long ("123" does not become 123). The string inside is a certain name that the native library will return a long value accordingly.

Long.valueOf(s) when s is string is similar to this one: new Long(Long.parseLong(s))

Long.valueOf(l) when l is a long type will convert the primary data type long to Long. Read about Unboxing and Autoboxing in java

Long.parseLong(s) will convert the string inside to long value.

So they are all different except for Long.valueOf(l) and Long.parseLong(s) they are almost the same however the first one returns a Long object and the other one returns a long the primary data type.

Alan Deep
  • 2,037
  • 1
  • 14
  • 22
  • I know what `Long.getLong` does, but I have no idea what you mean with _"The string inside is a certain name that the native library will return a long value accordingly."_ `Long.getLong` has nothing to with native libraries. It gets the value of the system property with the name passed to `getLong` and converts that value to a `Long` (or `null` if not convertible). See also the javadoc of [`Long.getLong()`](https://docs.oracle.com/javase/8/docs/api/java/lang/Long.html#getLong-java.lang.String-) (which explains it pretty well). – Mark Rotteveel Jul 18 '18 at 20:26
  • @Mark Rotteveel The person who asked the question thinks that the 4 statements are the same. I did not provide an entire explanation of what `Long.getLong` does, I only provided the explanation that it does not convert the `String` to `Long` ( It doesn't `parse` it ). The question is not about what each one does, the question is about which one should he uses and which is more optimized. – Alan Deep Jul 21 '18 at 09:09
  • You didn't address the part that I have no clue what you mean with _"The string inside is a certain name that the native library will return a long value accordingly."_. That is not what it does, as far as I can make sense of it. – Mark Rotteveel Jul 21 '18 at 10:30