About Java Ten Things You Did not Know

So, you from the beginning been using Java? Remember those days was called "Oak" of, OO is still a hot topic, C ++ believe that there is no chance Java, Applet or something?
I bet you do not know at least half of the following content.

1. no abnormality is checked

That's right! JVM does not know any such thing, only the Java language to know.
Do you want to prove JVM does not know such a thing? Try following code:
public class {the Test

// No throws clause here
public static void main(String[] args) {
    doThrow(new SQLException());
}

static void doThrow(Exception e) {
    Test.<RuntimeException> doThrow0(e);
}

@SuppressWarnings("unchecked")
static <E extends Exception> 
void doThrow0(Exception e) throws E {
    throw (E) e;
}

}

2. You can make the method overloading only differ in return type

That does not compile, right?
{the Test class
Object X () {return "ABC";}
String X () {return "123";}
}
pair. Java language does not allow two methods "to cover equivalent" in the same class, regardless of their throws clause or how returntype may differ.
Wow, yeah, this makes sense. In actuality, when it almost happens when you are writing the following:
abstract class the Parent {
abstract the X-T ();
}

class Child extends Parent {
@Override
String x() { return “abc”; }
}
在中检出生成的字节码Child:
// Method descriptor #15 ()Ljava/lang/String;
// Stack: 1, Locals: 1
java.lang.String x();
0 ldc <String “abc”> [16]
2 areturn
Line numbers:
[pc: 0, line: 7]
Local variable table:
[pc: 0, pc: 3] local: this index: 0 type: Child

Method, descriptor # 18 is // () Ljava / lang / Object;
// Stack:. 1, the Locals:. 1
Bridge Synthetic java.lang.Object X ();
0 aload_0 [the this]
. 1 invokevirtual Child.x (): the java.lang .string [. 19]
. 4 areturn
Line Numbers:
[PC: 0, Line:. 1]
Thus, T is really just Object bytecode. Well understood.
Bridge synthesis method is actually generated by the compiler, because Parent.x () may be desirable in some typeObject signed return call position. If no such bridging method, you can not add a generic binary compatible way. Therefore, changing the JVM to make it painful to have this feature smaller (which also makes covariate effects become overloaded ......) clever, right?
Whether you are familiar with the language and details of internal knowledge?

3. All of these are two-dimensional array!

class the Test {
int [] [] A () {return new new int [0] [];}
int [] B () [] {return new new int [0] [];}
int C () [] [] { new new int return [0] [];}
}
Yes, it is true. Even if your psychological analyzer may not understand the above methods immediately return type, they are also the same! Similar to the following code snippet:
class the Test {
int [] [] A = {{}};
int [] B [] = {{}};
int C [] [] = {{}};
}
you think this is crazy right?
@Target (ElementType.TYPE_USE)
@interface Crazy {}

class Test {
@Crazy int[][] a1 = {{}};
int @Crazy [][] a2 = {{}};
int[] @Crazy [] a3 = {{}};

@Crazy int[] b1[]  = {{}};
int @Crazy [] b2[] = {{}};
int[] b3 @Crazy [] = {{}};

@Crazy int c1[][]  = {{}};
int c2 @Crazy [][] = {{}};
int c3[] @Crazy [] = {{}};

}
Enter a comment. Its power beyond mere mystery of equipment
other words:
When I did this the last time I submitted four weeks before the holiday

I will find you any of these practical exercises a use case.

4. You do not have the conditional expression

So, you think you know it all when you use conditional expressions? I tell you, you do not. Most of you will think of the following two fragments are equivalent:
Object O1 = to true new new Integer (1):? New new Double (2.0);
... the same?
Object o2;

IF (to true)
O2 = new new Integer (. 1);
the else
O2 = new new Double (2.0);
no. Let's conduct a quick test
System.out.println (O1);
System.out.println (o2);
the program will print:
1.0
1
is conditional operators will realize digital promotion, if "needed" , for a very, very strong quotes' needs. " Because you want the program throws a NullPointerException?
I = new new Integer Integer (. 1);
IF (i.equals (. 1))
I = null;
Double Double new new D = (2.0);
Object O = I to true:? D; // a NullPointerException!
System.out.println ( O);

5. You did not get compound assignment operators

Weird enough yet? Let us consider the following two codes:
a
2 = J + i;
i = i + J;
intuitively, they should be equivalent, right? But guess how. they are not! JLS specify:
the form E1 op = E2 complex assignment expression is equivalent to E1 = (T) ((( E1) op (E2))), where T is the E1 type, only E1 is evaluated only once.
It is so beautiful, I would like to quote Peter Lawrey stack overflow answer to this question:
a good example of this conversion is to use the * = or / =
bytes = 10 b;
b * = 5.7;
System.out.println (b); // print 57
or
bytes = 100 B;
B / = 2.5;
System.out.println (b); // print 40
or
char CH = '0';
CH * = 1.1;
the System.out. println (ch); // print '4'
or
char CH = 'A';
CH * = 1.5;
System.out.println (CH); // print 'a'
now, how this incredible? I convert the characters / multiplication in the my application. Because, you know ...

6. random integer

Now, this is more like a puzzle. Read the solution yet. See if you can find it yourself. When I run the following program:
1
2
. 3 for (int I = 0; I <10; I ++) {
System.out.println ((Integer) I);
}
... then "sometimes" I get the following output:
92
221
45
48
236
183 is
39
193
33 is
84

7. Go

This is one of my favorites. Java has GOTO! Enter ...
int goto = 1;
this will result in:
Test.java:44: error: expected
int goto = 1;
^
This is because a goto keyword is not used, just in case ...
but this is not exciting section. Exciting is that you can realize with vines break, continue and the marker block:
skip forward
label: {
// do Stuff
IF (Check) BREAK label;
// do More Stuff
}
in the bytecode:
2 iload_1 [check]
. 3 // jump forward ifeq absolute expression 6
6 ...
bounce back
label: do {
// do Stuff
IF (check) Continue label;
// do More Stuff
BREAK label;
} the while (to true);
in the bytecode :
2 iload_1 [check]
. 3 ifeq absolute expression 9
. 6 GOTO 2 // skip back
9 ...

8. Java has a type alias

In other languages (such as Ceylon), we can very easily define type Alias:
interface People => Set;
People so constructed type can be used interchangeably with the Set the following items:
? People p1 = null;
? Set P2 = p1;
? People p3 = p2;
in Java, we can not at the top level alias defined type. But we can do it within the scope of the class or method. Suppose we name the Integer, Long, etc. are not satisfied, we want to use a shorter name: I and L. Simple:
class the Test {
void X (the I I, L L) {
System.out.println (
i.intValue () + "," +
l.longValue ()
);
}
}
In the above procedure, Integer "alias" I range for the Test class, and Long "alias" L for the range of X () method. Then we can call the above method as above:
new new the Test () the X-(1, 2L);.
Of course, this technique should not be taken seriously. In this case, Integer and Long are the final type, which means typeI and L are valid alias (almost assignment is compatible only one way). If we use a non-final type (eg Object), then we really will use ordinary generic.
These stupid tricks enough. Now another truly remarkable thing!

9. Some type relationship is uncertain!

Well, now it will become very fashionable, and therefore can have a coffee concentrate. Consider two of the type:
// A Helper of the type Also the Just by You could use List.
Interface Type {}

class C implements Type<Type<? super C>> {}
class D

implements Type<Type<? super D<D

>>> {}
Now, do typeC and even D What does it mean?
Some of them are recursive, recursive similar (but slightly different) ways java.lang.Enum. Consider:
public abstract class the Enum <E the extends the Enum> {...}
Using the above specifications, the actual implementation is simply syntactic sugar enum:
// This
enum {} MyEnum

Sugar for Really Is is the Just for // the this
class MyEnum the extends the Enum {...}
With that in mind, let us return to two kinds of type. The following can compile it?
{the Test class
the Type new new C = C () <Super C?>;
the Type new new D = D () <Super D?>;
}
C is Type <sub type it? superC>?
Step 0) C <?: Type <? superC>
Step 1) Type <Type <? super C >> <?: type (inherited)
Step 2) C (check wildcards? superC)
step. . . (Forever loop)
then:
D is a Type <sub-type it? superD>?
Step 0) D <?: Type <? superC>
Step 1) Type <Type <? superD <D >>> <?: type < ? superD>
Step 2) D <?: Type <? superD <D >>
Step 3) Type <type <? superC >> <?: type <? superC>
Step 4) D <D> <?: Type <? superD <D >>
step. . . (Never extended)
try to compile the above code in Eclipse, it will collapse!

10. Type intersection

Java has a very unique feature called type intersection. You can declare a (common) type, it's actually two types of intersection. For example:
class Test <T & the Cloneable the extends Serializable> {
}
generic type parameter T, you bind instances of Test classes must implement two Serializable and Cloneable. For example, String is not the boundaries of the possible, but a Date:
// Does not the compile
the Test S = null;

Compiles //
the Test d = null;
the Java 8 has been reused in this feature, you can now type in which converted to a temporary type of intersection. What's the use? Almost no, but if you want to force the lambda expression for this type is no choice. Suppose you approach by this crazy type constraints:
<T & Runnable the extends Serializable> void Execute (T T) {}
You Runnable Serializable also wish to do so, in case you want to perform it elsewhere and transmitted via wires it. Lambda and serializing a bit odd.
Lambda can serialize:
If the target type lambda expressions and capture parameters can be serialized, it can be serialized
However, even if this is true, they will not automatically Serializable marker interface. To force them to use this type, you must be cast. But when you only vote for ...... Serializable
the Execute ((Serializable) (() -> {}));
... then, lambda will no longer run.
Ah ...
so ...
to cast it into two types:
the Execute ((& Runnable Serializable) (() -> {}));
and finally, the development of so many years I have summarized the data and a set of interview questions to learn Java, if you want to upgrade their technology in the above, you can follow me, I can add about the Java learning exchange group: 970 917 008, have time to remember to help me make the point under forward with more people see Oh.Here Insert Picture Description

Published 76 original articles · won praise 11 · views 10000 +

Guess you like

Origin blog.csdn.net/zhaozihao594/article/details/104234847
Recommended