Get the App
SLTechnology News&Howtos  ›  Development  › 

Analysis of Java basic types, data types, Packaging classes and automatic unpacking examples

Shulou Source: shulou.com Published: 2022-06-02 12:56:54 09月26日 Update

This article mainly explains "Java basic types, data types, packaging classes and example analysis of automatic unpacking". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn the "Java basic types of data types, packaging classes and automatic unpacking examples analysis" bar!

Preface

We know that the basic data types include byte, short, int, long, float, double, char, boolean, and the corresponding wrapper classes are Byte, Short, Integer, Long, Float, Double, Character, Boolean.

So why do you need wrapper classes?

JAVA is an object-oriented language, the parameters in many classes and methods need to use objects, but the basic data types are not object-oriented, which causes a lot of inconvenience.

For example, List = new ArrayList ();, it cannot be compiled through

To solve this problem, we introduced a wrapper class, which, as the name implies, "wraps" the basic type with the nature of an object, including the ability to add properties and methods under the java.lang package.

Unpacking and packing

Now that you have basic data types and wrapper classes, there must be conversions between them, such as:

Public static void main (String [] args) {Integer a = 0; for (int I = 0; I

< 100; i++){ a += i; }} 将基本数据类型转为包装类的过程叫"装箱"; 将包装类转为基本数据类型的过程叫"拆箱"; 自动拆箱与自动装箱 Java为了简便拆箱与装箱的操作,提供了自动拆装箱的功能,这极大地方便了程序员们。那么到底是如何实现的呢? 上面的例子就是一个自动拆箱与装箱的过程,通过反编译工具我们得到, public static void main(String[] args) { Integer a = Integer.valueOf(0); for (int i = 0; i < 100; i++) { a = Integer.valueOf(a.intValue() + i); }} 我们不难发现,主要调用了两个方法,Integer.intValue() 和 Integer.valueOf( int i) 方法 查看Integer源码,我们找到了对应代码: /** * Returns the value of this {@code Integer} as an * {@code int}. */public int intValue() { return value;} /** * Returns an {@code Integer} instance representing the specified * {@code int} value. If a new {@code Integer} instance is not * required, this method should generally be used in preference to * the constructor {@link #Integer(int)}, as this method is likely * to yield significantly better space and time performance by * caching frequently requested values. * * This method will always cache values in the range -128 to 127, * inclusive, and may cache other values outside of this range. * * @param i an {@code int} value. * @return an {@code Integer} instance representing {@code i}. * @since 1.5 */public static Integer valueOf(int i) { if (i >

= IntegerCache.low & & I = IntegerCache.low & & I

Tags: Type wrapper method data cache boxing procedure disassembly object scope configuration that is analysis between code content actual source code program learning Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS MariaDB Apple Huawei vpn