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 how to use Java basic data types and constant pools. Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn how to use Java basic data types and constant pools.
Size of base data type int 32 bits 4 bytes short 16-bit float 32-bit double 64-bit long 64-bit char 16-bit byte 8-bit boolean 1-bit automatic unpacking and boxing means that integer will be automatically converted to int for calculation when calculating values. When int is passed a reference of type integer, int values are wrapped as integer.// 8 bit byte bx = Byte.MAX_VALUE; byte bn = Byte.MIN_VALUE; //16 bit short sx = Short.MAX_VALUE; short sn = Short.MIN_VALUE; //32 bit int ix = Integer.MAX_VALUE; int in = Integer.MIN_VALUE; //64 bit long lx = Long.MAX_VALUE; long ln = Long.MIN_VALUE; //32 bit float fx = Float.MAX_VALUE; float fn = Float.MIN_VALUE;//64-bit double dx = Double.MAX_VALUE; double dn = Double.MIN_VALUE; //16-bit char cx = Character.MAX_VALUE; char cn = Character.MIN_VALUE; //1-bit boolean bt = Boolean.TRUE; boolean bf = Boolean.FALSE; AutoUnboxing and Boxing//The constant pool for primitive data types is between-128 and 127. //Basic data classes in this range can be automatically unboxed and compared directly. public static void main(String[] args) { //int automatic unpacking and packing only in the range-128 to 127, more than two integers == judgment will return false. Integer a1 = 128; Integer a2 = -128; Integer a3 = -128; Integer a4 = 128; System.out.println(a1 == a4); System.out.println(a2 == a3); Byte b1 = 127; Byte b2 = 127; Byte b3 = -128; Byte b4 = -128; //bytes are equal because the range is between-128 and 127 System.out.println(b1 == b2); System.out.println(b3 == b4); // Long c1 = 128L; Long c2 = 128L; Long c3 = -128L; Long c4 = -128L; System.out.println(c1 == c2); System.out.println(c3 == c4); //char has no negative values //char is also found to be automatically unboxed between 0 and 127 Character d1 = 128; Character d2 = 128; Character d3 = 127; Character d4 = 127; System.out.println(d1 == d2); System.out.println(d3 == d4); Integer i = 10; Byte b = 10; //Compare Byte and Integer. Two objects cannot be compared directly, error reported //System.out.println(i == b); System.out.println("i == b " + i.equals(b)); //The answer is false, because the comparison of wrapper classes first compares whether it is the same class, if not, it returns false directly. int ii = 128; short ss = 128; long ll = 128; char cc = 128; System.out.println("ii == bb " + (ii == ss)); System.out.println("ii == ll " + (ii == ll)); System.out.println("ii == cc " + (ii == cc)); //is true at this time, because the basic data type directly compares the values, the same value is OK.
Summary: Note the unboxing of basic data types and understanding of constant pools.
The principle of automatic unboxing and boxing above is actually related to the constant pool. 3.1 In the stack: public void(int a) { int i = 1; int j = 1; } The i in the method is stored in the local variable table of the virtual machine stack, i is a reference, j is also a reference, they all point to the integer value 1 in the local variable table. int a is a pass-by reference, so a will also have a local variable table. 3.2 There is a heap: class A{ int i = 1; A a = new A(); } i is a member variable of the class. Class instantiates objects exist in the heap, so member variables also exist in the heap, reference a stores the address of the object, reference i stores the value, this value 1 will also exist in the heap. It can be understood that the reference i points to this value 1. It can also be understood that i is 1. 3.3 The constant pool we say can also be called the object pool. For example, String a= new String("a").intern() will first find whether there is an "a" object in the constant pool. If there is, it will directly return the address of the "a" object in the constant pool, that is, let the reference a point to the memory address of the constant "a" object. public native String intern(); Integer is the same.
The following figure shows how an Integer type finds objects of the same value in a constant pool.
public static Integer valueOf(int i) { if (i >= IntegerCache.low && i = 127; } private IntegerCache() {} }
Therefore, the wrapper type of the basic data type can look for the object of the corresponding value in the constant pool, and if it cannot be found, it will automatically create the object of the value in the constant pool.
String types can do this with intern.
After JDK 1.7, the constant pool was put into heap space, which caused intern() function to function differently, and see the following code:
[java] view plain copy String s = new String("1"); s.intern(); String s2 = "1"; System.out.println(s == s2); String s3 = new String("1") + new String("1"); s3.intern(); String s4 = "11"; System.out.println(s3 == s4); The output is: [java] view plain copy JDK1.6 and the following: false false JDK1.7 and above: false true Here, I believe that we have a deeper understanding of "Java basic data types and how to use constant pools", may wish to actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!
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.