Java data types
1.1 Basic data types
The data type | The size of the | The scope of | The default value | |
The integer | byte | 1 byte | 2 –72 –7– 1 | 0 |
short | 2 – | 2 –152 –15– 1 | 0 | |
int | 4 bytes | 2 –312 –31– 1 | 0 | |
long | 8 bytes | 2 –632 –63– 1 | 0L | |
floating-point | float | 4 bytes | 3.4 e38-3.4 e38 | 0.0 f |
double | 8 bytes | 1.7 e308-1.7 e308 | 0.0 d | |
character | char | 2 – | \ u0000 – u \ FFFF | ‘\ u0000’ |
The Boolean | boolean | True, false, | false |
1.2 Reference data types
Class interface array
Second, operators
2.1 Arithmetic operators
Unary operator | ||||
is | negative | On the 1 | Since the minus 1 | |
+ | – | ++ | — | |
Binary operator | ||||
add | Reduction of | take | In addition to | Take more than |
+ | – | * | / | % |
2.2 bit operators
Decimal to binary
Bitwise and | & | 1 if both numbers are 1, 0 otherwise |
Bitwise or | | | 1 if one is, 0 otherwise |
Exclusive or | ^ | If two numbers are the same, it is 0; otherwise, it is 1 |
non | ~ | 1 to 0,0 to 1, and then to complement (the positive complement is itself, the negative complement is the same sign bit and then take the inverse plus 1) |
Shift the sign to the left | << | Move the low left to fill the 0 |
Move to the right with the sign | >> | The right shift uses the sign extension mechanism, so that if a positive number is present, then 0 is present, and if a negative number is present, then 1 is present. |
Unsigned shift to the right | >>> | Whether it’s positive or negative, we’re going to add 0 |
2.3 Relational operators
Is equal to the | Is not equal to | Less than | Is greater than | No greater than | Not less than | |
= = | ! = | < | > | < = | > = |
2.4 Logical operators
with | or | non | Short circuit and | Short circuit or |
& | | | ! | && | || |
2.5 Ternary operators
Boolean expression? Expression 1: expression 2 True returns the value of expression 1, false returns the value of expression 2
2.6 Assignment operators
=, + =, = =, *, /, % = =, <, < =, > > =, > > > =, & =, | = ^ =
2.7 Instanceof operator
A instanceof B: True if A inherits B or a is of type B. Note: a is an instance, and a must be strongly cast to type B by (B) A, otherwise the compiler will error. B cannot specify generic parameters
2.8 Priority:
priority | The operator | |
1 | (), [] | From left to right |
2 | ! , + (positive), – (negative), ~, ++, — | From right to left |
3 | *, /, % | From left to right |
4 | +(plus), -(minus) | From left to right |
5 | <<, >>, >>> | From left to right |
6 | <, <=, >, >=, instanceof | From left to right |
7 | = =,! = | From left to right |
8 | &(bitwise and) | From left to right |
9 | ^ | From left to right |
10 | | | From left to right |
11 | && | From left to right |
12 | || | From left to right |
13 | ? : | From left to right |
14 | =, + =, = =, *, /, % = =, <, < =, > > =, > > > =, & =, | = ^ = | From right to left |
3. Process control
3.1 Conditional Statements
1. If -else if(A) ① else ② if A is true, execute ①; otherwise, execute ②
2.if-else if-esle if… If (A) ① else if(B) ② else if(C) ③ A = true
3.2 Loop Statements
1.for for(initialization; Boolean-expression; step) statement
2.foreach
for(variable : collection)
statement
4. Do -while do{①} while(A); If A is true, the statement ① is executed until A is false. Unlike while, the statement ① is executed at least once
3.3 Interruption process control
Break, continue, return note: a break is a break from the current loop, and a continue is a break from the current loop
3.4 Multiple select statements
1. Switch switch(A) case B: ①; break; Case C: (2); break; Note: A must be byte, short, char, int and its wrapper class, String(jdk1.7 or above), enum
Fourth, object-oriented
4.1 Three Characteristics
1. Encapsulation combines the attributes and methods of the object into an independent whole, and hides the internal details of the object as far as possible. It only needs to be called without knowing how to achieve the inside, just like the debugging interface, as long as the tube takes parameters and returns results. For example, I want to eat bowl of beef noodles in soup, to which noodle is ok, but if I know there’s a noodle soup is special herbs refining to fill, I’ll be more want to go to that rely on large repair function, if I knew that the soup is how to do, I can take out the formula to sell, for the noodle shop has lost its core competitiveness. 2. Inheriting the subclass inherits the parent class. The subclass can obtain the attributes and behaviors of the parent class and extend new attributes and behaviors according to its own needs, which is conducive to code reuse and program extension. For example, I define a soup-like noodles with attributes: soup, noodles, and then I need to add a big piece of meat to the soup noodles to derive the meat soup noodles or add a big piece of ribs to derive the pork soup noodles. Note: ① A subclass inherits from its parent class. A subclass has all the behavior and attributes of its parent class, but not all the access rights
The modifier | The current class | With the package | Children of class | Other packages |
public | Square root | Square root | Square root | Square root |
protected | Square root | Square root | Square root | x |
default | Square root | Square root | x | x |
private | Square root | x | x | x |
4.3 Overloading and overwriting
Overloading: If it occurs in the same class, the method name must be the same, the parameter type must be different, and the number of parameters must be different. The method return value and access modifier can be overwritten differently. If it occurs in the parent class, the method signature must be the same, the return value type must not be larger than the parent class, the exception must not be larger than the parent class, and the access modifier must not be smaller than the parent class
4.4 the static
Static: static: belongs to this class. Static: belongs to this class. Note: (1). The order of execution: the parent class static variables and static block subclass static variables and static code block The parent class member variables and non-static code block Superclass constructor subclasses member variables and the static block of code a subclass constructor (2) the static method does not advance execution, the static block in each statement when a new object will perform a (3). Because static modifiers are loaded first, static modifiers cannot directly call non-static methods and properties, and therefore cannot use this, super, and cannot be overridden
4.5 the final
Final means final and unchangeable. Modified classes cannot be inherited, modified methods cannot be overridden, and modified variables can only be assigned once as constants
4.6 Call by Value and Call by Reference
The value of the actual parameter cannot be changed. The value of the actual parameter cannot be changed. The value of the actual parameter can be changed
4.7 the Object method
(1) The default clone method is shallow copy (with different reference addresses and attributes of the basic data type), while deep copy is different with different reference addresses, attributes of the basic data type and attributes of the objects. Second, Clone is protected, because the Object class does not know the reference types of its subclasses and cannot make deep copy. Therefore, clone method must be redefined and declared as public to implement deep copy, so as to prevent the Object from being affected by other calls that modify the reference properties of the Object. When using the Clone method, classes need to implement cloneable to give a flag so that no exceptions are raised. Equals and hashCode equals are used to check whether two objects are the same. By default, equals compares memory addresses. We want two different objects with the same property to be equal, so we need to override equals, and why we need to override hashCode, because when objects call equals and return true, their hashCode returns the same value, So you also need to rewrite the hashCode method. Objects with the same hash value are not necessarily equals, and equals returns true to two objects with the same hash value. The toString and getClass methods toString returns a string of object values. Wait (), wait(long), wait(long, int), notify, notifyAll() Wait method: the current thread changes from running state to blocked state and enters the wait queue. It releases the current lock notify to wake up one thread and notifyAll to wake up all threads. ⑤ Finalize GC is used to determine that objects to be collected are not referenced