preface
Next, start sorting out the contents of Java. Because the contents in front of the array are basically similar to those in many languages, we will simply sort out the contents in one chapter, mainly starting from the object-oriented notes
1. Understanding java
java features
Java has the characteristics of simplicity, object-oriented, distributed, robustness, security, platform independence and portability, multithreading, dynamics and so on.
JVM: JVM is the abbreviation of Java Virtual Machine. It is a fictitious computer, which is realized by simulating various computer functions on an actual computer.
JDK: JDK is the abbreviation of Java Development Kit and the development kit of Java.
JRE: JRE is a Java runtime environment, not a development environment, so it does not contain any development tools (such as compiler and debugger), but only for users using Java programs.
install
How to install
Configure environment variables
How to develop java source programs:
Write source code - > compiler translates source code into class file - > run class file
Command: javac source file name java
java_ Class name
use Dos Command line execution(In the corresponding folder) javac HelloWorld.java //Generate class file java HelloWorld //Executive document The format must be such that other errors are reported
Execution process
Source code Java file
Compile generation class file(Bytecode file),Only such documents can be used JVM function. Load loads the file into memory, then reads the data in the byte stream and stores it in memory JVM Method area Execute search main()Method, the object instance will be placed java Heap generation Java Stack, and then in and out of the stack
Notes for writing source code:
The source file can contain multiple classes, but there can only be one public modified class at most, and the name of the public modified class is consistent with the name of the source file. The main method must be in the public modified class. If there is no public modified class, the file name can be the same as any type.
Escape character
2. Data type, variable, operator
Classification of data types:
Basic data types: three types and eight types
Integer: byte short int long
Decimal: float double// When defining a variable, float should be followed by f or F, because there is a decimal point, and the default is double
Non numeric value: both false and true of char boolean //boolean are lowercase, which is easy to remember as uppercase
Reference data type: except for the eight basic data types, all the others are reference data types
Naming rules
It contains numbers, letters, and $symbols, but the beginning cannot be a number or a keyword in java. Try to see the name and meaning as much as possible.
Variable or method naming: the first letter of the first word is lowercase, and the first letter of the following word is capitalized, which meets the hump naming rules (some people are used to separating words with underscores, and all letters are lowercase)
Constant: all letters are capitalized and words are separated by underscores
Class or interface naming: the first letter of each word is capitalized according to Pascal naming rules
Package naming: all lowercase
Key words: all letters are lowercase, some words that java has used.
Literal value: true false null
Reserved word: goto const
Use of variables:
Declare that [local variable] can only be used after assignment (if it is not assigned, the default value will be automatically assigned)
special:
Static variable static:
cover static Modified member variables are called static variables. Static variables have the following two characteristics: a.The data of static variables is shared by all instance objects in this class; b.If the access permission of the static variable is higher than private,Then the static variable can be passed through "class name".Variable name is accessed directly.
Constant final:
The variable modified by final is a constant, that is, once defined, an error will be reported after modification.
Operator:
Arithmetic operators: +, -, *, /,%, + +, –
Relational operators >, <, > =, < =, = ==
Logical operators: & &, |!
Bitwise operators: <, > >, > >, ^, &, |~
int a = 60; /* 60 = 0011 1100 */ int b = 13; /* 13 = 0000 1101 */ int c = 0; c = a & b; /* 12 = 0000 1100 */ // &It is 1 when binary is 1 System.out.println("a & b = " + c );//Output: A & B = 12 c = a | b; /* 61 = 0011 1101 */ //|Or if one is 1 in binary, it is one System.out.println("a | b = " + c );//Output: a | b = 61 c = a ^ b; /* 49 = 0011 0001 */ //The difference is 1 System.out.println("a ^ b = " + c ); c = ~a; /*-61 = 1100 0011 */ //Contrary to the original (to put together a group of four) System.out.println("~a = " + c ); c = a << 2; /* 240 = 1111 0000 */ //Overall shift left by two System.out.println("a << 2 = " + c ); c = a >> 2; /* 15 = 1111 */ //Shift the whole right by two digits System.out.println("a >> 2 = " + c ); c = a >>> 2; /* 15 = 0000 1111 */ System.out.println("a >>> 2 = " + c );
supplement
Indicates shift to the right. If the number is positive, the high position is supplemented by 0; if it is negative, the high position is supplemented by 1;
It means unsigned right shift, also known as logical right shift, that is, if the number is positive, the high position is supplemented by 0, and if the number is negative, the high position is also supplemented by 0 after right shift.
The expression is:
result = exp1 >> exp2;
result = exp2 >>> exp2;
Indicates that the number exp1 is moved to the right by exp2 bits.
For example:
res = 20 >> 2;
The binary of 20 is 0001 0100, and after shifting 2 bits to the right is 0000 0101, the result is res = 5;
res = -20 >> 2;
-The binary of 20 is the complement of its positive number plus 1, i.e. 1110 1100. After shifting 2 bits to the right, it is 1111 1011, and the result is res = 211;
For the > > > symbol:
res = 20 >>> 2; The results are the same as > >;
res = -20 >>> 2;
-The result of 20 is res = 59; That is 1110 1100, 0011 1011 after moving 2 bits to the right
Supplement:
< < is the shift left operator corresponding to > >, which means to move exp1 to the left by exp2 bits and fill 0 in the low order. In fact, moving n bits to the left is equivalent to multiplying 2^n.
Move left without < < operator!
Ternary (meta) operator (conditional operator): (?:)
Assignment operators: =, + =, - =, * =, / =,% =, < =, > > =, & =, ^ =|=
Instanceof operator: instanceof (judge whether a variable or reference type belongs to a type)
Operator priority
3. Process statement
1) If (three)
1,if()
2,if()...else...
3,if()...else if()... else...
2)switch
switch (variable){ case value: break; case value: break; default: break; }
be careful:
1. Equivalent judgment
2. break usage
3,default
3) while (two)
1,while(){
}
2,do{
}while()
4) for (two)
1. Similar to: for (int i = 0; I < 10; I + +) {}, the content separated by semicolons can be saved, but it must be reflected in other places
2. Similar to: for(int a:b), B is an iteratable type, such as an array
break,continue,return
break: end this loop statement
continue: end this cycle and proceed to the next cycle
return: end this method body
4. Array
Once the length of the array is defined, it cannot be changed. If it needs to be changed, the idea of dynamic array can be considered, which is explained in detail in the column of algorithm and data structure Algorithm and data structure_ Chapter 2 linear structure + sequential storage
1) One dimensional array
Several types of definitions:
1: Most commonly used data type[] Array name = new data type[length]; Example: int[] age = new int[3]; 2: data type[] Array name; Array name = new data type[length]; 3: data type[] Array name = {Value, value, value} //If you provide several values, the length of the array is several
View array length: array name length; Note that there are no parentheses. It's always easy to get confused with parentheses
- For the String object defined by the String class in Java, use length() to find its length.
- If you want to find the length of an array, you can use their own attribute length.
- That is, the array is not added, and the string is added
Note: error prone definition format
Error 1: int[] age; age = {1,2,3}; Error 2: int[] age = new int[3]; age = {1,2,3};
1) Two dimensional array
Define format:
1,int[][] a = new int[2][3];//Commonly used 2,int[][] a = new int[2][];//Commonly used 3,int[][] a = {{1},{1,2},{2,3,4}};
Draw a picture here to understand
A two-dimensional array is defined. The address is saved in the stack. A piece of memory is opened up in the heap to save the address, that is int[][] a = new int[5][],The first piece of memory created is full of null. Then proceed a[0] = new int[4],A second memory is created to store values, a[0]What is saved here is the address of the memory later developed. If you understand this, you can easily understand one dimension. A two-dimensional array can be understood as a one-dimensional array, but the one-dimensional array stores a one-dimensional array. So what is the length of a two-dimensional array? Front, of course[]The value inside. This is example 5 Therefore, when defining format 2, it must have the size of the previous dimension, indicating that the length of this array is 2, and each value saves an array. Therefore, in a two-dimensional array, the length of each dimension can be different, which is different from c/c++.