Instructor Zhang Xiaoxiang's jdk1.5 High-Tech Notes

Source: Internet
Author: User

Instructor Zhang Xiaoxiang's jdk1.5 High-Tech Notes

 

1. Static Import static java. Lang. Math .*;

2. variable parameters such as: int add (int x, int... ARGs)

3. For Loop enhancement function for (Type X: array) {system. Out. println (x + "")}

4. Automatically boxed integer I = 10; (ensure the same object-128 ~ 127) Automatic unpacking integer I = 10; int x = I;

 

PackageJava high-tech;

Public classTestinteger {

Public static voidMain (string [] ARGs ){

IntX1 = 1;

IntX2 = 1;

Integer X3 = 1;

Integer X4 = 1;

Integer X5 = 128;

Integer X6 = 128;

IntX7 = integer.Valueof(128 );

IntX8 = integer.Valueof(128 );

System.Out. Println (x1 = x2 );

// System. Out. println (x2 = X3 );

System.Out. Println (X3 = X4 );

System.Out. Println (X5 = X6 );

System.Out. Println (X7 = X8 );

}

}

 

5. Enumeration class

Objects in an enumeration class are subclass objects of this enumeration class. The enumeration class can have constructor methods, but must be private.

 

PackageJava high-tech;

/**

* You can use enumeration to implement a singleton. One enumeration contains only one object.

*/

Public classTestenum {

 

Public EnumWeek

{

Sun(1 ),Mon(1 ),Tue(1 ),Wed,Thu,Fri,SAR; // All are week subclass objects

// Constructor

PrivateWeek () {system.Out. Println ("this is first constructor ");};

PrivateWeek (IntDay) {system.Out. Println ("this is second constructor ");};

}

Public EnumTrafficlamp {

Red(20 ){

@ Override

PublicTrafficlamp nextlamp (){

Return
Green;

}

},Green(30 ){

@ Override

PublicTrafficlamp nextlamp (){

//TodoAuto-generated method stub

Return
Yellow;

}

},Yellow(40 ){

@ Override

PublicTrafficlamp nextlamp (){

//TodoAuto-generated method stub

Return
Red;

}

};

Public AbstractTrafficlamp nextlamp ();

Private int
Time
;

PrivateTrafficlamp (IntTime ){

This. Time = time;

}

}

/**

*@ ParamARGs

*/

Public static voidMain (string [] ARGs ){

System.Out. Println (Week.Fri);

System.Out. Println (Week.Fri. Ordinal ());

System.Out. Println (Week.Valueof("Sun "));

System.Out. Println (Week.Values(). Length );

}

}

 

 

6. Class

The 8 Basic Types + void all belong to 9 pre-defined class objects, the original class.

PackageJava high-tech;

 

Public classTestclass {

 

Public static voidMain (string [] ARGs)ThrowsException {

/**

* Three methods are used to obtain the class bytecode, which is the same as the same bytecode.

*/

String S = "test ";

ClassC1 = S. getclass ();

ClassC2 = string.Class;

ClassC3 = Class.Forname("Java. Lang. String ");

System.Out. Println (C1 = c2 );

System.Out. Println (C1 = C3 );

System.Out. Println ("int. Class = integer. Class? "+ (Int.Class= Integer.Class));

System.Out. Println ("int. Class = integer. type? "+ (Int.Class= Integer.Type));

System.Out. Println ("int []. Class is primitive? "+Int[].Class. Isprimitive ());

System.Out. Println ("int []. Class is primitive? "+Int[].Class. Isarray ());

System.Out. Println ("void. Class is primitive? "+
Void.Class. Isprimitive ());

}

}

 

 

7. Reflection calls the Domain value in a class

Package Java high-tech;

 

Import java. Lang. Reflect. constructor;

Import java. Lang. Reflect. field;

 

Class B {

Private int X;

Public B (){};

Public B (int I) {This. x = I ;};

Private int getx (){

Return this. X;

}

}

Public class testreflect {

/**

* @ Param ARGs

*/

Public static void main (string [] ARGs) throws exception {

String S1 = new string (New stringbuffer ("ABC "));

 

Constructor <?> Constructor = string. Class. getconstructor (stringbuffer. Class );

 

String S2 = (string) constructor. newinstance (New stringbuffer ("ABC "));

 

String S3 = new string (New stringbuffer ("ABC "));

 

System. Out. println (s1.equals (S2) + "" + (S1 = S2 ));

System. Out. println (s1.equals (S3 ));

 

B = B. Class. newinstance ();

// System. Out. println (B. getx (); // getx () is private

B b2 = new B (10 );

// Field = B. Class. getfield ("X ");

Field field = B. Class. getdeclaredfield ("X ");

Field. setaccessible (true); // brute force reflection

System. Out. println (field. Get (B2 ));

}

}

 

 

 

8. Call Private methods in a Class Using Reflection

PackageJava high-tech;

ImportJava. Lang. Reflect. method;

/**

* Use reflection to change a single character of a string in a class to a for all B.

*@ Author Jzm

*/

ClassTestmethodclass {

PrivateString X;

PrivateString y;

PublicTestmethodclass (string X, string y ){

Super();

This. X = X;

This. Y = y;

}

 

Private void 
Print (IntI){

System.Out. Println ("hello" + I );

}

 

@ Override

PublicString tostring (){

Return"X =" + x + "Y =" + Y;

}

}

Public classTestuserprivatemethod {

/**

*@ ParamARGs

*/

Public static voidMain (string [] ARGs)
ThrowsException {

Testmethodclass t2 =NewTestmethodclass ("1", "2 ");

// T. Print ();

Method method = testmethodclass.Class. Getdeclaredmethod ("print ",NewClass [] {Int.Class});

Method. setaccessible (True);

Method. Invoke (t2,NewObject [] {1 });

}

}

 

9. annotation Annotation

New Features of jdk1.5

Including three annotation types

 

You can customize annotations such:

 

@ Retention (retentionpolicy.Runtime)

Public @ InterfaceMyannotation {

String color ()Default"Blue ";

}

 

 

10. Generic

 

 

 

 

 

Class 11 Loaders

 

JVM provides three class loaders Bootstrap (written in C ++)-à extclassloader (Java class) and à appclassloader (Java class)

 

You can write a class loader by yourself.

 

12. For more information about dynamic proxy, see my other article.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.