I was trying to write an AsycTask in an android application. There I came across Integer and Long data types and I am not sure what they are. I tried using long
in place Long
, but I got an error in eclipse saying
'Syntax error on token "long", Dimensions expected after this token'.
Long is a class. long is a primitive. That means Long can be null, where long can't. Long can go anywhere that takes an Object, long can't (since it isn't a class it doesn't derive from Object).
Java will usually translate a Long into a long automatically (and vice versa), but won't for nulls (since a long can't be a null), and you need to use the Long version when you need to pass a class (such as in a generic declaration).
Q: What's the difference between "long" and "Long"?
A: The former is a "primitive"; the latter is an "object".
Here is a great article suggesting why you might prefer "Long" (the "object wrapper"):
Primitive Types Considered Harmful
PS:
There are many advantages to using the "Long" object wrapper (including "null" values), and many advantages to using the "long" primitive (including conciseness and efficiency).
"Boxing" and "Unboxing" is the mechanism to change between one and the other. Another good link:
Using Boxing With Care
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