In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
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
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.