Java basics enhanced by Java Web
1. install and use myeclipse * eclipse: it is a free development tool * myeclipse: it is a paid plug-in to crack the requirements of myeclipse and ** installation directory: you cannot have Chinese characters or spaces **. After installation is complete, select a workspace. The workspace cannot contain Chinese characters or spaces * Crack myeclipse ** To run. bat file, but before running, jdk must be installed. By configuring the environment variable * myeclipse use * to create a project-type java project web project-select the dependent jdk, you can use the jdk that comes with myeclipse, or use the installed jdk * to create the package-cn. itcast. test XX. XX. XX * create a class-class naming convention in the package: ** the first letter must be capitalized, for example, TestDemo1 UserManager *. Create the public void method in the class. Test1 (parameter list) {method body or return value;}-the first letter of the method naming convention is lowercase, for example: addNum () * defines the variable-variable naming convention ** the first letter is lowercase, the first letter of the second word must be capitalized, for example, the userName * naming method ** yonghuming mima using Chinese pinyin ** is the most basic principle that Chinese pinyin and English letters cannot be mixed with userMing: see what the name means * code needs to be indented * run the program as java application debug as java application2, debug mode (breakpoint debug mode) * use this mode, debug the program (see the changes in data in the program) * use debug to set a breakpoint (stop the program running on this line)-display the line number-double-click the left, and a dot appears, indicates that a breakpoint is set * uses the debug as method to run the trip Sequence-Prompt whether the debugging interface is displayed. yes-there is a green bar at the breakpoint, indicating that the program stops in this line and does not run down * to let the program run down, -The step over shortcut is F6 (one-step execution)-resume F8: indicates that the debugging is complete and runs directly down. ** for example, if there is a breakpoint after the current breakpoint, skip to the next breakpoint, ** if there is no breakpoint after the current breakpoint, the program runs directly. * debug another purpose ** view the source code of the program ** F5 step into: Enter the method ** F7 step return: return 3. Use the * code prompt for the shortcut key of myeclipse: alt/* Quick Guide package ctrl shift o * single line comment ctrl/* remove single line comment ctrl/* multiple line comment ctrl shift/* remove multi-line comment ctrl shift \ * Delete row ctrl d4, junit use * unit test * Test The object is a method in the class * juint is not part of javase and you want to use the imported jar package **. However, in myeclipse, the self-contained jar package of junit * first, when the unit test method of junit version 3.x 4.x * is used, the public void method name () {}* uses annotations to run the test method, in the preceding method ** @ Test: indicates that the method performs a unit Test --- @ Test public void testAdd1 () {TestJunit test01 = new TestJunit (); test01.testAdd (2, 3 );} -select the method name and right-click "run as --- junit test". When a green bar appears, the method test is successful. When a red or brown bar appears, indicates that the method test fails. --- to run multiple test methods in the class, click another location in the class and run as --- junit test ** @ Ignore: indicates that this method does not perform unit tests. ** @ Before: Run ** @ After: Run ** Assert. assertEquals ("test expected value", "actual value of method running ") jdk5.0 new features jdk 1.1 1.2 1.4 5.0 ** generic, enumeration, static import, Automatic Disassembly box, enhanced for, variable parameters ** reflection 5, generic Introduction * why use generic? -Generally, it is used on a set. ** for example, if a value of the string type is put into the set, the value is put into the set and the type of the set is lost, it can only be of the object type. In this case, if you want to convert the type of the value, the type conversion error may easily occur. How can this problem be solved, you can use generics to solve * How to Use generics on a set-list set map-generic syntax set for a Common set <String> for example, List <String> * What is written in a generic set? an object, string cannot be written to basic data types, such as int (****) ** write the basic data type corresponding to the packaging Class byte -- Byte short -- Short int -- Integer long -- Long float -- Float double -- Double char -- Character boolean -- Boolean * use the generic type on the list three implementations of list ArrayList upload List Vector code: @ Test public void testList () {list <String> list = new ArrayList <String> (); List. add ("aaa"); list. add ("bbb"); list. add ("ccc"); // There are three methods to traverse the list set // The common for loop iterator enhances for // The common for loop for (int I = 0; I <list. size (); I ++) {String s = list. get (I); System. out. println (s);} System. out. println ("======================"); // use enhanced for (String s1: list) {System. out. println (s1);} System. Out. println ("======================"); // use the Iterator to traverse Iterator <String> it = list. iterator (); while (it. hasNext () {System. out. println (it. next ();} * Job 1: ArrayList objects list Vector three differences * use generic code on set: // use @ Test public void testSet () {set <String> Set = new HashSet <String> (); set. add ("www"); set. add ("qqq"); set. add ("zzz"); // set. add ("qqq"); // There are two ways to traverse the set // The iterator enhances for // use the enhanced for traversal for (String s 2: set) {System. out. println (s2);} System. out. println ("======================"); // use the Iterator to traverse Iterator <String> it1 = set. iterator (); while (it1.hasNext () {System. out. println (it1.next () ;}* use the generic-map structure on map: key-valu format code: // use the generic @ Test public void testMap () {map <String, String> Map = new HashMap <String, String> (); map on map. put ("aaa", "111"); map. put ("bbb", "222"); map. put ("ccc", "333"); // You can traverse the map several times. Calendar method: two types // 1. Obtain all keys, get the value through the key. Use the get method. // 2. Obtain the relationship between the key and the value. // use the first method to traverse. // obtain all the key sets <String> sets = map. keySet (); // traverses the set for (String key: sets) returned by all keys {// obtain value String value = map through key. get (key); System. out. println (key + ":" + value);} System. out. println ("================"); // obtain the relationship Set between key and value <Entry <String, string> sets1 = map. entrySet (); // traverse sets1 for (Entry <String, String> entry: sets1) {// Entry is the relationship between key and value String keyv = entry. getKey (); String valuev = entry. getValue (); System. out. println (keyv + ":" + valuev) ;}} 6. Use Method * to define an array to realize the exchange of array elements at the specified position. * The logic of the method is the same, only the data types are different. In this case, use the generic Method */** to use the generic method. You need to define a type and use uppercase letters to represent T: this T represents any type * Before void is written before the return value <T> * ======== indicates that a type is defined. This type can be used below. T **/public static <T> void swap1 (T [] arr, int a, int B) {T temp = arr [a]; arr [a] = arr [B]; arr [B] = temp;} ** job 2: implement a generic method that accepts any array and reverses all elements in the array. 7. Usage of generics in classes (understanding) * define a type on a class. This type can be directly used in the class * public class TestDemo04 <T> {// T type T aa can be directly used in the class; public void test11 (T bb) {}// write A generic type defined in the class for the static method. public static cannot be used in the static method. <A> void test12 (A cc) {} 8. Enumeration overview * What is enumeration? ** The value must be within a certain range. This value can only be any of the values in this range. ** Real-world scenario: Traffic Signals in three colors, but only one of the three colors can be highlighted at a time. * use the enum keyword ** enum Color3 {RED, GREEN, and YELLOW ;} * enumeration constructor is also private * Special Enumeration operations (for details) ** there are constructor methods in enumeration classes ** there are parameters in the constructor, you need to write the parameter ** there is an abstract method in the enumeration class ** on each instance. In each instance of the enumeration, rewrite this abstract Method 9 and the enumerated api operation ** name. (): return the name of the enumeration ** ordinal (): subscript of the enumeration. The subscript starts from 0 ** valueOf (Class <T> enumType, String name ): the enumerated object ** has two other methods, both of which are not in the api. during compilation, two methods are generated. *** valueof (String name) convert the enumerated object *** values () to obtain the number of all enumerated objects Group * exercise: conversion between enumeration objects, enumeration object subscript, and enumeration Object Name representation-// knows the enumeration object to obtain the enumeration name and subscript @ Test public void test1 () {// obtain the enumerated object Color100 c100 = Color100.RED; // enumerated name String name = c100.name (); // enumerated subscript int idx = c100.ordinal (); System. out. println (name + "" + idx);}-// know the enumeration name. Obtain the enumerated object and subscript @ Test public void test2 () {String name1 = "GREEN "; // obtain the object Color100 c1 = Color100.valueOf (name1); // enumerated subscript int idx1 = c1.ordinal (); System. out. println (id X1);}-// know the subscript of the enumeration and obtain the enumerated object and name @ Test public void test3 () {int idx2 = 2; // obtain the enumerated object Color100 [] cs = Color100.values (); // obtain the object Color100 c12 = cs [idx2] According to the subscript; // obtain the enumerated name String name = c12.name (); System. out. println (name);} 10. static import (learn more) * You can directly use the static import method in the code to import static methods or constants * import static XX. XX. xxx * import static java. lang. system. out; import static java. util. arrays. sort; ** for example, a calculator is now implemented in the Math class 11, and the box is automatically disassembled * packed ** Convert the basic data type to the packaging class * unpacking ** to convert the packaging class to the basic data type ** // automatically boxing Integer I = 10; // automatically unboxing int m = I; ** in jdk1.4, how do I implement packing and unpacking-// In jdk1.4, implement the disassembling box public void test1 () {// boxed Integer m = new Integer (10 ); // unpack int a = m. intValue ();} ** jdk is backward compatible-for example, the code written in jdk1.4 can also be run in 5.0. ** exercise: backward compatible = the result of execution is that doSomething (double m) = is called in jdk1.4. If the following method is called, type conversion is required, however, jdk cannot implement Automatic Disassembly box = Because jdk is backward compatible, you can call this method in jdk. Still call this method public static void main (String [] args) {doSomething (10);} public static void doSomething (double m) {System. out. println ("double ...... ");} public static void doSomething (Integer a) {System. out. println ("integer ..... ");} ** remember: The packaging class * int --- Integer * char --- Character12 corresponding to the eight basic data types, and the Enhancement for loop (*****) * syntax for (the retrieved value: the set to be traversed) {}-for (String s: list) {System. out. println (s);} * Use Case: array; real Now, the Iterable interface set can use an enhanced for loop * use an enhanced for loop to traverse the list set on the set to implement the Iterator interface. Therefore, you can use an enhanced for loop map instead of an enhanced for loop, the Iterator interface is not implemented, so it is not possible to use an enhanced for loop * Enhanced for loop purpose: to replace the Iterator ** enhanced for underlying layer is implemented by the Iterator 13. Content supplement (1) generic erasure * First, generics only appear in the source code phase. After compilation, Generics do not exist. (2) exercise: implement a generic method and accept arrays of any type, reversing all element codes in the array public static <T> void reverses (T [] arr1) {/** basic idea: swap the first element with the last one, swap the second element with the last and last element .... * Swap length/2 ** // traverse the array for (int I = 0; I <arr1.length/2; I ++) {/* int temp = arr1 [0]; arr1 [0] = arr1 [arr1.length-1]; */T temp = arr1 [I]; arr1 [I] = arr1 [arr1.length-i-1]; arr1 [arr1.length-i-1] = temp;} 14. In what scenarios can variable parameters be applied to? ** add two numbers, implement the addition of three numbers and four numbers -- if multiple methods are implemented, the logic in these methods is basically the same. The only difference is the number of passed parameters, you can use variable parameters * variable parameters to define the data type... array name * is interpreted as an array, which stores the passed parameter-code public static void add1 (int... nums) {// nums is interpreted An array stores the passed parameters // System. out. println (nums. length); int sum = 0; // traverse the array for (int I = 0; I <nums. length; I ++) {sum + = nums [I];} System. out. println (sum);} * Note (1) variable parameters must be written in the parameter list of the method and cannot be defined separately (2) only one variable parameter in the parameter list of the method (3) can be included in the Variable Parameter List of the method, which must be placed at the end of the parameter-add1 (int a, int... nums) 15. How reflection works (********* understanding ********) * In some code with high universality * Most of the frameworks learned later are implemented using reflection * in framework development, all are developed based on the configuration file. ** you can configure the class in the configuration file. You can get all the content in the class through reflection and execute a method in the class *. All content: properties, constructor without parameters, constructor with parameters, common method * image analysis and reflection principle * You must first save the java file to the local hard disk. java * compile the java file. class files * use jvm to load class files to the memory through classes * Everything is an object, and class files use Class classes in the memory * When reflection is used, first, you must obtain the Class. After obtaining this Class, you can get all the content in the class file-including the property construction method common method * the property is constructed using a class Filed * using a class Constructor * Common method through a class Method16, using the reflection operation class no parameter Constructor (** write **) * first obtain the Class-// obtain the Class clazz1 = Person. class; Class clazz2 = new Person (). get Class (); Class clazz3 = Class. forName ("cn. itcast. test09.Person"); * For example, to instantiate a Class, you can use new instead of new. How can you get it? -// Obtain Class c3 = Class. forName ("cn. itcast. test09.Person "); // obtain the Person class's instance Person p = (Person) c3.newInstance (); * Code // operation of the construction method without parameters @ Test public void test1 () throws Exception {// get Class c3 = Class. forName ("cn. itcast. test09.Person "); // obtain the Person class instance Person p = (Person) c3.newInstance (); // set the value p. setName ("zhangsan"); System. out. println (p. getName ();} 17. constructor with parameters for the reflection operation (** write **) // constructor with parameters for the Operation @ Test public void test2 () throws Exception {// get Class c1 = Class. forName ("cn. itcast. test09.Person "); // use the constructor with parameters // c1.getConstructors (); // obtain all constructor methods. // pass the parameter types in the constructor with parameters, type: pass Constructor cs = c1.getConstructor (String. class, String. class); // set the value through the constructor with parameters // create the Person instance Person p1 = (Person) cs through the constructor with parameters. newInstance ("lisi", "100"); System. out. println (p1.getId () + "" + p1.getName ();} 18. Use the reflection operation attribute (** write **) * // operation name attribute @ Test public void test3 () {try {// obtain Class c2 = Class. forName ("cn. itcast. test09.Person "); // get the name attribute // c2.getDeclaredFields (); // get all attributes // get the Person class instance Person p11 = (Person) c2.newInstance (); // obtain the attribute through this method. The parameter is the attribute name Field f1 = c2.getDeclaredField ("name"); // The operation is a private attribute and the operation is not allowed, you need to set the setAccessible (true) private attribute to be operated, and the f1.setAccessible (true) private attribute to be operated; // set THE name value set method with two parameters: the instance of the first parameter class, the second parameter is the set value f1.set (p11, "wangwu"); // equivalent. name = "wangwu"; System. out. println (f1.get (p11); // equivalent to p. name} catch (Exception e) {e. printStackTrace () ;}19. use generic operations for common methods (** write **) * use the Method class to represent common methods * Code // operate common methods, for example, the setName @ Test public void test4 () throws Exception {// obtain the Class c4 = Class. forName ("cn. itcast. test09.Person "); // obtain the Person instance Person p4 = (Person) c4.newInstance (); // obtain the common method // c4.getDeclaredMethods (); // get all common methods // pass two parameters: the first parameter, the Method name; the second parameter, the type of the parameter in the Method m1 = c4.getDeclaredMethod ("setName", String. class); // Let the setName method be executed, execute the set value // use invoke (p4, "niuqi"); pass two parameters: the first parameter, the person instance; the second parameter, the Set Value // After the invoke method is executed, it is equivalent to executing the setName method. At the same time, this method sets the value niuqi m1.invoke (p4, "niuqi"); System. out. println (p4.getName ();} * // specifies the private method of the operation. The value must be set to true * // m1.setAccessible (true ); * When the operation method is a static method, the static method is called by the class name. method Name, no class instance * when using the static reflection method, it is not required * In the first parameter of the invokie method, write a null-m1.invoke (null, "niuqi ");