• Networking
  • Programming
  • DBMS
  • Operating System
  • Internet
  • Hardware
  • Software

Tech Differences

Know the Technical Differences

Difference Between int and long

int-and-longThe data types int and long are among the two integer data types described in Java. In Java, the types int and long are signed, +ve and -ve values. Java does not support unsigned int and long types. The fundamental difference between an int and long data type is the width of each type. The datatype int has the width of 32-bit for the variable of int type.

On the other hand, the data type long has the width of 64-bit for the variable of long type. The comparison chart below shows some other differences between int and long.

Content: int Vs long

  1. Comparison Chart
  2. Definition
  3. Key Differences
  4. Conclusion

Comparison Chart

Basis for Comparisonint long
BasicThe datatype int is of 32-bits.The data type long is of 64-bits.
BytesIf counted in bytes, int is 4 bytes.If counted in bytes, long is 8 bytes.
RangeIn Java the range of type int is –2,147,483,648 to 2,147,483,647.In Java the range of type long is
–9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
KeywordKeyword used to declare a variable of int type is "int".Keyword used to declare a variable of long type is "long".
MemoryMemory required to store an int variable is less as compared to long.Memory required to store a long variable is larger as compared to int.

Definition of int type

A data type int is a most commonly used data type which holds an integer value in it. A value that an int variable can hold is not too short and not too long. Generally, the value of an int variable is used as a loop variable which controls the loop, or it is used as an index variable of an array.

The data type byte and short have shorter range when compared with data type int, but they can not replace int, even if the value of an int variable is of shorter range. The reason behind this is that when we use data type byte and short in an expression then while evaluating that expression the data type byte and short are promoted to int by the compiler. So, from this, it can be said that it is better to use int wherever an integer is needed.

Let’s have a look at the usage of int datatype.

byte a = 70;
byte b = 80;
int c = a* b; //no error.
byte d = a + 10;   //Compile time error.

As you can observe in above code the two, byte variables a and b containing value 70 and 80, which is valid byte-ranged value. But, the intermediate result of a*b; exceeds the limit of the byte. Hence, the compiler automatically promotes the byte, short and char variables to type int, if they are used in an expression.

Now, you can see that the result of a* b is assigned to an int variable which does not cause any error and compiles smoothly because the variables a and b are promoted to the type int and assignment of type int to the type int doesn’t cause the error.

In the last line of code, you can see an expression, where the value 10 is added to a byte variable ‘a’ its result still didn’t exceed the limit of the byte. But, the assignment of the intermediate result of expression “a+10” to the byte variable ‘d’, causes a compile time error because while expression evaluation byte variable ‘a’ is promoted to the type int. And assignment of type int to type byte is not allowed. Hence, it causes a compile time error.

Definition of long type

The data type long has the largest range and width as compared to byte, short, and int types. In Java, the type long is 64-bit signed integer type. The type long is used where the type int is not that large to hold the desired value. The range of long is –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 which is quite large, to hold the larger values like big whole numbers. We can understand the type long with an example below.

class weight {
public static void main(String args[]) {
int item;
int wt_per_item;
long grams;
long Box ;
item  = 10000000; // specify number items
wt_per_item= 10000; //specify weight of one item in kilos
grams= wt_item * 1000; // convert weight to gram
Box = item * grams; // compute weight of  box in grams
System.out.println("Number of item " + item );
System.out.println(" Weight per item " +wt_per_item +"kilos");
System.out.println(" Weight of box in gram" + Box +"kilos");
}
}
Number of item 10000000
Weight per item 10000 kilos
Weight of box in gram 100000000000000 kilos

Observe the output in above code; the result computed for the weight of entire box containing box containing 10000000 items of which per item weights 10000 kilos. The computed value for the weight of box in grams, can not be held by an int variable. Hence, sometimes it becomes necessary to use long type when large values are to be computed or hold.

Key Differences Between int and long

  1. The basic difference between the type int and long is of their width where int is  32 bit, and long is 64 bits.
  2. The types int and long when counted in bytes instead of bits the type int is 4 bytes and the type long is just twice if type int i.e. 8 bytes.
  3. In Java, the range of type int is from –2,147,483,648 to 2,147,483,647 whereas, the range of type long is from –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 which is very much greater than type int.
  4. The keyword used to the declare a variable of the type int is “int”. On the other hand, the keyword used to declare the variable of type long is “long”.
  5. Generally, the memory required to hold the value of type int is less as compared to long.

Conclusion

While writing code if there is a need for medium range value then you can use the type int but when the output produced by any expression will be larger, or the larger values are being used for computation then the long type variable must be used to hold the values.

Related Differences:

  1. Difference Between Stack and Queue
  2. Difference Between SOP and POS
  3. Difference Between if-else and switch
  4. Difference Between Type Casting and Type Conversion
  5. Difference Between & and &&

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Top 10 Differences

  • Difference Between OLTP and OLAP
  • Difference Between while and do-while Loop
  • Difference Between Guided and Unguided Media
  • Difference Between Preemptive and Non-Preemptive Scheduling in OS
  • Difference Between LAN, MAN and WAN
  • Difference Between if-else and switch
  • Difference Between dispose() and finalize() in C#
  • Difference Between for and while loop
  • Difference Between View and Materialized View
  • Difference Between Server-side Scripting and Client-side Scripting

Recent Addition

  • Difference Between Java and Python
  • Difference Between PHP and HTML
  • Difference Between GPS and GNSS 
  • Difference Between Virtualization and Containerization
  • Difference Between Storage and Memory

Categories

  • Artificial Intelligence
  • DBMS
  • Hardware
  • Internet
  • Networking
  • Operating System
  • Programming
  • Software

Copyright © 2025 · Tech Differences · Contact Us · About Us · Privacy