1

Hi I'm very new to Java and I wonder what is the difference between these two statements:

long statusId = (long)(Some_Valid_Cast); 

and:

Long statusId = (Long)(Some_Valid_Cast); 

Which one should be preferred for casting and why?

Thanks in advance.

sriram
  • 8,562
  • 19
  • 63
  • 82
  • possible duplicate of [Long vs Integer, long vs int, what to use and when?](http://stackoverflow.com/questions/5857812/long-vs-integer-long-vs-int-what-to-use-and-when) – Ian Roberts Dec 13 '12 at 15:29

3 Answers3

3

The first one casts to the primitive type long, and the second to an object of type Long. These are different- see this related question.

Community
  • 1
  • 1
mbatchkarov
  • 15,487
  • 9
  • 60
  • 79
1

Do you want an object representation of a long, or just the number (the primitive) ?

Primitives (int, long etc.) can be interchanged with their object equivalents (Integer, Long etc.). If you use the object variants they can then be inserted in collections that normally take objects (e.g. Map, List, Set) and used wherever an Object is expected. I would, however, normally expect you to be using the primitive variant for most applications.

It's worth looking at this SO question for more information on the pros/cons.

Community
  • 1
  • 1
Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
1

long is primitive type and Long is Object type. Its based on your need. >=Java5 version , explicit type cast not required