Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between long and Long in android code?

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'.
like image 560
Prince Kumar Avatar asked May 23 '13 18:05

Prince Kumar


2 Answers

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).

like image 153
Gabe Sechan Avatar answered Sep 20 '22 18:09

Gabe Sechan


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

like image 37
paulsm4 Avatar answered Sep 22 '22 18:09

paulsm4