1.java Order of execution
Java (compile) Java (run)
Write source-----------> loadline file-----------> Execute
2. Writing source attention
A. The class name and file name match.
B. The parentheses should appear in pairs.
The difference between 3.println and print
PRINTLN will break.
Print does not
4. Escape character
\ n = line break
\ t denotes a space
5.java comments
: Single-line comment
/* * *: Multi-line Comment
/** * * indicates a text comment
6.java Code Specification
A. Class name to be decorated with oublic
B. Write only one statement in a row
C. Note the location of {}
D. Code hierarchy relationship indentation (Tab key)
variables, data types, and operators
2.1 Variables
Variable: A data storage space representation (pronoun)
2.2 Basic syntax for variables
Data type variable name//declaration variable = variable value;//Assignment
2.3 Use of variables
A. Variables must be declared and initialized enough to be used.
B. Variable names cannot be duplicated.
2.4 Common data types
Int (integer)
Double (dual-precision floating-point number)
char (character)
String (String)
Supplement : 8 Basic data types
Byte (byte type)
Short (shorter integer type)
char (character type)
int (integral type)
Long (integer)
Float (single-precision floating-point type)
Double (dual-precision floating-point type)
Boolean (Boolean)
2.5 variable naming rules
variable name : consists of numbers, characters, underscores, dollar signs,
But you can't start with numbers.
2.6 Assignment operations
Variable name = expression;
2.7 Arithmetic operators
+,-,* (multiply),/(except rounding),% (remainder)
+ +, (self-increment) equivalent to itself = itself +1
--, (self-subtraction) equivalent to itself = itself-1
2.8 Type Conversions
2.8.1 classification
A. Automatic type conversion
B. Forcing type conversions
2.8.2 Automatic type conversion
Large type = Small type
2.8.3 Coercion Type conversions
Small type = (small type) Large type
Note: Forcing type conversions is likely to overflow
2.9 Relational operators
> (greater than),< (less than)
= = (equals),! = (not equal to)
>= (greater than or equal) <= (less than or equal)
Note: = = equals, = =: Assignment
3.0 Boolean Type
Only true and false two values
3.1 Operator Arithmetic Precedence
() > Arithmetic operators > Relational operators > Logical Operators > Assignment Operations (=)
+,-..., < | | (or) && (with)! Non-
Select structure
The first type:
if (condition) {
code block
}
Explanation: If the condition result is true, then execute the code block, otherwise do not execute
The second type:
if (condition) {
Code Block 1
}else{
Code Block 2
}
Explanation: If the condition result is true, then execute code block 1, otherwise execute code block 2
The third type :
if (condition) {
Code Block 1
}else if (condition 2) {
Code Block 2
}...... {
...
}else{
Code block n
}
Explanation: If the condition 1 result is true, then execute code block 1
If condition 1 is false, the next condition is executed
Judge, repeat the above procedure in turn.
Note :
Determine if strings are identical with str1.equals (STR2)
Judging the same number with NUM1 = = num2
2.SWITCH...CASE selection Structure
switch (expression value) {
Case value 1: statement; break;
Case value 2: statement; break;
.....
Default: statement n;
}
Explanation: If the value of the expression is value 1, then statement 1 is executed,
If the value of the expression is a value of 2, then statement 2 is executed,
........
If the expression does not find a match to the value from top to bottom, then the statement n after default is executed.
Note :
1). The expression value is an integer type, character type. jdk1.7 can be sting type later
2). Break: Represents the end of the expression as a result of the entire Swtich statement.
3). If a value is matched and no break is performed, then the statement from the match to that value
At the beginning, each of the following statements executes, knowing that the entire program ends up with the latter encountering a break.
Loop structure
1. Whileloop
while (condition) {
code block
}
Explanation: If the structure of the condition is true, then the code block is executed
When a block of code finishes executing, the condition is executed,
Loop in turn, knowing that the result is false and the entire loop ends.
2. Program Debugging
First step: Set breakpoints (left-click)
Step two: Debug mode execution code
Step three: Perform barcode debugging (f6)
Fourth step: Check the code (see changes in variable values)
Note : The Java View and debug view can be toggled in the upper right corner.
3.Do...while Cycle
do{
code block
}while (condition) {
code block
}
explanation : Execute the code block first and then make the conditional judgment.
If the condition is true, continue to execute the code block
Loop in turn, knowing that the condition is false and the entire loop ends.
Note :
The scope of a variable: from the beginning of the variable declaration to the end of the block of code it contains.
4. Whileand do....while differences
A. The order of execution is different
The loop body in the b.do...while will be executed at least once;
The loop body in the while is not executed when the result is false.
5. For Loop
Grammar:
for (initialization expression; conditional expression; cyclic variable change expression) {
Loop body
}
Initialization expression: Used to initialize the value of a loop variable
Conditional expression: Executes the loop body if true, otherwise exits
Loop variable expression: used for control of loop variables such as i++.
Execution order:
First step: Initialize an expression (only once)
Step two: conditional expression; If true, the third step is executed, otherwise the loop is exited.
Step Three: Loop body
The fourth step: loop variable expression, after execution, then perform the second step, loop in turn.
Attention:
All three expressions in the For loop can be omitted.
If the second expression is not written, its result is true.
Array
1. declaration definitions for Arrays
array type [] variable name = new data type [length];
Example: int [] ary = new int [5];
2. value, assign value.
Value: array name [subscript];
int a = ary [1];
Assignment: variable = array name [subscript];
Example: ary [1] = 10;
3. iterating the array
The length of the array: array name. length;
for (int i=0;i< array name. length;i++) {
Array name [i]: Access value for each element
}
4. Common Exceptions for Arrays
ArrayIndexOutOfBoundsException//array subscript out of bounds
The above error occurs when the subscript of an array is accessed beyond 0~length-1.
Note : Array subscript range: 0~length-1
5. Common methods of arrays
Arrays.tostring (array name); displaying array contents
Arrays.sort (array name); Array sorted in ascending order
6. Post- traversal
For{int i = ary.length-1; i>=0; i--
Ary[i];
}
7. Compare the size of a string
If A.comparetoignorecase (b) >0 is true then A is greater than B.
If A.comparetoignorecase (b) <0 is true then A is less than B.
If A.comparetoignorecase (b) ==0 is True then a equals B.
8. Breakand Continue
Continue: Continue (indicates the end of this round loop, into the next loop)
Break: Terminate, End (indicates terminating the current loop structure)
Note : Multi-layer loops only work on direct loops
Classes and objects
1. Objects and Classes
A class is an abstraction of an object that is a concrete case of a class.
Example: People are classes, Xiao Ming is the object.
2. create an Object
Syntax: Class Name Object name = new class name ();
Example: person p = new person ();
Note : The class name is also a data type. Grammar can also be written.
Data type variable name = new data type ();
3. Syntax for classes
public class class Name {
Attribute---> Characteristics
Method---> Behavior
}
4. Defining Attributes
class {
The data Type property name of the property;
Example: int age;//denotes age
}
Property Access: Object name. property name;
Property Assignment: The object name. property name = value;
5. access to methods:
The name of the object. Method Name ();
No parameter method
1. Definition of the method
Public return value Method name () {
Method body
}
Note :
If there is no return value, then the return value type is void.
If there is a return value, you must return the value with return, and the data type of the value must be the definition method
Data type at the time.
The function of return:
A. Return value
B. End method. (similar to break)
2. Note When writing methods
1th: When you define void as the return value type, you cannot use the return+ return value.
2nd: The method cannot return multiple values.
3rd: Methods cannot be nested.
4th: The program logic code cannot be written directly outside the method.
3. The difference between a member variable and a local variable
A. Different scopes
The scope of a local variable is limited to the method that defines it
The scope of a member variable is visible within the entire class
B. Different initial values
Java will give the member variable an initial value
Java does not give initial values to local variables
Note :
A. Local variable names cannot be duplicated in the same method
B. In different methods, local variable names can be repeated
C. In the same class, the member variable names and local variables can be the same, generally with local variables. (Nearest principle)
4.java.lang.NullpointerException (null pointer exception)
Cause: The value of the object is null, and the secondary object is manipulated.
Solution: Locate the data source for the secondary object and view the code for the new object
Whether or not to write, to solve the problem for different reasons.
Note :
Write any code
The execution process of the 1th code.
2nd: Where the value comes from.
recommendation :
1. Don't write the code first, write the steps first.
2. Don't focus on implementation details
A method of participation
1. definition of the method
Public returns a value type method name (argument type 1, parameter name 1, argument type 2, parameter name 2, ...). Parameter type N) {
Method body
}
2. parameter-aware invocation
A. If a method can be called directly in the same class.
B. If a different class, the method must be called through the object,
The name of the object. Method Name (argument, argument 2 ... )
Note :
1) The data type of the argument, the number of parameters, the order of the parameters should be consistent with the image.
2) Call the method that has the return value, generally accept the return value, and make corresponding processing.
3. Package
A. Package: folder (starting from src directory)
Example: Oo.day01
B. Package naming conventions
The package consists of lowercase letters and cannot start or end with a dot.
C. When using classes from different packages, you must import the class into the
The specified class, (if it is a class in the same package, does not require Import,java to be imported automatically).
4. Definition of String
String str = "Content"
String str = new string (); Content is null
String str = new String ("content");
5. java.lang.*
The package where the string is located is Java.lang.
Note : All classes under the Java.lang package are not required for import when we use them.
6. Common Methods
Str.length (); The length of the string
Str.indexof (STR1); Sub-string str1 the occurrence of the subscript. (from the back to find).
Str.lastindexof (STR1); Sub-string str1 (from back to front).
Str.substring (begin); Intercepts the substring from the subscript begin to the end.
Str.substring (Begin,end); Intercept the substring from the subscript begin to the subscript end.
Str.trim (); Remove the end and end blanks
Str.tolowercase (); Returns the lowercase of a string
Str.touppercase (); return string Uppercase
7. Steps to abstract classes from reality
First: Find the classification (analyze the Class)
Second: Find out the characteristics of the class (analyze the related properties of the class)
Third: Find out the behavior of classes (methods for analyzing classes)
8. Constants (variable values that often occur)
Syntax: public static final data type variable name = variable value;
Note :
All letters of the variable name are capitalized, and multiple words are split with an underscore (_).
Example: public static final String sex_male= "Q Tsai";
how constants are accessed :
Class name. constant name;
Construction Method (Special method used to create an object without writing it will be constructed by default)
1. Syntax :
public class name (argument list) {
Method body
}
2 Use
Class Name object New constructor method name (parameter);
Example: Dog d = new Dog ();
3 function of the construction method
A. Create an object.
B. Initializing values (attribute values)
4 The difference between a construction method and a common method:
A. Construction method does not return a value type (return ...). )
B. The method name of the construction method is the class name, and the normal method can be arbitrarily taken.
C. Call to construct method: New constructor Method name (parameter)
invocation of the normal method: Object name. Method Name (parameter)
Note : When you do not write a constructor, Java provides the default parameterless constructor.
Cases:
Public dog{
}
Method overloading
1. Method Overloading :
The parameter list is not the same as the method name.
Note : Overloading is independent of the return value type and the access modifier.
2.Static and final
Static: Statically
You can directly use the class name by using the static Modified property. Method Name Access
Final: The Ultimate
With the final modified property, it cannot be changed after its value is initialized
Note : Static can no longer modify local variables in methods
Add:
After + +, first put the value of itself as the value of the expression, and then itself +1;
Example: i++
The first + +, the first of itself plus 1, and then the value as the value of the expression;
Example: ++i
After--first put the value of itself as the value of the expression, and then itself-1;
Example: i--
Before--the first itself-1, and then the value as the expression value;
Example: I.
Packaging
1. Encapsulation : Hide the inner details of the class
2. Package Steps :
The first step is to privatize the properties. (private)
Step Two: Provide the Getter/setter method (Getxxxx (), setxxxx ()).
Step three: Add the control statement in the Getter/setter.
3. This keyword
This: Represents the current object.
Call properties: this. Property name
Calling method: this. method name ();
This (): represents the call constructor.
Note : This () must be written on the first line of the constructor.
Inheritance (extends)
1. Inheritance : Genetics
2. Syntax
public class subclass extends parent class {
code block
}
Example: public class Dog extends Pet {
code block
}
3. Characteristics of inheritance
A. Subclasses can inherit non-private properties and methods of the parent class
B. Cannot inherit a constructor method
C. Methods and properties that inherit the default permission modifier, subclasses and parent classes must be in the same package
4. Super (parent Class)
Super. Attribute name; Represents a property that accesses a parent class
Super. Method name (parameter); Represents a method that accesses a parent class
Super (parameter);//The constructor that accesses the parent class;
Note : When super () accesses the parent class constructor, it must be written on the first line;
Abstract class
1. Abstract class
Syntax: Public abstract class name () {};
2. Abstract Methods
Syntax: Public abstract return value type method name (parameter list);
Example: Public abstract void print ();
Note : Abstract methods have an abstract modification, and the abstract method has no method body.
3. characteristics of abstract classes and abstract methods
A. Both are modified with an abstract
B. There is no method body for abstract methods
C. Abstract classes cannot be instantiated, and inheritance must override all abstract methods in an abstract class
Note :
1. Abstract methods are not necessarily abstract in class
2. Classes containing abstract class methods must be abstract classes (interfaces)
Rewrite
1. overrides are subclasses overriding methods that define the parent class
2. Rewrite rules
A. The method name and parameter list are the same.
B. Modifier access permissions The subclass is the same as the parent class or larger than the parent class.
C. Subclass of return value type subclass and parent class or return value type of parent class
D. The exception is the same as the parent class or has a smaller scope
When overridden :
Subclasses have to be consistent with the parent class.
method Overrides :
The same location, the same method name, the same argument list, the same return value or its subclasses, the access modifier can not be more strict than the parent class
method overloading : Same location, same method name, parameter list not identical, return value independent, access modifier Independent
Final
The final decorated class cannot be inherited
The final decorated method cannot be overridden
Final-Modified properties cannot be modified
Polymorphic
1. polymorphic meaning : Multiple forms of a thing
2. polymorphic Performance
Behavior (method) Polymorphism: Overrides and overloads.
Referencing polymorphism (dynamic binding): This behavior is called dynamic binding when the compile-time type is inconsistent with the runtime.
Example: Parent class = Child class.
Pet p = new Dog ();
3. upward transformation and downward transformation
Upward Transformation: Parent class = subclass;
Downward transformation: Subclass = (subclass) parent class;
Example: Dog D = Dog () p;
Note :java.lang.ClassCastException: Type conversion exception.
The run-time type of the parent class is inconsistent with the type of the subclass to be turned
Note : Inheritance relationships exist for referencing polymorphism
instanceof
Syntax: Object a instanceof type B;
Determines whether the type of object A is type B and returns true if it is, otherwise false.
Example: D instanceof Dog
Two forms of multi-state implementation
A. Using a parent class as a method parameter to implement polymorphism
public void Play (Pet p) {}
B. Using a parent class as a method return value to implement polymorphism
Public Pet getpet (int type) {}
Interface
1. Syntax
Public interface Interface name extends Interface 1, interface 2: {
1. Constants
2. Abstract methods
}
2. features
A. Using interface to modify
B. Interfaces can inherit interfaces, multiple inheritance
C. Only constants and abstract methods can be defined within an interface
D. The interface cannot be instantiated, but can be used as a reference for subclasses
E. Any subclass that implements an interface must implement all of its abstract methods
foreach Loop and ...
...: A parameter appears in a parameter, indicating a variable
foreach Loop:
Syntax: for (int subscript = 0;i < array length; i++) {array element value array [i]}
Syntax: for (element value of array element type array: array) {}
Abnormal
1. exception: The process of the execution of the program to affect the normal operation of the phenomenon.
2. exception Syntax
try{
code block
}catch (Exception type E) {
code block
}catch (Exception type 2 e2) {
code block
}... {
code block
}finally{
code block
}
Note :
Try: a block of code that represents an exception that might occur.
catch: Grab exceptions, and handle them, you can grab multiple exceptions,
The range of exceptions is crawled from small to large, and only the first matching exception type is executed.
finally: Eventually, the code block in finally executes regardless of whether an exception occurs.
In addition to the virtual machine Stop (system.exit (1)) in this case.
Note :
The execution order of finally and return:
Executes return, saves the returned result in the returned result area, does not return, and then executes finally,
Finally, the results saved in the result area are returned to the caller.
3.throws declaration Exception
A. It is the current method that cannot resolve this exception and must be given to the previous caller for processing.
B. Syntax
Access modifier return value type method name (argument list) [throws Exception]{}
Java Exception System
Throwable
Error: (Errors handled by a program that cannot be passed)
Exception: Errors that can be crawled or thrown with the program.
Check for exceptions (non-runtime Exceptions): Exceptions that occur during compilation
SqlException,
IOException,
ClassNotFoundException
Non-check exception (run-time exception Runtimee)
NullPointerException,
Arrayindexofboundsexception,
ClassCastException,
Note :
Checked exception, which is required for processing
Run-time exception, can not be handled
Importing third-party jar packages
First step: Right-click the project name, create a new folder (Fold), the name is lib.
Step Two: Copy the third-party jar package to the Lib directory.
Step Three: Right-click the project name---->properties
---->java Build Path
---->libraries
---->add jar
----> Select the jar package you want from the project's Lib directory
----> OK.
Using log4j
First step: Import the log4j jar package
Step Two: Write the configuration file
Note :
FileName and file path
(development) (build)
Log4j.rootlogger=debug | Info
Step Three: Use log4j
A. Importing the Log4j class
Import Org.apache.log4j.Logger;
B. In the specified class, write properties
private static Logger Logger = Logger.getlogger (Specifies class name. Class);
C. Writing a log on a specified line
Logger.debug ("Log Information");
Logger.info ("Information");
Collection Frame
Collection Map
List Set HashMap
ArrayList LinkedList HashSet
1. List Method
list< element type > list = new linkedlist< element type > ();
List.add (Element); add element
List.add (index, Element); Inserts an element into the specified position
List.remove (subscript); Delete the element that specifies the subscript
List.remove (Element); Delete the specified element
List.get (index); Gets the specified subscript element
List.contains (Element); Whether the collection contains the specified element
2. the difference between Collection,list,set
Collection: not unique, unordered
List: not unique, orderly
Set: Unique, unordered
3. the difference between ArrayList and LinkedList
A. All implementations of the list
B.arraylist is a kind of edge-length array method, and the query efficiency is high.
LinkedList is a list-mode implementation that increases and removes efficiency.
4. List Common methods
List <Object> list = new arraylist<object> ();
List.add (OBJ); add element
List.get (subscript); Gets the specified subscript element
List.size (); Length
List.remove (OBJ); Delete the specified element
List.contains (OBJ); Whether to include the specified element
5. Map Common methods (key-value pairs store data)
map<object,object> maps = new hashmap<object,object> ();
Maps.put (Key,value); add element
Maps.get (key); Gets the value of the specified key
Maps.size (); Length
Maps.remove (key); Delete the specified element
Maps.containskey (key); Whether to include the specified key
Note :
Key is unique, and if repeated then the back will overwrite the previous one.
Value can be duplicated.
6. the similarities and differences between Vector and ArrayList
The principle of implementation, the same function, can be interoperable
Vector thread safety, ArrayList heavy speed, light security, thread non-secure
When the length grows, the vector grows by default by a factor of 50% ArrayList growth
Similarities and differences of Hashtable and HashMap
Hashtable inherits the Dictionary class, HashMap implements the map interface
Hashtable thread safety, HashMap thread non-secure
Hashtable null values are not allowed, HashMap allows null values
First knowledge of Java