In programming, it is required to store data. The data is stored in memory. Memory locations that can store data is called variables. Each memory location can store a specific type of data. Memory size for each data type is different. The int data type is used to store numeric values without decimal points. The float and double data types are used to store numerical values with decimal points. The char data type is used to store a single character value. Likewise, each data type can store a particular value depending on the type. In programming languages such as python, it is not necessary to declare the variable type. If the programmer is written as a = 3, Python automatically identifies that it is an integer variable. In programming languages such as Java, the programmer should specify the data type. If the variable is declared as an int, then he cannot assign a character value to it. The int and long are two data types. This article discusses the difference between int and long. The key difference between int and long is that int is 32 bits in width while long is 64 bits in width.
CONTENTS
1. Overview and Key Difference
2. What is int
3. What is long
4. Similarities Between int and long
5. Side by Side Comparison – int vs long in Tabular Form
6. Summary
What is int?
A data type int is a most commonly used data type that holds an integer value in it. It is predefined data type supported by many programming languages such as Java. To declare an integer variable, the keyword ‘int’ is used. Therefore, it cannot be used as an identifier such as a variable name or a method name. Refer the below example program.
According to the above program, the variable a is an int and has the value 10. The variable b is an int and has the value 20. The sum of a and b are calculated and assigned to the variable sum. It is also an integer. In the for loop, ‘i’ is the counter variable. It is an integer. It will iterate 5 times. When the ‘i’ value becomes 6, the condition becomes false and get out of the loop.
Typecasting can be performed on data types. It is the process of converting a one data type to another data type. When assigning a smaller data type to a larger data type, there is no casting required. The widening happens in a byte, short, int, long, float, double. When assigning a larger data type to a small data type, it is necessary to do the casting.
In above program, the num1 variable has value 10. The variable num2 has value 20. The total is an int. As int is a larger data type than a byte, it is necessary to typecast into byte in order to store into a byte variable. If there is no typecasting, it means the integer value is assigned to the byte variable so there will be a compile-time error.
What is long?
The long is a predefined data type provided by languages such as Java. In Java, the data range is from -9,223,372,036,854,775,808(-2^63) to 9,223,372,036,854,775,807 (inclusive) (2^63-1). It is 64 bits in width. The number of bytes for a long is 8 bytes. One byte is equivalent to 8 bits. Refer the below program.
According to the above program, width and length are long variables. The resulting value is assigned to a long variable. The long is the largest data type. Other data types are smaller than long. So other data types can be assigned to long without typecasting. When assigning a long value to int, it is required to typecast.
What is the Similarity Between int and long?
- Both int and long are predefined data types supported by programming languages such as Java.
What is the Difference Between int and long?
int vs long |
|
The int data type is a 32-bit signed two’s complement integer. | The long data type is a 64-bit signed two’s complement integer. |
Number of Bytes | |
The int is 4 bytes long. | The long is 8 bytes long. |
Minimum Value | |
Minimum value of int is – 2,147,483,648 (-2^31) in Java | Minimum value of long is -9,223,372,036,854,775,808(-2^63) in Java |
Maximum Value | |
Maximum value of int is 2,147,483,647 (inclusive) (2^31-1) in Java | Maximum value of long is 9,223,372,036,854,775,807 (inclusive) (2^63-1) in Java |
Default Value | |
The default value of int is 0. | The default value of long is 0L. |
Keyword | |
The keyword ‘int’ is used to declare an integer. | The keyword ‘long’ is used to declare a long. |
Required Memory | |
The int requires less memory than long. | The long requires more memory than int. |
Summary – int vs long
In programming, it is necessary to store data. Those data are stored in memory locations. Those memory locations are called variables. Each variable has a specific type of data to be stored. There are data types such as int, char, double and float etc. This article discussed the difference between two data types that are int and long. The int data type is a 32-bit signed two’s complement integer. The long data type is a 64-bit signed two’s complement integer. The long is a larger data type than int. The difference between int and long is that int is 32 bits in width while long is 64 bits in width.
Reference:
1.Point, Tutorials. “Java Basic Datatypes.” Tutorials Point, 8 Jan. 2018.Available here
2.“Type conversion in Java with Examples.” GeeksforGeeks, 5 Jan. 2017. Available here