PRIMITIVETYPES principle interpretation
PRIMITIVETYPES principle interpretation of Guava primary analysis
Introduction:
Guava is a set of excellent foundation libraries provided by Google for Java developers. The PrimitiveTypes module provides us with a simple way to operate the original type of Java.This article will introduce the principles and usage methods of the PRIMITIVETYPES module of Guava, and comes with a Java code example.
1. Profile of PrimitiveTypes module
The PrimitiveTypes module is part of the Guava library, which aims to provide operating support for the original type of Java.Java original types include int, long, double, etc.This module can help developers more conveniently handle the conversion and comparison operations of the original type.
2. Usage of the PrimitiveTypes module
The PrimitiveTypes module provides a series of static methods to operate the original type of Java.The following is a detailed introduction and example of some commonly used methods:
- isWrapperType(Class<T> type)
This method is used to determine whether the given class is a packaging class of the original type.The return result is BOOLEAN type.
Example code:
boolean isWrapper = PrimitiveTypes.isWrapperType(Integer.class); // 返回true
- unwrap(Class<T> type)
If the given class is a packaging class of the original type, this method returns the corresponding original type.Otherwise, the given class will be returned.
Example code:
Class<?> unwrappedType = PrimitiveTypes.unwrap(Integer.class); // 返回int.class
- wrap(Class<T> type)
If the given class is the original type, this method returns the corresponding packaging class.Otherwise, the given class will be returned.
Example code:
Class<?> wrappedType = PrimitiveTypes.wrap(int.class); // 返回Integer.class
- cast(Class<S> sourceType, Class<T> targetType)
This method is used to convert the given original type into the target original type.If the conversion is not feasible, the classcastexception will be thrown.
Example code:
int intValue = PrimitiveTypes.cast(Long.class, int.class); // 抛出ClassCastException
- wrapArray(Class<T> componentType)
This method returns the array class corresponding to the original type of original type.
Example code:
Class<?> arrayType = PrimitiveTypes.wrapArray(int.class); // 返回Integer[].class
- unwrapArray(Class<T> componentType)
This method returns an array class corresponding to the original type corresponding to the given packaging type.
Example code:
Class<?> arrayType = PrimitiveTypes.unwrapArray(Integer.class); // 返回int[].class
3. Summary
Guava's PrimitiveTypes module provides a convenient method set to operate the original type of Java and its packaging type.By using PrimitiveTypes, developers can more easily perform type conversion, comparison and other operations.This article introduces the common methods and examples of the module, hoping to help readers better understand and apply the PrimitiveTypes module of Guava.
references:
-Gava official document: https://github.com/google/guava/wiki/primitiveSexplained