Preface
In Java,long
andLong
The main difference between them is that they belong to different data types: basic data types and packaging data types respectively.
-
Data Type:
-
long
is a primitive data type in Java, used to represent large ranges of integer values. -
Long
yeslong
The wrapper class is an object that contains along
Field of type.
-
-
default value:
-
long
The default value is0
。 -
Long
The default value isnull
,becauseLong
is an object.
-
-
Memory usage:
-
long
Types occupy 8 bytes in memory. -
Long
Because the object is a class, it containslong
In addition to the 8 bytes of the value, there are object headers and possible padding, so it will take up more memory.
-
-
Use scenarios:
-
long
Applicable to where value types are needed, such as mathematical operations. -
Long
Applicable to where objects are needed, such as in collection classes (such asList<Long>
、Set<Long>
) or use it if needednull
Values represent certain special meanings.
-
-
Method support:
-
long
As a primitive data type, method calls are not supported. -
Long
As a class, it has a series of methods, such asvalueOf(String s)
、parseLong(String s)
、equals(Object obj)
andhashCode()
wait.
-
-
Packing and unboxing:
-
long
Types cannot be used directly where objects are needed, but Java provides automatic boxing and unboxing functions, allowinglong
andLong
Automatic conversion between .
Sample code description:
-
long primitiveLong = 10; // Basic data typesLong objectLong = new Long(10); // Package data type// Automatic boxingLong autoBoxed = 10L; // Automatic unboxinglong unboxed = objectLong;
In actual programming, choose to uselong
stillLong
Depend on the specific application scenario and requirements. When possible, using basic data types is often more efficient because they avoid the overhead of boxing and unboxing and consume less memory. However, when using objects is required, or when using special methods of packaging classes, useLong
It is suitable.
What is the packaging data type
In Java, Wrapper Classes is a special set of classes that provide corresponding object versions for each primitive data type in Java. These packaging classes belong to the standard library (package) of Java and provide the following main functions:
- Allows the original data type to be converted to an object: Because Java is an object-oriented language, sometimes it is necessary to process the basic data type as an object, such as storing the value of the basic data type in a collection class. The wrapper class makes this conversion possible.
-
Provide useful methods: The wrapper class provides many useful methods to manipulate the corresponding basic data type. For example,
Integer
The class provides the conversion of strings to integersparseInt
Method, andCharacter
Class provides a method to determine whether a character is a numberisDigit
。 -
Supports automatic packing and unboxing: Starting with Java 5, Java introduced the concepts of autoboxing and unboxing, which allows automatic conversion between basic data types and their wrapping classes.
The following is the wrapper class corresponding to each basic data type in Java:
-
byte
->Byte
-
short
->Short
-
int
->Integer
-
long
->Long
-
float
->Float
-
double
->Double
-
char
->Character
-
boolean
->Boolean
Here are some basic usage examples of packaging classes:
// Create a wrapper instanceInteger intObject = new Integer(10); Double doubleObject = new Double(20.5); // Methods of using packagingint intValue = ("123"); boolean isDigit = ('5'); // Automatic boxing: convert basic type to packaging typeInteger autoBoxedInt = 100; // Automatic unboxing: Convert the packaging type to the basic typeint unboxedInt = autoBoxedInt; // Use wrapper class as collection elementList<Integer> integerList = new ArrayList<>(); (1); // Automatic boxing
Wrapping classes are very important in Java programming, especially when you need to interact with basic data types with objects. However, they also bring some performance overhead, as packing and unboxing operations take time and packaging objects take up more memory than basic data types. Therefore, in performance-sensitive occasions, it is generally recommended to use basic data types.
Detailed code explanation
The following will explain the difference between basic data types and wrapping data types through a simple Java code.
public class DataTypeDemo { public static void main(String[] args) { // Basic data types int primitiveInt = 10; // Package data type Integer wrapperInt = new Integer(10); // Print value ("Value of basic data type int: " + primitiveInt); ("Value of wrapping data type Integer: " + wrapperInt); // Basic data types cannot directly call methods // The following code will cause a compilation error // (); // Methods can be called when wrapping data types String wrapperIntString = (); ("Convert the wrapper data type Integer to a string: " + wrapperIntString); // Basic data types cannot be used for generics // The following code will cause a compilation error // List<int> intList = new ArrayList<>(); // Package data types can be used for generics List<Integer> integerList = new ArrayList<>(); (20); // Automatic boxing ("Packaging the value of data type Integer in the collection: " + (0)); // Default value of basic data type int defaultPrimitiveInt; // Print the default value (it won't be printed here because the variable is not initialized) // ("Default value of uninitialized basic data type int: " + defaultPrimitiveInt); // Default value of packaging data type Integer defaultWrapperInt = null; ("Default value of wrapping data type Integer: " + defaultWrapperInt); } }
In this code example, we can see the following differences:
Basic data types(like
int
) directly store the value, andPackage data type(likeInteger
) is an object that can store values and other information.Basic data typesCan't call methods directly, butPackage data typeVarious methods can be called, such as
toString()
。Basic data typesCannot be used for generics (for example, cannot be created
List<int>
),andPackage data typeCan be used for generics (for example, you can createList<Integer>
)。Basic data typesThere are default values (such as
int
The default value is0
),andPackage data typeThe default value isnull
。
Let's continue to dive into what are the autoboxing and unboxing functions
Let's use the code examples mentioned earlier to explain the concepts of autoboxing and unboxing.
AutoboxingIt refers to the process in which Java automatically converts basic data types to the corresponding wrapping data types. This is done automatically in Java 5 and later.
In the code example, the following line of code shows automatic boxing:
List<Integer> integerList = new ArrayList<>(); (20); // Automatic boxing
Here, we have a basic data typeint
The value of20
. When we try to add it toList<Integer>
(This is a generic collection that can only store wrapper types.Integer
) When Java will automaticallyint
Value20
Convert toInteger
Object, this is automatic boxing.
UnboxingIt is the opposite process of automatic boxing, that is, Java automatically converts the wrapping data type to the corresponding basic data type.
In the code example, the following line of code shows unboxing:
("Packaging the value of data type Integer in the collection: " + (0));
Here, when we go fromList<Integer>
When getting elements in the collection, the returned oneInteger
Object. However, when we concatenate it with a string, Java will automaticallyInteger
Object conversion back to basic data typeint
, this allows string connection, which is unboxing.
To summarize:
- Automatic boxing: Java automatically converts basic data types to wrapping data types.
- Unboxing: Java automatically converts wrapping data types to basic data types.
Summarize
This is all about this article about the difference between long and long in java. For more related content on the difference between long and long in java, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!