Collect a few error-prone or fun Java output problems, share to everyone, later in the programming learning in a little attention to OK.
1. Invisible spaces?
Will the output below be normal?
package Basic;public class Integertest {public static void main (string[] args) {System.out.println (INTEGER.PA Rseint ("1")); System.out.println (Integer.parseint ("? 2")); }}
parse: Copy the above code (don't knock yourself) and run it in your own environment to see if the output is the following error:
1
Exception in thread "main" Java.lang.NumberFormatException:For input string: "? 2"
at Java.lang.NumberFormatException.forInputString ( Unknown source)
at Java.lang.Integer.parseInt (Unknown source)
at Java.lang.Integer.parseInt (Unknown source)
at Basic. Integertest.main (integertest.java:7)
actually said the second sentence has a problem, on the surface completely can not see any problem is not!
In fact, the cause of the error here relates to a concept- 0 width Space , may have been contacted, but believe that more people have not even heard, what is 0 width space? It actually is a Unicode character, is a space, the key is that it has no width, so we are generally invisible to the naked eye. But you can see the under Vim, the second statement above 2 has a 0-width space, put into vim after opening you will find the following statement:
System.out.println (Integer.parseint ("<feff>2"));
unicode specification, each file is preceded by a character that represents the encoding sequence, which is named "0-width non-newline space" (Zerowidthno-breakspace), with feff represents. This happens to be two bytes, and FF is 1 larger than FE. So the following statement will output 65279, just feff .
System.out.println((int)"2".charAt(0));
2. class static member initialization
Can the following program compile and pass? If passed, say the result and explain, cannot compile, say wrong reason.
class a{public static int X; static {X = B.y + 1;}} public class b{public static int Y = a.x + 1; static {} public static void Main (string[] args) {System.out.println ("X =" +a.x+ ", Y =" +b.y "); }}
Parse: This program can run correctly, the class running process is as follows:
First loading the main class B, initializing the static member Y, discovering the need for the information of Class A, so load Class A, initialize static member X, also use class B information, because at this time the Class B is not successfully loaded so here is the default value of 0, so that the Class A is the X 1, and then return to Class B, Y is 2
3. The actual process of packing and unpacking
About automatic boxing, I believe most people understand what is the matter, but really fully understand it?
Let's look at the following code:
Short s1 = 1;Short s2 = s1;System.out.println(s1 == s2);
Everyone knows of course printing true. Now add a sentence to try:
Short s1 = 1;Short s2 = s1;s1++;System.out.println(s1 == s2);
or is it true? No, this time the output is false. Why? Don't S1 and S2 refer to the same object? With these questions you are not very clear about the process of automatic boxing unpacking, in fact the above code can be translated into the following code (the actual execution process, to master):
Short s1 = new Short((short)1);Short s2 = s1;short tempS1 = s1.shortValue();tempS1++;s1 = new Short(tempS1);System.out.println(s1 == s2);
Oh, so that's clear, so we'll be careful when it comes to automatic boxing.
4. Your self-righteous abnormality
First two lines of code:
NullTest myNullTest = null;System.out.println(myNullTest.getInt());
believe that many people see this code, will be self-righteous said: nullpointerexception . Is it true? You haven't seen how the Nulltest class is defined. Now look at the definition of this class:
class NullTest { public static int getInt() { return 1; }}
FoundgetInt()The method body does not use any class variables and class methods, so this will output 1 normally.
Remember:The use of class variables and class methods depends only on the type of reference. Even if the reference is null, it can still be called. From a good practice point of view, it is advisable to useNullTest.getInt()To replacemyNullTest.getInt(), but who is not guaranteed not to run into such code, so be careful.
5. How to change the length parameter and array?
The variable-length parameter feature brings a powerful concept that can help developers simplify code. But what is behind the variable-length parameters? Basically, is an array.
public void calc(int... myInts) {} calc(1, 2, 3);
The compiler translates the preceding code into something like this:
int[] ints = {1, 2, 3};calc(ints);
But here are two points to note:
- Beware of air conditioning with a statement, which is equivalent to passing a null as a parameter.
Calc ();
Equivalent to
int[] ints = null;
Calc (ints);
- Of course, the following code causes a compilation error because the two statements are equivalent:
public void M1 (int[] myints) {...}
public void M1 (int ... myints) {...}
Resources
Java five-way output error-prone analysis (come in challenge)