Basic content and grammar
1.java Foundation
It consists of hardware and software
Application fields:
1) scientific calculation
2) artificial intelligence (data analysis)
3) data processing
4) auxiliary design
5) computer network
Division of software:
1) system software: DOS, windows, Linux (CentOS 7.8, red hat, deepin: domestic), Mac
Mobile terminal: android,ios, Hongmeng (Huawei)
2) application software: the client software must be updated to use the functions of the server's higher version!
QQ client
office software
VNC view (client)
VNC service (server)
Division of software structure:
C/S structure: Client/Server, client and server
Example: QQ client
Features: the client software must be upgraded to use the functions of the higher version of the server
B/S architecture: Browser/Server: interaction between browser and server (future direction)
Server: open source server: tomcat web container theory concurrent visits: 500; http server: nginx (load balancing, reverse proxy): 50000
Features: as long as there is a browser in the computer
(ie, Firefox, Google, browser side can access some content in the server without specific upgrade!
Software development:
notepad + + Advanced notepad
idea(2019/2020): Lenovo is very accurate (not suitable for the first two weeks)
DOS command:
command | usage |
---|---|
exit | Exit the current DOS command window |
DOS replication: | Right click anywhere in the window |
dir | Lists all sub files / subdirectories in the current directory |
cd | (change directory) |
cd ... | Go back to the parent directory |
cd \ | Go straight back to the root path |
c: Enter d: enter e: enter f: Enter | Switch drive letter |
Ipconfig | View IP address |
Xxx -version | View version |
Del * | delete |
md | Create a folder |
rd | Delete folder (must be empty) |
rd /s /q | Force delete |
rd /s | Directory with content (folder): ask if you want to delete this directory |
Basic meaning of java:
JDK: (Java Development Kit)
JRE: (java runtime environment)
JVM: (Java Virtual Machine)
Download address: https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html
After installing the jdk: enter dos and enter java -version: to view the version information of the current local jdk
After the jdk is installed: you need to use the compilation and execution commands in the bin directory of the installation directory
javac.exe compile command
java.exe run command
path environment (system configuration):
Reason for configuring the path environment variable:
We need to compile and run java source files using javac and Java in any directory
Mode 1: (disadvantages)
Win10 / win8 ---- > this computer ---- > right click Properties ---- > advanced system settings ---- > environment variables ---- > system variables find the path and copy the bin path of the jdk installation directory into the path!
Path: path to JDK \ bin
Disadvantages:
1) When the jdk version is upgraded, developers need to manually change the path: other variables of the path may be changed, so it's not good!
2) In the second stage, the tomcat server (written in java) is used. When starting, it will look for whether the path in the system variable in the computer exists
JAVA_HOME. If it cannot be found, the "flash screen" will appear when the server starts;
Usage 2: (recommended):
Win10 / win8 ---- > this computer ---- > right click Properties ---- > advanced system settings ---- > environment variables ---- > system variables
New variable name:
JAVA_HOME
Variable value:
jdk path
advantage:
1) there is no need to change the path manually
2) when the Tomcat server starts, find JAVA_HOME variable, there will be no flash screen
Execution process of path environment variable:
Write the java file, compile and run the java file;
1) first, you need to find the directory of java files in dos. If you can't find it
2) find% Java in the path environment variable_ HOME%\bin
3) if there is no such file in the path, you will be prompted that the file cannot be found for a long time
classpath: temporary environment variable (understand):
Classpath execution process: first find out whether there is a classpath in the system variable
If there is, the priority is XXX Class file
2. Notes
Single line note:
//Comment only the current line
Multiline comment:
/* Note Content Note Content ... */
javadoc comments:
(this annotation is a professional annotation. The annotation information will be parsed and extracted by javadoc.exe tool and help documents will be generated)
/** \* javadoc notes \* javadoc notes \* javadoc notes \* javadoc notes \* javadoc notes */
3. Keywords and identifiers
3.1 keywords
What are keywords:
A word given a specific meaning by the Java language!
And in advanced notepad and development tools, there is a special color mark for "keyword"!
notepad++ eclipse/idea
editplus
matters needing attention:
goto and const are used as reserved words, not keywords!
keyword:
Class: means to create a class (the most basic unit of Java)
Public: one of the permission modifiers: public, public, with sufficient access rights
Static: later in the object-oriented part, we will talk about static modifiers
void: it has something to do with methods in Java (say it when talking about Java methods)
3.2 identifier
What is an identifier
In the original Java program, all the words that programmers have the right to name themselves are identifiers (black font)
Elements that can be identified: class name, method name (main), variable name, interface name, constant name (srgs)
Composition of identifier:
A legal identifier can only be composed of "numbers, letters, underscores and $", and cannot contain other symbols
Cannot start with a number
Strictly case sensitive
Keywords (blue) cannot be used as identifiers
Theoretically, there is no length limit. It's best not to be too long
If it is a single word:
All lowercase letters
give an example:
main() int price = 100 ;
If more than one word:
All letters of the first word are lowercase, the first letter of each word is capitalized from the second word, and the rest are lowercase (small hump naming method)
give an example:
checkUserName() int applePrice = 100 ;
Rules for naming constants:
If it is a single word: all letters are capitalized (multiple words are recommended)
give an example:
HELLO=100;
If there are multiple words: each word is capitalized, and the words are separated by underscores
give an example:
HELLO_WORLD=100;
Post learning: Thread: Thread Class
public final static int MIN_PRIORITY = 1; Minimum priority public final static int NORM_PRIORITY = 5; Default Priority public final static int MAX_PRIORITY = 10;Maximum priority
4. Constant
The amount whose value does not change during the execution of the program!
1) Literal constant
**String constant: * * contents enclosed in double quotes: called "string constant"
give an example:
"I love Gao Yuanyuan"
"HelloWorld"
**Character constants: * * contents wrapped in single quotes
give an example:
'a','A','0'
"I"
'abc' illegal data (only single content enclosed in single quotation marks)
Integer constant:
100 includes "base" integers -- default decimal
64
...
Decimal constant:
3.14
12.56
Boolean constant: true false
Null constant: null
Reference data type default null
2) Custom constant
(object oriented later: keyword: final: status modifier)
public final static int MIN_PRIORITY = 1; Minimum priority
5. Variables
What are variables?
In essence, a variable is a space in memory that has data types, names, and literals
Variables include: data type, name, and literal value
Variables are the most basic units for storing data in memory
Variable requirements:
The specific "data" stored in the variable must be consistent with the "data type" of the variable. If it is inconsistent, the compilation will report an error.
Declaration / definition of variables syntax format of variables:
Data type variable name (legal identifier);
Assignment of variables:
Syntax format: variable name = literal value ("=" is assignment operator)
PS. the data type of literal value must be consistent with that of variable name
eg.
Int i=10;//Declarations and assignments can be done together /* After the variable is assigned, it can be re assigned (variable value) With the concept of variable, the memory space can be reused */ int i=10; System.out.println(i);//10 int i=100; System.out.println(i);//100 ... ... System.out.println(i)
Access method for accessing variables:
1. Read the specific data saved in the variable get / get
2. Modify the specific data set / setting saved in the variable
eg.
i=20;//set System.out.println(i);//get //Variables can be declared more than one on a line. First declare and then assign values before accessing. int a,b,c; int a=1; int b=2; int c=3;
Java code is executed from top to bottom. The program on the next line can only be executed after the code on the previous line is completed
In the same scope, the variable name cannot be duplicate, but the variable can be re assigned
Scope of variable:
Scope: the valid range of a variable, within which it can be accessed. As long as it is out of this range, the variable will not be accessed. Out of {}, I don't know.
Classification of variables:
Classify according to the location of variable declaration:
Local variables: variables declared in the method body
Member variables: variables declared outside the method and within the class body
(variable names can be duplicated in different scopes; variable names cannot be duplicated in the same scope)
6. Data type
What is the role of data types?
Different data have different types, and different data types will allocate different space at the bottom
Data type is a guide to how much memory space the program should allocate at run time
6.1 basic data type
Basic data type | Value range | Default value | Occupied space size (bytes) |
---|---|---|---|
byte | -128~127 | 0 | 1 |
short | -32768~32767 | 0 | 2 |
int | -231~2......231-1 | 0 | 4 |
long | -263~263-1 | 0 | 8 |
float | 0.0f | 4 | |
double | 0.0d | 8 | |
char | 0~2^16-1(65535) | false(0) | 2 |
boolean | true/flase | /u0000 | 1 |
byte
1 byte = 8 bit s (1 bit represents a binary bit)
1 KB = 1024 byte
1 MB = 1024 KB
1 GB = 1024 MB
1 TB = 1024 GB
The byte type in the integer type takes up 1 byte, so the byte type data takes up 8 bits.
What is the value range of byte type?
As for the number types in java, numbers are divided by positive and negative, so there is a binary bit in the binary of numbers, which is called "sign bit", and this "sign bit" is on the far left of all binary bits, 0 represents a positive number and 1 represents a negative number.
max: 01111111 (10000000 (binary) - 1 = 127) min:11111111 (- 127)
The specific binary representation also includes: original code, complement code and inverse code
Binary conversion
Two for ten for two
Computers only know binary, so how do computers represent words in the real world?
Among the eight basic data types, char represents text in real life. By default, there is no conversion relationship between text and computer binary.
In order for the computer to also express the characters in the world, we need human intervention, and people are responsible for formulating the comparison relationship between "characters" and "binary" in advance. This comparison is called character coding.
At first, the computer only supported English, and the first character code was ASCII
'a'=97[01100001]
'A'=65
'0'=48
The same set of comparison table is used for encoding and decoding, and there will be no random code.
When the same set of comparison table is used for decoding and coding, garbled code will appear
Data type conversion
public class practice{ public static void main(String[] args){ /* 100L Is a long type literal x Is a variable of type long No type conversion, direct assignment */ long x=100L; /* x It is of type long, 8 bytes y Is of type int, 4 bytes Compilation error. A large capacity cannot be changed to a small capacity directly int y=x; Forced type conversion is required for large capacity to small capacity Casts require a cast character After adding the cast character, the compilation passed, but the runtime may lose precision Arbitrary casts are used with caution because the loss of precision can be severe Principle of forced conversion Original data 00000000 000000000 00000000 0000000 0000000 00000000 01100000 After cast: 00000000 0000000 00000000 01100100 Cut off the binary on the left! */ int y=(int)x; System.out.println(y); /* Original data: 00000000 000000000 00000000 10000000 00000000 00000000 000000000 Data after forced conversion: 10000000 0000000 0000000 0000000 (in complement form) The conversion of the above complement to the original code is the final result */ long k=2147483648L; int e=(int)k; System.out.println(e);//The loss of accuracy is very serious, and the result is negative [- 2147483648] /* Based on the current content, the following programs cannot be compiled Reason: 50 is a literal of type int and b is a variable of type byte Obviously, it is a small byte converted from a large int To convert a large capacity into a small capacity, you need to add a cast symbol. The following program does not add a cast symbol, so an error is reported during compilation However, during actual compilation, the following code passes and can be directly assigned to a byte type variable */ byte b=50;//sure byte c=127;//sure //Compilation error: 128. The literal value of int type exceeds the value range of byte type and cannot be directly assigned to a variable of byte type //byte b1=128; byte b1=(byte)128;//-125 /* To correct the error, you need to use a cast character, but you will lose precision Original data: 00000000 00000000 10000000 10000000 after the cast [this is stored inside the computer, this is a complement,] System.out.println(b1); */ } }
Complement:
Positive number: the same as the original code
Negative number: 0 becomes 1, 1 becomes 0, and then 1 is added
When the face value of an integer number does not exceed the value range of byte, short and char, this literal value can be directly assigned to variables of type byte, short and char. (for the convenience of programmers)
6.2 reference data type
Array, class, interface
7. Operator
7.1 arithmetic operators
Operators have priority. If there are too many operators in an expression, the priority will be increased if it is uncertain.
There is no need to memorize operator priorities
+ + after the variable appears, first perform the assignment operation, and add 1 to the value saved in the variable
+ + appears before variable, advanced nature adds 1, undertake assignment.
7.2 relational operators
The result of a relational operator must be Boolean: true/false
7.3 logical operators
& logical and (the result is true only if the operators on both sides are true)
| logical or (only one operator on both sides is true, and the result is true)
! Logical non (negative)
^ logical XOR (operators on both sides are different (true / false / true), and the result is true)
& & short circuit and (same as logic and operation results, but short circuit and short circuit exist)
The first half is judged as false, the whole sentence is false, and the second half is no longer executed (smarter than logic and)
|𞓜 short circuit or (the same as logic or operation result, but short circuit or short circuit exists)
The first half is true, the whole sentence is true, and the second half is no longer executed
Logical operators require operators on both sides to be Boolean types, and the final operation result of logical operators is also a boolean type
7.4 assignment operators
Basic assignment operators:
=
Extended assignment operator:
+= -= *= /= %=
eg.
byte b=10; b=b+5;//report errors
Correction:
b = (byte)(b+5); b + = 5;//b+=5 is equivalent to b=(byte)(b+5)
The assignment operator of the extension class does not change the data type of the operation result (assuming that the initial result is byte, no matter how to append or subtract, the final result is still byte type)
7.5 string concatenation operator
About the "+" operator in java
1 addition operation (number + number)
2 connection operation of string (string + string)
3 multiple "+" can appear in an expression. Without adding parentheses, follow the operation from left to right.
eg.
int a=1; int b=2; System.out.println(a+"+"+b+"="+(a+b));//1+2=3
7.6 ternary (ternary / conditional) operator
rule of grammar:
Boolean expression? Expression 1: expression 2
Execution principle:
When the Boolean expression result is true, select expression 1 as the execution result of the entire expression
When the Boolean expression result is false, select expression 2 as the execution result of the entire expression
eg1.
boolean sex=true; Char c =sex ? 'male':'female';//male
eg2.
Sex=true; System.out.println(sex ?'male':""Female");
7.7 bitwise operators
Bitwise Operators | |
---|---|
<< | The n-th power of multiplication by 2 |
>> | n-th power of division 2 |
>>> | The highest bit of the shifted binary, whether 0 or 1, is filled with 0. |
Process control statement
8. Process control statement
1. Sequential structure statement
2. Select structure statement
2.1 if statement
Syntax structure of if statement: Four writing methods
/*PS.For if statements in java, as long as one branch executes, the entire if statement ends.*/ //First: if(Boolean expression){ /* java sentence; java sentence; java sentence; java sentence; ... */ } //Second: if(Boolean expression){ /*java sentence; java sentence; ... */ }else{ /* java sentence; java sentence; ... */ } //Third: if(Boolean expression){ /* java sentence; java sentence; ... */ }else if(Boolean expression){ /* java sentence; java sentence; ... */ }else if(Boolean expression){ /* java sentence; java sentence; ... */ }..... //Fourth: if(Boolean expression){ /* ava sentence; java sentence; ... */ }else if(Boolean expression){ /** java sentence; java sentence; ... */ }else if(Boolean expression){ /* java sentence; java sentence; */ ... }else{ /* java sentence; java sentence; ... */ }
Both the second and fourth writing methods have else branches, which can ensure 100% branch execution.
All control statements can be nested with each other. Just nest in the river.
When nesting is used, the code format remains perfect. [must be indented when indenting]
If there is only one java statement in the branch of the if statement, the braces can not be written.
eg.
import java.util.Sacnner; public class Weather problem { public static void main(String[] args) { System.out.println("Winter vacation programming practice with nothing to do"); System.out.println("The weather is divided into“ sun"And“ rain"two types"); System.out.println("Gender is divided into“ man"And“ rain"two types"); java.util.Scanner s = new java.util.Scanner(System.in); //Receive weather conditions System.out.print("Please enter the current weather conditions:"); String weather = s.next(); System.out.print("Please enter gender:"); String sex =s.next(); if("sun".equals(weather)){ System.out.print("Please enter today's temperature:"); int wendu =s.nextInt(); System.out.println("clear sky"); if(wendu>30){ if("man".equals(sex)){ System.out.println("Wear sunglasses"); }else if("woman".equals(sex)){ System.out.println("Sprinkle sunscreen"); }else{ System.out.println("Shit! See clearly before entering!"); } }else{ System.out.println("Don't go out"); } }else if ("rain".equals(weather)){ System.out.println("Rainy day"); if("man".equals(sex)){ System.out.println("Big black umbrella"); }else if("woman".equals(sex)){ System.out.println("Floret umbrella"); }else{ System.out.println("Shit! See clearly before entering!"); } }else { System.out.println("Shit! See clearly before entering!"); } } }
2.2 swtich statement
switch Syntax structure of the statement: switch(int or String Literal or variable of type){ case int or string Literal or variable of type: java sentence; ... break; case int or string Literal or variable of type: java sentence; ... break; case int or string Literal or variable of type: java sentence; ... break; Default: java sentence; .... }
Execution principle:
1. Match the "data" in the parentheses after the switch with the "data" after the case one by one, and the branch execution with successful matching.
2. Match from top to bottom.
3. Match the successful branch execution. If there is a "break" statement at the end of the branch, the whole switch statement will terminate.
4. If the branch matches successfully and there is no "break" statement in the branch, it will directly enter the next branch for execution (no matching). This phenomenon is called case penetration phenomenon (providing break statement can avoid penetration phenomenon)
5 if all branches are not matched successfully, the default statement will be executed when there is a default statement
6. The data after switch and case can only be int or string data, not other types
Of course, byte short char can also be written directly after switch and case, because they can automatically type into int.
7 case s can be merged:
int i=10; switch(i){ case1:case2:case3: System.out.println("test code!"); }
eg.
import java.util.Sacnner; public class Switch What day is today { public static void main(String[] args) { java.util.Scanner s = new java.util.Scanner(System.in); System.out.print("Guess what day it is today:"); int num = s.nextInt(); switch(num){ case 1: System.out.println("Monday"); break; case 2: System.out.println("Tuesday"); break; case 3: System.out.println("Wednesday"); break; case 4: System.out.println("Thursday"); break; case 5: System.out.println("Friday"); break; case 6: System.out.println("Saturday"); break; case 7: System.out.println("Sunday"); break; default: System.out.println("Please enter a number from one to seven"); } } }
3. Circular structure statement
3.1 for statement
switch Syntax structure of the statement: for(Initialization expression; Boolean expression; Update expression){ //Is a code fragment that needs to be executed repeatedly [loop body: composed of java statements] }
eg.1
public class JavaApplication5 { public static void main(String[] args) { for(int i=1;i<=10;i+=2 ){ System.out.println("i="+i); } for(int j=2;j<=10;j+=2 ){ System.out.println("j="+j); } for(int k=10;k>0;k-- ){ System.out.println("k="+k); } for(int z=100;z>0; ){ System.out.println("z="+z); z-=10; } } }
Nested use of loop statements and conditional judgment statements
You'd better not think too much. Even if there is a for loop in the loop body, don't specialize this for,
[nesting of for and if]
eg.2
//All prime numbers within 100 public class ForFor Nested prime { public static void main(String[] args) { //Give scope for(int i=2;i<=100;i++){ //Find prime boolean sushu = true; for(int j=2;j<i;j++){ if(i%j==0){ sushu = false; break; } } //output if(sushu){ System.out.print(i+"\t"); } } } }
eg.3
//For all prime numbers within 10000, change one line for every eight numbers //Method ① public class ForFor Nested prime { public static void main(String[] args) { int count=0; //Give scope for(int i=2;i<=10000;i++){ //Find prime boolean sushu = true; for(int j=2;j<i;j++){ if(i%j==0){ sushu = false; break; } } //output if(sushu){ count++; System.out.print(i+"\t"); if(count%8==0){ System.out.println(); } } } } } //Method ② public class Prime upgrade { public static void main(String[] args){ int count=0; for(int i=1;i<10000;i++){ boolean sushu = true; for(int j=2;j<i;j++){ if(i%j==0){ sushu = false; break; } } //output if(sushu){ count++; System.out.print(i+"\t"); if(count==8){ System.out.println(); count=0; } } } } }
3.2 while statement
while Syntax structure of the loop: while(Boolean expression){ Circulatory body; }//When the expression is true, the loop body is executed
Number of while cycles:
0 to N times, the loop body of the while loop may not be executed once
eg.
//Output 0-10: public class ArrayOutPut { public static void main(String[] args){ int i=0; while (i<10){ System.out.print(i+"\t"); i++; } } }
3.3 Da while statement
\\do...while Syntax structure of the loop: do{ Circulatory body; }while(Boolean expression);
The execution times of do... whlie loop body code fragment are: 1-N times (at least once)
Precautions for do... while loop: the do... while loop statement finally has a semicolon
4. Jump statement
4.1break
Break is a keyword in a Java statement, translated as "break / break"
break+“;” It can be a single complete java statement: break;
break statements are used in switch statements to terminate the execution of switch statements.
break statements can also be used in loop statements to terminate the execution of loops
The break statement is used in the for, while, do... While loop statements to jump out of the loop and terminate the mysterious execution. Because when the program loops to a certain condition, the subsequent loops do not need to be executed, and the execution also consumes resources, so the loop can be terminated, which can improve the execution efficiency of the program.
By default, break terminates the nearest loop statement:
4.2continue
Continue means yes: continue / next
java statement of continue: continue;
The difference between break and continue:
break: jump out of loop
continue: enter the next cycle