7

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'.
Laksh
  • 6,717
  • 6
  • 33
  • 34
Prince Kumar
  • 388
  • 1
  • 4
  • 15
  • You might want to read about Java Boxing/Unboxing, which is why you may not have noticed the difference before: http://docs.oracle.com/javase/tutorial/java/data/autoboxing.html – onit May 23 '13 at 18:12

7 Answers7

22

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

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
8

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

paulsm4
  • 114,292
  • 17
  • 138
  • 190
1

Integer and Long are object wrappers on the int and long primitive data types.

AsyncTask uses generics to determine values however and generics only takes Objects as parameters.

FDinoff
  • 30,689
  • 5
  • 75
  • 96
0

AsyncTask uses generic parameters which require reference type parameters. long is a primitive type so is not allowed. On the other hand Long is a class so should be used instead.

Reimeus
  • 158,255
  • 15
  • 216
  • 276
0

long is a primitive data type whereas Long is an object.
AsyncTask can only take as parameters objects.

Thomas Kaliakos
  • 3,274
  • 4
  • 25
  • 39
0

Long is the Object form of long...

You should use long and int except where you need to make use of methods inherited from Object, such as hashcode. java.util.collections methods usually use the boxed (Object-wrapped) versions because they need to work for any Object

long is also pass-by-value whereas, Long is pass-by-reference value like all non-primitive Java types

Also, Long can be null

Nathan
  • 24,586
  • 4
  • 27
  • 36
0

You also have to be aware of the space that both of them take.

Long inherits from another class and also contains other values inside:

public final class Long extends Number implements Comparable<Long> {
    @Native public static final long MIN_VALUE = 0x8000000000000000L;
    @Native public static final long MAX_VALUE = 0x7fffffffffffffffL;
}

long is just a single primitive that takes up 8 bytes of space.

This can become very relevant when you are dealing with a lot of data being stored in memory or sent across a network.

Nav
  • 19,885
  • 27
  • 92
  • 135