• Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Today I’ll talk about some interesting uses of numbers in Java.

A readable way of writing numbers

For integers, you know you can use int, long, etc., but if it’s a big number, like tens of millions or hundreds of millions, how do you write it so you can read the variable quickly and clearly?

 // Can you quickly read the following tens of millions? Or hundreds of millions?
 int int1 = 21341763;
 long long1 = 457813974947972L;
Copy the code

For those of you who have studied accounting, you know a lot of numbers, and you can improve the readability of numbers by using the three-digit method, like 21,34,763.

In fact, as of JDK7, Java supports underlining numeric literal variables to increase program readability. The Java compiler automatically removes these underlining.

 // For example, we use the four-digit grading system
 int int2 = 2134 _1763;
 long long2 = 457_8139_7494_7972L;
Copy the code

This is also supported for binary and floating-point/double numbers

 // Float
 float ff = 3 _33. 1 f;
 double dd = 3 _3113. 1345 _63d;
 // Binary notation is useful for converting to hexadecimal etc
 int bin = 0b1101_1001;
Copy the code

Integer/Long/Short cache pool problem

Take a look at the following example:

 // What is the result below?
 Integer int11 = 10;
 Integer int12 = 10;
 System.out.println("Integer 10 == 10? " + (int11 == int12));
 ​
 Integer int21 = new Integer(10);
 Integer int22 = new Integer(10);
 System.out.println("new Integer(10) ? " + (int21 == int22));
 ​
 Integer int31 = 128;
 Integer int32 = 128;
 System.out.println("Integer 128==128? " + (int31 == int32));
 ​
 ​
 /** 结果:
 Integer 10 == 10? true
 new Integer(10) ? false
 Integer 128==128? true
 **/
Copy the code

If you are not surprised by the third result above, either you do not know the whiteness of the Integer buffer pool, or you do not understand the whiteness of the Integer buffer pool thoroughly (● – ●)

😏 intelligent students already know, one of the Integer IntegerCache, the default is to cache – 128 ~ 127 Integer, the above results, is because I changed the Java lang. Integer. IntegerCache. High = < size > the value of the upper limit.

But for the second surprise student, you might want to take a look at the source code for yourself. The Integer cache pool is only for the boxing process, so the new Integer in the second example does not refer to the boxing type, the naturally generated new object, and the result is false.

 public static Integer valueOf(int i) {
    if (i >= IntegerCache.low && i <= IntegerCache.high)
        return IntegerCache.cache[i + (-IntegerCache.low)];
    return new Integer(i);
 }
Copy the code

There are also cache pools for Long and Short, and their cache pools are also -128 to 127, but the upper and lower limits cannot be changed by configuration.

A casual mistake

Integer is an object, so it may be null. If (Integer == 1) is used directly, it will cause a null pointer exception, which is easy to ignore for novice programmers.

That’s all for today’s sharing. I hope it will help you.

I’m Pandas. I’m dedicated to sharing Java programming techniques for Pandas. If you find this post useful, don’t forget to like it and follow it!