171

Sometimes I see API's using long or Long or int or Integer, and I can't figure how the decision is made for that?

When should I choose what?

Borealid
  • 95,191
  • 9
  • 106
  • 122

7 Answers7

225

Long is the Object form of long, and Integer is the object form of int.

The long uses 64 bits. The int uses 32 bits, and so can only hold numbers up to ±2 billion (-231 to +231-1).

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, and a primitive type, like int or long, is not an Object.

Another difference is that long and int are pass-by-value, whereas Long and Integer are pass-by-reference value, like all non-primitive Java types. So if it were possible to modify a Long or Integer (it's not, they're immutable without using JNI code), there would be another reason to use one over the other.

A final difference is that a Long or Integer could be null.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Borealid
  • 95,191
  • 9
  • 106
  • 122
  • 8
    @Borealid, this is so wrong, Java is always pass by value, there is nothing such as pass by reference in Java. – Diego Ramos Jul 04 '18 at 15:03
  • 15
    Uh huh? Okay, let's explore the idea that Java never passes a reference. If I call a function giving it an int[2], and then inside the function I change the value held in the first element of that array, will the function's caller see that change? Yes? Well that's because what was passed to the function wasn't the values in the array, but rather a reference to those values. "Pass by value" means the actual data themselves are being copied around with no indirection. Whomever taught you that Java was pass-by-value did you a disservice. – Borealid Jul 04 '18 at 21:12
  • 6
    @Borealid you are incorrect, the value is changed because a new copy of the reference to the array was created, that is why whoever is calling the function will see the change, this is still pass by value. I can't believe you say that there is pass by reference in Java. You never pass the original reference to the function. – Diego Ramos Jul 05 '18 at 00:06
  • 7
    In a theoretical world where Java behaved completely differently from how it actually does, and passing an object or array to a function resulted in a totally separate copy (where modifications inside the function weren't reflected to callers at all), how would you then describe the language? Keeping in mind that what we're talking about is completely not what Java actually does. – Borealid Jul 05 '18 at 11:19
  • 6
    @DiegoRamos is right. Java is always pass by value, the confusion is created because the value when non primitive is a memory reference, static swapping does not work in java, more details here: https://www.journaldev.com/3884/java-is-pass-by-value-and-not-pass-by-reference – Panthro Jan 27 '19 at 22:17
  • 3
    Actually, java is "pass by value" only: a *reference* to an object will be passed **by value**, as a function argument (so references are copied into the stack frame of callees). The actual data referred to by a reference is never implicitly copied; merely the reference to it is. – ljleb Apr 19 '20 at 06:51
  • 3
    Let's say I have a function `foo(x)`. I invoke it by calling `foo(my_arr)`, and the function contains the lines `x[1]=5; x=[];`. If the language uses "pass by value" semantics, the _value_ of the array would be copied - not just the reference - and the caller would see no change to `my_arr`. If it uses "pass by reference" semantics, then after the call, `my_arr` would be empty. If the language uses "pass by reference value" semantics, the caller would see a `my_arr` with its second element set to `5`. Let's not come up with wording that makes two of these scenarios be called the same thing. – Borealid Apr 19 '20 at 10:13
  • Can anyone honestly 100% believe that any programming language be pass by value only? Passing by value, for some kind of structure anyway, is where the caller allocates onto the stack a new copy of the object prior to the function call. The caller then performs the usual function call semantics, passing the address of the "pre-allocated" and duplicated structure. The callee now has access to their own copy, or to a "pass by value" structure using the address it was given. It's up to the Callee to deallocate this structure and to fix the stack for the caller on return, to avoid a memory leak. – GodDamn Apr 24 '21 at 22:49
  • You can incorrectly state those steps as "pass by reference," but if you do, and you initiate a call to an external library function elsewhere with the callee expecting "pass by value" semantics, passing the address to your caller's structure. Your caller's structure will be deallocated on return, and there will be no structure at all on the return back to the caller. -Pass by Value duplicates the elements of Struct A to a new location of Struct B. -Pass by Reference simply uses the location of Struct A. No matter what! They all pass a memory location which can fit inside a register. – GodDamn Apr 24 '21 at 23:13
  • I think I described, above your comment, three different ways that passing an array can behave. – Borealid Apr 25 '21 at 02:05
  • Correction, Class forms not Object forms so Long is the Class form of long, and Integer is the Class form form of int – Ahmed Mar 17 '23 at 10:26
34
  • By default use an int, when holding numbers.
  • If the range of int is too small, use a long
  • If the range of long is too small, use BigInteger
  • If you need to handle your numbers as object (for example when putting them into a Collection, handling null, ...) use Integer/Long instead
Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
32

There are a couple of things you can't do with a primitive type:

  • Have a null value
  • synchronize on them
  • Use them as type parameter for a generic class, and related to that:
  • Pass them to an API that works with Objects

Unless you need any of those, you should prefer primitive types, since they require less memory.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
18

Integer is a signed 32 bit integer type

  • Denoted as Int
  • Size = 32 bits (4byte)
  • Can hold integers of range -2,147,483,648 to 2,147,483,647
  • default value is 0

### **Long** is a signed 64 bit integer type
  • Denoted as Long
  • Size = 64 bits (8byte)
  • Can hold integers of range -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
  • default value is 0L

If your usage of a variable falls in the 32 bit range, use `Int`, else use `long`. Usually long is used for scientific computations and stuff like that need much accuracy. (eg. value of pi).

An example of choosing one over the other is YouTube's case. They first defined video view counter as an int which was overflowed when more than 2,147,483,647 views where received to a popular video. Since an Int counter cannot store any value more than than its range, YouTube changed the counter to a 64 bit variable and now can count up to 9,223,372,036,854,775,807 views. Understand your data and choose the type which fits, as 64 bit variable will take double the memory than a 32 bit variable.

All Іѕ Vаиітy
  • 24,861
  • 16
  • 87
  • 111
14

An int is a 32-bit integer; a long is a 64-bit integer. Which one to use depends on how large the numbers are that you expect to work with.

int and long are primitive types, while Integer and Long are objects. Primitive types are more efficient, but sometimes you need to use objects; for example, Java's collection classes can only work with objects, so if you need a list of integers you have to make it a List<Integer>, for example (you can't use int in a List directly).

Jesper
  • 202,709
  • 46
  • 318
  • 350
1

When it comes to using a very long number that may exceed 32 bits to represent, you may use long to make sure that you'll not have strange behavior.

From Java 5 you can use in-boxing and out-boxing features to make the use of int and Integer completely the same. It means that you can do :

int myInt = new Integer(11);
Integer myInt2 = myInt;

The in and out boxing allow you to switch between int and Integer without any additional conversion (same for Long,Double,Short too)

You may use int all the time, but Integer contains some helper methods that can help you to do some complex operations with integers (such as Integer.parseInt(String) )

Lucky
  • 16,787
  • 19
  • 117
  • 151
Houcem Berrayana
  • 3,052
  • 22
  • 40
0

a) object Class "Long" versus primitive type "long". (At least in Java)

b) There are different (even unclear) memory-sizes of the primitive types:

Java - all clear: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

  • byte, char .. 1B .. 8b
  • short int .. 2B .. 16b
  • int .. .. .. .. 4B .. 32b
  • long int .. 8B .. 64b

C .. just mess: https://en.wikipedia.org/wiki/C_data_types

  • short .. .. 16b
  • int .. .. .. 16b ... wtf?!?!
  • long .. .. 32b
  • long long .. 64b .. mess! :-/
Franta
  • 986
  • 10
  • 17
  • This doesn't answer the question, which is about the difference between Long and long etc. – GreenAsJade Jan 14 '19 at 09:10
  • No, it does not explicitly answer it, not explicitly saying the difference between a primitive types vs. data type objects, indeed @GreenAsJade, because the question here above does not state any specific language. So I provided a comparison - so everybody can check the difference. (from the hard data, as provided) – Franta Sep 03 '19 at 00:15
  • The question stated Java. – GreenAsJade Sep 04 '19 at 02:00