----------- Android training, Java training, and hope to communicate with you! ------------
1. Static Import
Import static statement: imports the static method of a class.
import static java.lang.Math.*;public class Demo {public static void main(String[] args) {System.out.println(max(3, 7));}}
2. variable parameters
When do I use variable parameters?
When the number of parameters received by a method is not fixed.
Variable Parameter features:
① It can only appear at the end of the parameter list;
② "..." Is placed between the parameter type and the parameter name, and there are no spaces before and after it;
③ When a method containing variable parameters is called, the compiler implicitly creates an array for the variable parameter. In the method body, the variable parameters can be accessed as arrays.
Import java. util. arrays;
Class demo {
Public static void main (string [] ARGs ){
Add (3, 4, 7, 9, 2, 1 );
}
Public static void add (int I, Int... ARGs ){
System. Out. println ("variable parameter:" + arrays. tostring (ARGs ));
Int sum = I;
For (INT Arg: ARGs) {// enhancement
Sum + = ARG;
}
System. Out. println ("sum:" + sum );
}
}
3. Enhance the for statement (foreach)
Syntax: For (Type Iteration variable name: Set variable name)
Features: ① iteration variables (ARG in the code above) must be defined in () ② Collection variables can be arrays or collection classes that implement iterable.
4. Automatic packing and unpacking of basic data
Public class autobox {public static void main (string [] ARGs) {INTEGER iobj = 3; // automatically boxed character cobj = 'H'; system. out. println (iobj + 2); // automatically unpack system. out. println (cobj + 2); // automatically unpack integer I1 = 154; integer I2 = 154; integer I3 = 127; integer I4 = 127; system. out. println (I1 = I2); // falsesystem. out. println (I3 = I4); // true} // conclusion: After 1.5, an object of the new basic data type is not required and assigned directly to the reference of the basic data type. //-128 ~ The number between 127 is put in a pool and can be shared.
Sharing a large number of elements that are repeatedly used is called the "meta-mode ". Different parts of the element are used as parameters of the method.
For example, the icons of files and folders are designed in the metadata mode.
5. Enumeration★★
Enumeration features: 1. Allow a variable of a certain type to take only a fixed number of values; otherwise, the compiler reports an error. 2. When an enumeration class is used, all static variables in it will be initialized.
/** The enumeration type is modified with Enum, which is similar to class and interface * for a specific Enum type, the enum belongs to the subclass by default, all methods of the enum class * and two Implicit Static Methods public static t valueof (string) and public static T [] values ()***/
Class enumdemo {public static void main (string [] ARGs) {weekday MON = weekday. mon; // print the name of the enumerated value system. out. println (Mon. name (); // convert the string to the enumerated type system. out. println (weekday. valueof ("sun"); // obtain all weekday instances of weekday type [] WDS = weekday. values (); int COUNT = 0; For (weekday WD: WDS) {system. out. println ("weedday:" + WD + "...... "++ count) ;}} Enum weekday {// The internal class form defines the enumeration types Mon (), Tue, wed, Thu, Fri, SAT, Sun }}
Mon (here the parameter indicates which constructor is used). If no parameter is used, the default null constructor is used.
Public Enum trafficlamp {// traffic light enumeration red (30) {public trafficlamp nextlamp () {return green ;}, Green (40) {public trafficlamp nextlamp () {return yellow ;}, yellow (5) {public trafficlamp nextlamp () {return red ;}}; private int time; private trafficlamp (INT time) {// constructor of the enumeration class this. time = time;} public abstract trafficlamp nextlamp (); // you can also use if... else write Judgment Method}
6. Generic★★★
The generic type limits the type of data stored in the set.
1. The forced conversion process after elements are removed
2. Transfer runtime errors to compilation
When compiling the javac compiler, the generic type checks the specified type. After the collection element is stored, the "type information" is removed, which can be proved by reflection.
As there is no limit in the compiled bytecode, any element can be added to it through reflection.
So generics are only useful to compilers.
Generic class.
When to define generic classes?
When the referenced data types to be operated in the class are uncertain, the object is defined to complete the extension. Now the extension is defined for the generic type.
This prevents strong conversion during the operation.
Public class genericclass {public static void main (string [] ARGs) {/* tools T = new tools (); T. setobject (new worker (); worker W = (worker) T. getObject (); // forced transformation */utils <worker> U = new utils <worker> (); U. setobject (new worker (); worker W = u. getObject (); // you do not need to convert it to system. out. println (w) ;}} class worker {Public String tostring () {return "I'm a worker ";}} class teacher {} class tools {// early class private object OBJ; Public void setobject (Object OBJ) {This. OBJ = OBJ;} public object GetObject () {return OBJ;} class utils <YY> {// generic class private YY y; Public void setobject (yy y) {This. y = y ;}public YY GetObject () {return y ;}}
Generic method:
/** The generic type defined by the generic class is valid throughout the class. If the method is used, after the object type to be operated is determined, the real parameter passed to the method must be of the object type. ** To allow different methods to operate on different types, and the types to be operated are unknown, you can define generics on methods. * Generic methods and generic classes can be in the same class. And does not affect each other. * Define the Generic Format: <> put it before the type of the method return value, followed by the method modifier (for example, public static <t> void method ()). **★★Static methods cannot access generics defined on classes. The type is determined only after the object is created, and the static method has been loaded before the object is created. If the data type of the static method operation is unknown, you can define the generic type on the static method. **/Public class genericdemo {public static void main (string [] ARGs) {test <string> T = new test <string> (); T. show (9); // error: the show method in the test <t> class cannot be applied to the given type; T. print (9); // 9} class test <t> {public void show (t) {// consistent with the class generic system. out. println (t);} Public <q> void print (Q) {// any type of system can be operated. out. println (q );}}
Generic interfaces are similar to generic classes. In development, they are generally defined by others and used directly.
Generic restrictions:
/**? Wildcard, which can also be understood as a placeholder ** generic limitation: used for generic extension. * <? Extends E> can receive e-type or sub-type of E. The upper limit is * <? Super E> can receive the parent type of E or E. The lower limit ***/import Java. util. *; public class genericlimited {public static void main (string [] ARGs) {treeset <student1> TS = new treeset <student1> (New comp (); Ts. add (New student1 ("sabc1"); Ts. add (New student1 ("sabc2"); Ts. add (New student1 ("sabc3"); Ts. add (New student1 ("sabc4"); For (iterator <student1> it = ts. iterator (); it. hasnext ();) {system. out. println (it. next (). getname ();} tre ESET <worker1> ts1 = new treeset <worker1> (New comp (); ts1.add (New worker1 ("WAB --- C1 ")); ts1.add (New worker1 ("WAB --- C2"); ts1.add (New worker1 ("WAB --- C3"); ts1.add (New worker1 ("WAB --- C4 ")); for (iterator <worker1> it = ts1.iterator (); it. hasnext ();) {system. out. println (it. next (). getname () ;}} class person {private string name; Public Person (string name) {This. name = Name;} Public String getname () {return n Ame ;}} class student1 extends person {public student1 (string name) {super (name) ;}} class worker1 extends person {public worker1 (string name) {super (name) ;}} class comp implements comparator <person >{// definition <? Super E>, specifying the parent type, comparing multiple child types public int compare (person P1, person P2) {return p1.getname (). compareto (p2.getname ());}}
Arraylist <string> Al [] = new arraylist <string> [4]; error: the array type set cannot be generic.
Vector V1 = new vector <string> (); // The Compiler does not report an error.
Vector <Object> v2 = V1; // The Compiler does not report an error (V1 compiler will use it as a vector)
<> The parameter type can be obtained through reflection. In the reflection method, the generic parameter xxxmethod. getgenericparametertypes () is obtained. Then, the actual input type parameter ptype. getactualtypearguments () [0] is obtained.
7. Annotations★★★★★
Annotation
Transmits certain information to the compiler or tool software.
Adding an annotation is equivalent to an annotation instance object.
Annotations can be added to parameters and local variables of packages, classes, fields, methods, and methods.
The Java. lang package contains the most basic annotation.
@ Suppresswarnings indicates that some warnings of the compiler are eliminated; its annotation lifecycle is retentionpolicy. Source
@ Deprecated indicates the code that is out of date; its annotation lifecycle is retentionpolicy. Runtime
@ Override indicates that this method overwrites the method of the parent class. Its annotation lifecycle is retentionpolicy. Source.
Add attributes to annotations
Import Java. lang. annotation. elementtype; import Java. lang. annotation. retention; import Java. lang. annotation. retentionpolicy; import Java. lang. annotation. target; import CN. itcast. practice. heima. trafficlampenum;/** annotation lifecycle: * retentionpolicy. source corresponds to the Java source file * retentionpolicy. class corresponds to the class file (default) * retentionpolicy. runtime corresponds to the byte code in the memory *** // ** @ interface metaannotation {string value ();} the output error occurs when this annotation is put as package permission, leave a mark @. Put it in a separate file and use the public permission to appear. **/@ Target ({elementtype. method, elementtype. type}) // scope of action @ retention (retentionpolicy. runtime) // annotation life cycle: Public @ interface annotationmelon {// annotation attribute string color () Default "blue"; string value (); int [] arrayattr () default {1, 3, 4}; trafficlampenum. trafficlamp () default trafficlampenum. trafficlamp. red; metaannotation annotationattr () default @ metaannotation ("XYZ");} @ annotationmelon (annotationattr = @ metaannotation ("wyl"), color = "red ", value = "str", arrayattr = 2) // there is only one element in the arrayattr array. You can omit {} class demo {@ annotationmelon ("ABC ") // public static void main (string [] ARGs) for the Value Attribute {// whether there is a certain type of annotation Boolean b1 = demo. class. isannotationpresent (annotationmelon. class); system. out. println (B1); // determines whether the annotation type is Boolean b2 = annotationmelon. class. isannotation (); system. out. println (B2); If (demo. class. isannotationpresent (annotationmelon. class) {// obtain the annotationmelon ANM = Demo "body" annotation. class. getannotation (annotationmelon. class); system. out. println (ANM); system. out. println (ANM. color (); system. out. println (ANM. value (); system. out. println (ANM. arrayattr (). length); system. out. println (ANM. lamp (). nextlamp (); system. out. println (ANM. annotationattr (). value (); // print the value of the annotation type attribute @}}}
8. stringbuilder is also 1.5 out.
9. Lock Mechanism
It is explained in multithreading.
10. Update the thread creation mechanism. Executors. New ****;
----------------------- Android training, Java training, and hope to communicate with you! ----------------------
For details, see:Http://edu.csdn.net/heima