Location>code7788 >text

The relationship between int, Integer, long, Long, double, Double and BigInteger in Java

Popularity:110 ℃/2025-02-13 14:45:10

The relationship between int, Integer, long, Long, double, Double and BigInteger in Java

In Java, int, Integer, long, Long, double, Double and BigInteger are all types used to represent numeric values, but they have significant differences in their purpose, accuracy, range and performance. The following are their relationships and characteristics:

1. int (basic data type)

Type: int is one of the basic data types in Java.
Range: int is a 32-bit signed integer, ranging from -2^31 to 2^31 - 1 (i.e. -2147483648 to 2147483647).
Purpose: Suitable for general integer operations, high performance, but limited range.
Example:

int num = 12345;

2. Integer (Packaging Class)

Type: Integer is a package class of int and belongs to a package.
Range: Same as int, range is -231 to 231 - 1。
Purpose: Provides more methods and functions, such as parseInt, valueOf, etc. Can be used to store integers in collections such as ArrayList.
Features: Integer is an object and can call methods; while int is a basic type and cannot call methods.
Example:

Integer num = 12345; // Automatic boxing
 int primitiveNum = num; // Automatic unboxing

3. long (basic data type)

Type: long is one of the basic data types of Java.
Range: long is a 64-bit signed integer, range is from -263 to 263 - 1 (i.e. -9223372036854775808 to 9223372036854775807).
Purpose: Suitable for integer operations that require a larger range, such as handling large numbers or timestamps.
Example:

long num = 123456789012345L;

4. Long (packaging class)

Type: Long is a long package class, which belongs to the package.
Range: Same as long, range is -263 to 263 - 1。
Purpose: Provides more methods and functions, such as parseLong, valueOf, etc. Can be used to store long integers in a collection.
Features: Long is an object, which can call methods; while long is a basic type, which cannot call methods.
Example:

Long num = 123456789012345L; // Automatic boxing
 long primitiveNum = num; // Automatic unboxing

5. double (basic data type)

Type: double is one of the basic data types of Java.
Range: double is a 64-bit floating point number that can represent very large or very small values, but has limited accuracy (about 15-17-bit significant numbers).
Purpose: Suitable for scenarios where decimal operations are required, such as scientific calculations, financial calculations, etc.
Example:

double num = 123.456;

6. Double (Packaging Class)

Type: Double is a double packaging class and belongs to a package.
Range: Same as double, the range is a 64-bit floating point number.
Purpose: Provides more methods and functions, such as parseDouble, valueOf, etc. Can be used to store floating point numbers in a collection.
Features: Double is an object and can call methods; while double is a basic type and cannot call methods.
Example:

Double num = 123.456; // Automatic boxing
 double primitiveNum = num; // Automatic unboxing

7. BigInteger (class)

Type: BigInteger is a class that belongs to a package.
Range: BigInteger can represent integers of any size, without being limited by fixed digits.
Purpose: Suitable for scenarios where very large integers (out of long range), such as cryptography, large number operations, etc.
Features: BigInteger is an immutable object, and each operation returns a new BigInteger object.
Example:

import ;

BigInteger num = new BigInteger("123456789012345678901234567890");
BigInteger result = ((1));

Summarize

int and Integer: int is the basic type with high performance; Integer is a packaging class with rich features.
long and Long: long are basic types with high performance; long is packaging, rich in functions.
double and Double: double is the basic type with high performance; Double is a packaging class with rich features.
BigInteger: Used to handle very large integers, powerful but low performance.
In actual development, select the appropriate data type according to specific needs:
If the scope is within int or long, use the primitive type (int or long) first to improve performance.
If you need to use a collection or call a method, use a wrapper class (Integer, Long, Double).
If you need to deal with very large integers, use BigInteger.