Javaweb Basic Teaching Java Foundation strengthening version _java

Source: Internet
Author: User
Tags float double naming convention pack reflection set set wrapper

1, the installation and use of MyEclipse

* Eclipse: is a free development tool
* MyEclipse: is a charging plug-in, cracked myeclipse,
* * Installation directory requirements: Can not have Chinese and space
* * * After the installation completed, select a work space, This workspace cannot have Chinese and white space
* Cracked myeclipse *
* run run.bat files, but before running, you must install the JDK, by configuring
the environment variable * myeclipse Use
* Create a project 
- Type Java Project Web project
-Select a dependent JDK, either with a myeclipse jdk, or you can use the installed JDK
* To create a package package
-Cn.itcast.test XX.XX.XX
* Create a class
-class naming convention in the package:
* * First letter to uppercase
For example: TestDemo1 usermanager
* Create method in class
public void test1 (parameter list) {
method body or return value;
} 
-The naming specification of the method the
first letter lowercase for example: Addnum ()
* Define variable
-variable naming convention
* * First letter lowercase, second word to uppercase, such as UserName
* There is also a way to name c27/>** uses Hanyu Pinyin to name Yonghuming Mima *
* can't mix Hanyu Pinyin with English letters
userming the
most basic principle of naming: See the name know what it means
* code needs to be indented
* Run program Run as Java application
debug as Java application

2 debug mode of debug (Breakpoint debug mode)

* Use this mode, debug the program (see the changes in the data in the program)
* Use the first step of debug to set a breakpoint (let the program run to stop in this line)
-show line number
-double click on the left, a dot appears, indicating that a breakpoint is set
* Using the debug as method, run the program
-prompts whether to enter the debugging interface, yes
-at the breakpoint, there is a green bar, which means that the program stops in this line and does not run down
* can let the program go down,
-use the step over shortcut key is F6 (Single-step)
-resume F8: Means debug end, direct down
* * such as the current breakpoint after the breakpoint, jump to the next breakpoint,
* * If there is no breakpoint after the current breakpoint, the program runs directly to the end
* Debug another Purpose
* * View the source code for the program
* * F5 Step Into: Enter to method
* * F7 step return: Back

3, the use of MyEclipse shortcut keys

* Code hint ALT/
* Quick Guide Pack CTRL shift O
* Single Note CTRL/
* Remove Single-line Comment Ctrl/
* Multiline Comment Ctrl shift/
* Remove Multiline Comment Ctrl Shift \
* Delete Line Ctrl d

4, the use of JUnit

* Unit Test
* Test object is a method in a class
* Juint is not part of the javase, you want to use the Import jar package *
* But, in MyEclipse, with the JUnit Jar pack
* First JUnit version 3.x 4.x
* Unit test method, the method naming rule public void Method name () {}
* runs the test method using annotations, on top of the method
* * @Test: The notation method is used for unit testing
---@Test
public void TestAdd1 () {
Testjunit test01 = new Testjunit ();
Test01.testadd (2, 3);
}
-Select the method name, right click Run as---junit test
-When the green bar appears, the method test passes
-when a red-brown bar appears, the method test does not pass
---to run multiple test methods in the class, click the other location in the class, Run as---junit Test
* * @Ignore: means that this method does not unit Test
* * * @Before: Execute the Run * @After in each method
: Run the
* * Assertion after each method (understanding) c21/>-assert.assertequals ("Test expected value", "actual value of Method run")
jdk5.0 new feature
JDK 1.1 1.2 1.4 5.0
* * Generics, enumerations, static import, automatic Disassembly box, Enhanced for, variable parameters
* * Reflection

5, the generic type of introduction

* Why should I use generics? -Generally used on collections * * For example, now put a string-type value into the collection, this time, when this value is put into the collection, the type of loss, can only be object type, this time, such as to the value of the type conversion, it is easy to type conversion errors, how to solve this problem, You can use generics to resolve how to use generics on a collection-common collection list set map-generic syntax collections <String> such as List<string> * Write an object in a generic, String cannot write a basic data type such as INT (* * *) * * Write basic data type corresponding to wrapper class byte--byte short--Short int--Integer long--long float--float double--double Char --Character Boolean--Boolean * uses the three implementations of the generic list on the list ArrayList linkedlist Vector code: @Test public 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 three kinds//general for loop iterator enhancements for//normal for loop for (int i=0;i<list.size (); i++) {String s = list.get (i);
System.out.println (s);
} System.out.println ("=================");
Use the enhanced for for (String s1:list) {System.out.println (S1);}
System.out.println ("=================");
Using iterators to traverse iterator<string> it = List.iterator (); while (It.hasnext ()) {System.out.println (It.next ());} * Job 1:arraylisT LinkedList Vector These three differences * use generic code on set://generics use the Set set @Test public void Testset () {set<string> set = new HASHSET&L T
String> ();
Set.add ("www");
Set.add ("QQQ");
Set.add ("zzz");
Set.add ("QQQ");
There are several ways to traverse a set two//iterator enhancements for//use enhanced for traversal for (String s2:set) {System.out.println (s2);}
System.out.println ("=================");
Use iterators to traverse iterator<string> it1 = Set.iterator ();
while (It1.hasnext ()) {System.out.println (It1.next ());}} * Using the generic-map structure on the map: Key-valu form code://Using generics @Test public void TestMap () {map<string,string> map = new HASHMAP&L on map T
String,string> ();
Map.put ("AAA", "111");
Map.put ("BBB", "222");
Map.put ("CCC", "333"); Traversal map There are several ways to traverse the two//1, get all the keys, get value through key by using the Getting method//2, get the key and value relationship//Use the first way to traverse//Get all the key set<string> sets =
Map.keyset ();
Iterate over all key returns set for (string key:sets) {//Get value String value = Map.get (key) via key;
System.out.println (key+ ":" +value);}
System.out.println ("=============="); Get the relationship between key and value Set<entry<striNg, string>> sets1 = Map.entryset ();
Traversal sets1 for (entry<string, string> entry:sets1) {//entry is a key and value relationship String Keyv = Entry.getkey ();
String Valuev = Entry.getvalue (); System.out.println (keyv+ ":" +valuev);}

6, generics use in the method

* Defines an array that implements the exchange of the array elements at the specified position with the
same logic, but with different data types, this time using the generic method
* *
/* * Using a generic method to define a type using uppercase letters to denote T: this t represents any type
* Written before void before the return value <T>
* ======= indicates that a type is defined with this type of t
* * Can be used below the t * */public
static <T> void SW AP1 (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 array

7, generics in the use of the class (understand)

* Define a type on a class that can be used directly within a class by using the * public class
testdemo04<t> {
//T's type T AA in the class
;
public void test11 (T bb) {}
//write a static method that is defined above the class and cannot be used with public static
<A> void test12 (A cc) {}
}

8. Introduction to Enumeration

* What is an enumeration?
* * Need to take a value within a certain range, this value can only be any one within this range.
* * Reality scene: traffic lights, there are three colors, but only three colors can be lit each one
* use a keyword enum
* * Color3 {
red,green,yellow;
}
* The constructor of the enumeration is also private
* Special enumeration operation (Understanding) *
* There are constructors in the enumeration class *
* There are parameters in the constructor, and you need to write parameters on each instance *
* * There are abstract methods in the enumeration class
* * This abstract method is overridden in each instance of the enumeration

9, the enumeration of the operation of the API

* * Name (): Returns the name of the enumeration
* * Ordinal (): The subscript for the enumeration, subscript starting from 0
* * valueof (class<t> enumtype, String name): Object being enumerated
* * There are two other methods, both of which are not in the API and generate two methods when compiling
* * * valueof (String name) Transform Enumeration Object *
* VALUES () Get all enumerated object Arrays
* Practice: Enumerating objects, Enumeration object subscript, enum object name representation conversion
-//Know enumerated object, get enumeration name and subscript
@Test public
void Test1 () {
//Get enumerated object
Color100 c100 = color100.red;
Enumeration name
String name = C100.name ();
An enumerated subscript
int idx = c100.ordinal ();
System.out.println (name+ "" +idx);
}
-//know the name of the enumeration, get the enumerated object and subscript
@Test public
void Test2 () {
String name1 = "GREEN";
Get the object
Color100 c1 = color100.valueof (name1);
Enumerate subscript
int idx1 = c1.ordinal ();
System.out.println (IDX1);
}
-//knows the subscript of the enumeration, gets the object and name of the enumeration
@Test public
void Test3 () {
int idx2 = 2;
The object to be enumerated
color100[] cs = color100.values ();
According to subscript get Object
Color100 C12 = cs[idx2];
Gets the name of the enumeration
String name = C12.name ();
SYSTEM.OUT.PRINTLN (name);

10. Static import (understanding)

* Can be in the code, directly using static import mode, import static method or Constant * Import a 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 Unboxing

* Box * *
to convert the basic data type into a packing class
* unboxing * *
to convert the wrapper class to the basic data type
* *//Automatic boxing
Integer i = ten;
Automatic unboxing
int m = i;
* * Inside the jdk1.4 how to implement boxing and unboxing-///within
jdk1.4 implement unboxing public
void Test1 () {
//boxed
Integer m = new Integer (a); 
Unboxing 
int a = M.intvalue ();
}
* * JDK is backward
-compatible-such as the code written in jdk1.4, this time to 5.0 can also run
* * Practice: backward
= = The result of execution is to invoke dosomething (double m)
= = First of all in jdk1.4 inside definitely call this method, if call the following method, need type conversion, but jdk1.4 can not implement automatic unboxing
= = because the JDK is backward-compatible, so, in the jdk1.4 call this method, in jdk5.0 still will call this method Public
static void Main (string[] args) {
dosomething (a);
}
public static void DoSomething (double m) {
System.out.println ("double ...");
}
public static void DoSomething (integer a) {
System.out.println ("Integer ...");
}
* * Remember: eight basic data types corresponding to the wrapper class
* int---Integer
* char---Character

12. Enhance for loop (* * *)

* Syntax for (traversal value: the set to traverse) {}-for
(String s:list) {
System.out.println (s);
}
* Use scene: Array, the collection that implements the Iterable interface can use the enhanced for loop * Use the enhanced
for loop on the collection to implement the iterator interface with the enhancement for loops
, so you can use the enhanced for loop
The map cannot use the enhanced for loop and does not implement the iterator interface, so you cannot use the enhanced for loop
* To enhance the For loop appearance: to override
the iterator * * Enhanced for Bottom is the iterator implementation

13. Content Supplement

(1) Generic erase
* First generics only appear in the source code phase, and when compiled, generics do not exist
(2) Practice: Implement a generic method, accept an array of any type, reverse all elements of the array
code public
Static < t> void Reverses (t[] arr1) {
* * Basic idea: Swap the first element and the last element to place the second element and the penultimate element ....
* Swap Length/2 *///
traversal 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

* What the variable parameters can be applied to:
* Implementing the addition of two numbers, adding four numbers of three numbers-
-If you implement multiple methods where the logic is basically the same, the only difference is the number of parameters passed, you can use the variable parameter
* Definition method data type for variable parameters ... The name of the array is
understood as an array that stores the arguments passed over
-code public
static void add1 (int ...). Nums) {
//nums understood as an array that stores the arguments passed over
//system.out.println (nums.length);
int sum = 0;
Traversal array for
(int i=0;i<nums.length;i++) {
sum + = Nums[i];
}
SYSTEM.OUT.PRINTLN (sum);
* Note the place
(1) The variable parameters need to be written in the parameter list of the method, cannot be defined separately
(2) in the parameter list of the method can only have a variable parameter in the parameter list of a variable parameter
(3) method, which must be placed at the end of the argument
-add1 ( int A,int ... Nums)

15, the principle of reflection (******** understanding ********)

* Used in some of the more common code
* Behind the framework learned, most of which are implemented using reflection
* In the framework development, is based on the configuration file development
* * In the configuration file configuration of the class, can be reflected in the class all content, You can let a method in a class perform
all the content in the * Class: Properties, construction methods without parameters, structured methods with parameters, common methods
* Drawing analysis of the principle of reflection
* First you need to save the Java file to your local hard disk. Java
* Compile the Java file into a. class file
* Using the JVM, loading class files into memory through classes loading
* All things are objects, class files use class classes in memory to represent
* when using reflection, First you need to get to class classes, you get this class, you can get all the contents of the class file
-including the property construction method Common method
* Property through a class Filed
* Construction method through a class constructor
* Normal method through a class

16, using the reflection of the operation of the class without parameters of the construction method (* * will write * *)

* First gets class
-//Get 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, do not use new, how to get?
-//Get class
Class C3 = Class.forName ("Cn.itcast.test09.Person");
Gets the instance of person class
P = (person) c3.newinstance ();
* Code
//Operation no parameter construction method
@Test public
void Test1 () throws Exception {
//Get class
class C3 = Class.forName ("Cn.itcast.test09.Person");
Gets the instance of person class
P = (person) c3.newinstance ();
Set Value
p.setname ("Zhangsan");
System.out.println (P.getname ());
}

17, the use of reflection operations have parameters of the construction method (* * will write * *)

The constructor method with parameters
@Test public
void Test2 () throws Exception {
//Get class
Class C1 = Class.forName (" Cn.itcast.test09.Person ");
Using a constructor method with parameters
//c1.getconstructors ()//Get all of the construction methods
//passes are parameters of the constructor method inside the parameter type, the type uses class form to pass
constructor cs = C1.getconstructor (string.class,string.class);
Create a person
instance person
p1 = (person) cs.newinstance ("Lisi", "M")
by using the constructor method with parameters by setting the value//. System.out.println (P1.getid () + "" +p1.getname ());
}

18, using Reflection Operation Properties (* * will write * *)

*//Operation Name property
@Test public
void Test3 () {
try {
///Get class class
C2 = Class.forName (" Cn.itcast.test09.Person ");
Get the name attribute
//c2.getdeclaredfields ()//To get all the attributes
//Get the instance person of the person class
P11 = (person) c2.newinstance ();
This method gets the property, which is the name of the property
Field f1 = C2.getdeclaredfield ("name");
Operation is private property, do not let operation, need to set can manipulate private property setaccessible (True), can manipulate private property
f1.setaccessible (true);
Sets the name value set method, two parameters: an instance of the first parameter class, 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, the use of generic operation of the common method (* * will write * *)

* Use the method class to represent common methods
* Code
//Operation common methods, such as Operation SetName
@Test public
void Test4 () throws Exception {
// Get class class
C4 = Class.forName ("Cn.itcast.test09.Person");
Get person Instance person
P4 = (person) c4.newinstance ();
Get the Common Method
//c4.getdeclaredmethods ()//Get all the Common method
//Pass two parameters: first parameter, method name, second parameter, method inside the type of parameter
m1 = C4.getdeclaredmethod ("SetName", string.class);
Let the SetName method execute, Execute Set value
//Use Invoke (P4, "Niuqi"), pass two arguments: first argument, person instance, second parameter, set value
//After executing the Invoke method, equivalent to The SetName method is implemented, while a value is set by this method Niuqi
M1.invoke (P4, "Niuqi");
System.out.println (P4.getname ());
}
*//The Private method of the operation, need to set the value is True
*//m1.setaccessible (TRUE);
* When the method of the operation is a static method, because the static method invocation method is the class name. Method name, no instance of class
* using reflection to manipulate the static mode is also not required instance
* In the first parameter of the Invokie method, write a null
- M1.invoke (NULL, "Niuqi");

The above is a small set to introduce the Javaweb basic Teaching Java Foundation to strengthen the version, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.