1. Installation and use of myeclipse
* eclipse: is a free development tool* myeclipse: is a paid plug-in to crack myeclipse, ** Installation directory requirements: There cannot be Chinese and spaces** After installation is completed, select a workspace, which cannot have Chinese and spaces* Cracking myeclipse** Run the run.bat file, but before running, you must install jdk, and use the environment variable * use of myeclipse* Create a project- Type java project web project- Select the dependency jdk, you can use the jdk that comes with myeclipse, or you can use the installed jdk* to create a package package- cn.itcast.test XX.XX.XX* Create a class in the package- Class naming specification: ** The initial letter should be capitalized, for example: TestDemo1 UserManager* Create method public void test1 (parameter list) {Method body or return value;} - Method naming specification lowercase first letter, for example: addNum()* Define variable - variable naming specification ** The initial letter lowercase, the first letter of the second word should be uppercase, such as userName* There is another way to naming** Use Chinese pinyin to naming yonghuming mima** You cannot mix Chinese pinyin and English letters to use userMing* The most basic principle of naming: see the name and know what it means * The code needs to be indented* Run the program run as java applicationdebug as java application2. Debug debug mode (breakpoint debug mode)
* Using this mode, debug the program (see the changes in the data in the program) * The first step to use debug requires setting a breakpoint (let the program run stop on this line) - Show the line number - Double-click on the left, a dot appears, indicating that a breakpoint has been set * Use debug as method, run the program - prompt whether to enter the debug interface, yes- On the breakpoint, there is a green bar, indicating that the program stops on this line and does not run down * You can let the program execute down, - Use step over The shortcut key is F6 (single-step execution) - resume F8: indicates that the debugging is over, run directly down ** For example, there is a breakpoint after the current breakpoint, jump to the next breakpoint, ** If there is no breakpoint behind the current breakpoint, the program runs directly end * debug Another purpose ** View the program's source code** F5 step into: Enter the method** F7 step return: Return
3. Use of myeclipse shortcut keys
* Code prompt alt /* Quick guide package ctrl shift o* Single line comment ctrl /* Remove single line comment ctrl /* Multi-line comment ctrl shift /* Remove multi-line comment ctrl shift /* Delete line ctrl d
4. Use of junit
* Unit test* The test object is a method in a class* juint is not part of javase, I want to use the import jar package** However, the jar package with junit is brought in myeclipse* First of all, when junit version 3.x 4.x* unit test method, method naming rules public void method name() {}* Run the test method using annotation method, above the method** @Test: means that the method is unit tested--- @Testpublic void testAdd1() {TestJunit test01 = new TestJunit();test01.testAdd(2, 3);}- Select the method name, right-click to run run run as --- junit test- When a green bar appears, it means that the method test passes - When a red-brown bar appears, it means that the method test fails--- To run multiple test methods in the class, click other locations in the class, run as --- junit test** @Ignore: means that this method is not unit tested** @Before: Running on each method** @After: Running after each method** Assert.assertEquals("Test the expected value", "The actual value of the method running") jdk5.0 new features jdk 1.1 1.2 1.4 5.0** Generics, enumeration, static import, automatic unboxing, enhancement for, variable parameters** Reflection5. Introduction to generics
* Why use generics? - Generally used on sets** For example, now put a string type value into the set. At this time, after putting this value into the set, the type that loses its ability can only be the object type. At this time, for example, if you want to type convert this value, it is easy to have type conversion errors. How to solve this problem can be used to solve this problem using generics* How to use generics on sets - Commonly used set list set map- Generic syntax collection <String> For example, List<String>* is written as an object in a generic, and String cannot write basic data types, such as int (****)** Write basic data types corresponding to the wrapper class byte -- Byteshort -- Shortint -- Integerlong -- Longfloat -- Floatdouble -- Doublechar -- Characterboolean -- Boolean* Three implementations of using generic lists on list ArrayList linkedList Vector code: @Testpublic void testList() {List<String> list = new ArrayList<String>();list.add("aaa");list.add("bbb");list.add("ccc");//There are several ways to traverse the list collection.//Ordinary for loop iterator enhancement for//Ordinary for loop for(int i=0;i<list.size();i++) {String s = list.get(i);System.out.println(s);}System.out.println("=============================================================================================================================================================================================================================================================================================================================================================================================================================================================================================== These three differences * Use generic code on set: //Generics use set on set @Testpublic void testSet() {Set<String> set = new HashSet<String>();set.add("www");set.add("qqq");set.add("zzz");//set.add("qqq");//Transfer There are several ways to traverse set //Iterator enhancement for//Use enhancement for traversal for (String s2 : set) {System.out.println(s2);}System.out.println("======================================================================================================================================================================================================================================================================================================================================================== = set.iterator(); while(it1.hasNext()) {System.out.println(it1.next());}}* Use generics-map structure on map: key-valu form code://using generics on map @Testpublic void testMap() {Map<String,String> map = new HashMap<String,String>();map.put("aaa", "111");map.put("bbb", "222");map.put("ccc", "333");//Transip map There are several ways to traverse // 1. Get all keys and get value through key Use get method // 2. Get the relationship between key and value //Use the first method //Get all keySet<String> sets = map.keySet();//Transfer the setfor returned by all keys (String key : sets) {//Get valueString value = map.get(key);System.out.println(key+" : "+value);}System.out.println("=============================================================================================================================================================================================================================================================================================================================================================================================================== {//entry is the relationship between key and value String keyv = entry.getKey();String valuev = entry.getValue();System.out.println(keyv+" : "+valuev);}}6. Generics are used in methods
* Define an array to implement the exchange of array elements at the specified position* The method logic is the same, but the data types are different. At this time, use generic methods* /** Use generic methods to define a type using capital letters to represent T: this T represents any type* Before the return value is written <T>* ========= Define a type. This type is T* Here you can use this type 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, accept any array, and reverse all elements in the array7. Use of generics on classes (understand)
* Define a type on a class, this type can be used directly in the class * public class TestDemo04<T> {//You can use T type Taa in the class; public void test11(T bb) {}//Write a generic defined by a static method on the class, and you can no longer use public static method public static <A> void test12(A cc) {}}8. Introduction to enumeration
* What is an enum? ** You need to take a value within a certain range, and this value can only be any one of the ranges. ** Realistic scene: Traffic lights, there are three colors, but only one of the three colors can be bright at a time* Use one keyword enum** enum Color3 {RED,GREEN,YELLOW;}* The enum construction method is also private* The operation of special enum (understand)** There is a construction method in the enum class** There is a parameter in the construction method, and parameters need to be written on each instance** There is an abstract method in the enum class** Rewrite this abstract method in each instance of the enum9. The operation of enumerated API
** name(): Return the name of the enum** ordinal(): The subscript of the enum, the subscript starts from 0** valueOf(Class<T> enumType, String name): Get the object of the enum** There are two other methods, both of which are not in the API, and two methods are generated during compilation*** valueof(String name) Convert the enum object*** values() Get all enum object array* Exercise: Conversion between enum objects, enum object subscript, and enum object name representation- // Know the enum object, get the enum name and subscript @Testpublic void test1() {//Get the enum object Color100 c100 = Color100.RED;//Enum name String name = c100.name();//The subscript of the enumeration int idx = c100.ordinal();System.out.println(name+" "+idx);}- //Know the name of the enumeration, get the object and subscript of the enumeration @Testpublic void test2() {String name1 = "GREEN";//Get the object Color100 c1 = Color100.valueOf(name1);//Enum int idx1 = c1.ordinal();System.out.println(idx1);}- //Know the subscript of the enumeration, get the object and name of the enumeration @Testpublic void test3() {int idx2 = 2;//Get the enumeration object Color100[] cs = Color100.values();//Get the object Color100 according to the subscript c12 = cs[idx2];//Get the enumeration name String name = c12.name();System.out.println(name);}10. Static import (understand)
* 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, now implement a calculator in the Math class
11. Automatic unpacking and assembly
* Box** Convert the basic data type to the packaging class* Unboxing** Convert the packaging class to the basic data type** //Autobox Integer i = 10;//Autobox int m = i;** How to box and unbox in jdk1.4 - //Autobox in jdk1.4 public void test1() {//Box Integer m = new Integer(10); //Unbox int a = m.intValue();}** jdk is backward compatible - For example, the code written in jdk1.4 can also run in 5.0** Exercise: backward compatible == The result of execution is to call doSomething(double m)== First of all, this method must be called in jdk1.4. If the following method is called, type conversion is required, but jdk1.4 cannot realize automatic unboxing == Since jdk is backward compatible, if this method is called in jdk1.4, this method will still be called in jdk5.0 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 wrapper class corresponding to the eight basic data types* int --- Integer* char--- Character12. Enhanced for loop (*****)
* Syntax for(the value traversed: the set to be traversed) {}- for(String s : list) {System.out.println(s);}* Usage scenarios: array; sets that implement the Iterable interface can use the enhanced for loop* Use the enhanced for loop to traverse the list set on the set The Iterator interface is implemented, so the enhanced for loop can be used, and the Iterator interface cannot be implemented, so the enhanced for loop cannot be used. The Iterator interface cannot be implemented, so the enhanced for loop cannot be used. The purpose of the enhanced for loop appears: to replace the iterator** The underlying layer of the enhancement for is implemented by the iterator13. Content supplement
(1) Generic erase* First of all, generics only appear in the source code stage. When compiled, generics no longer exist (2) Exercise: implement a generic method, accept arrays of any type, and reverse all element codes in the array public static <T> void reverses(T[] arr1) {/** Basic idea: swap the first element with the last element, and swap the second element with the penultimate element. . . . * Swap length/2* *///Transweep 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. Variable parameters
* In what scenario can variable parameters be applied: ** Implement the addition of two numbers, and 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 parameters passed. You can use variable parameters* Definition method of variable parameters Data type... The name of the array* is understood as an array, which stores the passed parameters-code public static void add1(int...nums) {//nums is understood as an array, which stores the passed parameters//System.out.println(nums.length);int sum = 0;//Tranquility array for(int i=0;i<nums.length;i++) {sum += nums[i];}System.out.println(sum);}* Note (1) Variable parameters need to be written in the parameter list of the method, and cannot be defined separately (2) There can only be one variable parameter in the parameter list of the method (3) Variable parameters in the parameter list of the method must be placed at the end of the parameter - add1(int a,int...nums)15. The principle of reflection (************)
* Applied in some codes with higher versatility* Most of the frameworks learned later are implemented using reflection* In framework development, they are developed based on configuration file** In configuration file, all contents in the class can be obtained through reflection, and a method in the class can be used to execute * All contents in the class: attributes, constructors without parameters, constructors with parameters, ordinary methods* The principle of drawing and analyzing reflection* First, you need to save the java file to the local hard disk.java* Compile the java file and become .class file* Use jvm to load the class file into memory through class load* Everything is an object, and the class file is represented by Class class in memory* When using reflection, you first need to obtain the Class class. After obtaining this class, you can get all the contents in the class file - including attribute constructors, ordinary methods * Attributes are filed* Constructor* Constructor* Normal method passes through a class Method
16. Use the parameterless construction method in the reflection operation class (** will write **)
* First get the Class class - // Get the Class class Class Class clazz1 = Person.class;Class clazz2 = new Person().getClass();Class clazz3 = Class.forName("cn.itcast.test09.Person");* For example: To instantiate a class, you can new, but do not use new, how to get it? - //Get ClassClass c3 = Class.forName("cn.itcast.test09.Person");//Get the instance of Person class Person p = (Person) c3.newInstance();* Code//Operate the constructor without parameters @Testpublic void test1() throws Exception {//Get ClassClass c3 = Class.forName("cn.itcast.test09.Person");//Get the instance of Person class Person p = (Person) c3.newInstance();//Set the value p.setName("zhangsan");System.out.println(p.getName());}17. Use reflection operation to have parameters construction method (** will write**)
//Operate the constructor with parameters @Testpublic void test2() throws Exception {//Get ClassClass c1 = Class.forName("cn.itcast.test09.Person");//Use the constructor with parameters//c1.getConstructors();//Get all constructors//Pass the parameter type in the constructor with parameters, and the type is passed in the form of a class. Cs = c1.getConstructor(String.class,String.class);//Set the value through the constructor with parameters//Create the Person instance through the constructor with parameters Person p1 = (Person) cs.newInstance("lisi","100");System.out.println(p1.getId()+" "+p1.getName());}18. Use reflection operation properties (** will write**)
* //Operate name attribute @Testpublic void test3() {try {//Get Class Class c2 = Class.forName("cn.itcast.test09.Person");//Get name attribute//c2.getDeclaredFields();//Indicate to get all attributes//Get the instance of Person class Person p11 = (Person) c2.newInstance();//Get the attribute through this method, the parameter is the name of the attribute Field f1 = c2.getDeclaredField("name");//The operation is a private property, and the operation is not allowed. You need to set the private property setAccessible(true), which can operate the private property f1.setAccessible(true);//Set the name value set method, two parameters: the first parameter instance, and the second parameter is the set value f1.set(p11, "wangwu"); //Equivalent to p.name = "wangwu";System.out.println(f1.get(p11)); //Equivalent to p.name}catch(Exception e) {e.printStackTrace();}}19. Use generic operation methods (** can write **)
* Use Method class to represent ordinary methods* Code //Operate ordinary methods, such as operating setName@Testpublic void test4() throws Exception {//Get Class Class c4 = Class.forName("cn.itcast.test09.Person");//Get Person instance Person p4 = (Person) c4.newInstance();//Get the ordinary method//c4.getDeclaredMethods();//Get all ordinary methods//Transfer two parameters: the first parameter, the method name; the second parameter, the type of the parameter in the method Method m1 = c4.getDeclaredMethod("setName", String.class);//Let the setName method execute, execute the set value //Use invoke(p4, "niuqi"); pass two parameters: the first parameter, person instance; the second parameter, set value // After executing the invoke method, it is equivalent to executing the setName method, and at the same time, a value is set to be niuqim1.invoke(p4, "niuqi"); System.out.println(p4.getName());}* //The private method of the operation needs to be set to true* //m1.setAccessible(true);* When the operation method is a static method, because the static method is called class name. Method name, no instance of the class is needed* When using reflection to operate the static method, no instance is needed* In the first parameter of the invokie method, write null- m1.invoke(null, "niuqi");The above is the Java Basic Enhanced Edition of the Java Web Basic Tutorial introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!