Long vs Integer, long vs int, what to use and when?

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?

long long vs int multiplication

Given the following snippet: #include <stdio.h> typedef signed long long int64; typedef signed int int32; typedef signed char int8; int main() { printf(%i/n, sizeof(int8)); printf(%i/n, size

long double vs long int

I’m doing a program that calculates the probability of lotteries. Specification is choose 5 numbers out of 47 and 1 out of 27 So I did the following: #include <iostream> long int choose(unsigned

Type long Vs Type long int

What would following typedef mean, and why would it be required typedef unsigned long int ULONG; how different is it from typedef unsigned long ULONG; For gcc sizeof int = 4, sizeof long = 8, and s

integer constant long long int?

long long int It seems to be valid in C. but is this valid in c++?

C# Method Resolution, long vs int

class foo { public void bar(int i) { … }; public void bar(long i) { … }; } foo.bar(10); I would expect this code to give me some error, or at least an warning, but not so… What version of bar()

Iteration speed of int vs long

I have the following two programs: long startTime = System.currentTimeMillis(); for (int i = 0; i < N; i++); long endTime = System.currentTimeMillis(); System.out.println(Elapsed time: + (endTim

unsigned long long int constant value vs. multiplicative value

I tried the below, and it seems that ‘test’ results in wrong value. 2500*2500*2500 == 15625000000, why the below operations result in different outcome? unsigned long long int test = 2500*2500*2500; f

What is the difference between long long and long int

I know the difference between long and int But What is the difference between long long and long int

polling vs long polling

I got onto these examples showing polling vs long-polling in javascript, however I do not understand how they differ from one another. Especially regarding the long polling example, how does it keep i

when and where to use int vs Integer

I came across one class which has Integer variables even to catch length or size in for loop. Do this approach is good in practice or we have use primitive data type int . Integer size = something.get


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

The long is 64 bits long. The int is 32 bits, and so can only hold numbers up to +-2 billion (-2^31 to +2^31-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.

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.

  • 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

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

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 strenge 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 fot 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) )