JavaSEDay02 summary notes
01 identifier
Introduction: the symbols that give names to classes, methods, variables, etc
- Simply remember: your own name
Naming rules for identifiers:
- Number 0 ~ 9
- Character a-z A-Z
- _ $
- Cannot be keyword
- Cannot start with a number
- Strictly case sensitive
- Identifier naming conventions:
Nomenclature of small hump: (variable) If it's a word, All letters are lowercase ---> age max If it is more than one word, Capitalize from the second word --> maxAge tempMax Nomenclature of large hump: (class) If it's a word, title case ---> Student If it is more than one word, Capitalize each word ---> OperatorDemo ScannerDemo
02 - data type
- Basic data type
integer: byte 1 Bytes -128~127 short 2 Bytes int 4 Bytes long 8 Bytes decimal: float 4 Bytes double 8 Bytes character: char 2 Bytes Boolean: boolean 1 Bytes
- Ideas for defining variables in the future
1. If you define an integer type variable, be the first choice int, find int I can't fit it, change into long (join L Identification of) 2. If you define a decimal type variable, be the first choice double, Must be defined float Type variable, Need to join F identification
- Supplementary details:
All integers, All by default int type System.out.println(10); All decimals, All by default double type System.out.println(12.3); char Value range of type : 0~65535 problem: Why the numerical range answer: Every character, The bottom layer has a numerical identification form Character type, The value range is the value range - Each character, The bottom layer has a numerical representation - ASCII : 'a' ----> 97 'b' ----> 98 'c' ----> 99
- Reference data type
String character string String name = "Zhang San";
Example:
public class DataTypeTest { /* Requirement: please define your personal information as a variable and display it on the console (Name, age, gender, height, marital status) Name: String: String Age: integer: int Gender: character: char Height: decimal: double Marital status: boolean */ public static void main(String[] args) { String name = "Zhang San"; int age = 23; char gender = 'male'; double height = 180.1; boolean flag = false; System.out.println(name); System.out.println(age); System.out.println(gender); System.out.println(height); System.out.println(flag); }
03 keyboard entry
- Effect: make the data used in the program more flexible
- Steps:
import java.util.Scanner; public class ScannerDemo { /* 1. Find a spell Location: write on the top of the class Code: import Java util. Scanner; From the code warehouse, find the spell Scanner 2. Summon Sprites Location: written in the main method Code: Scanner sc = new Scanner(System.in); Meaning: summon the spirit in the spell and call it sc 3. Command the elves to work Location: written in the main method Code: int age = sc.nextInt(); Command sc to receive an integer from the keyboard and give it to the age variable record Code: String name = sc.next(); Command sc to receive a string from the keyboard and give it to the name variable record */ public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Please enter your age:"); int age = sc.nextInt(); System.out.println(age); System.out.println("Please enter your name:"); String name = sc.next(); System.out.println(name); } }
Operator 04 arithmetic
+ - * --------------------------------------- / : Integer division, The result can only be an integer, Want to get results with decimals, Decimals are required % : Take mold, Get the remainder after division
Self increasing and self decreasing operators:
++ : Let the value of the variable itself + 1 -- : Let the value of the variable itself - 1 be careful: Only variables can be manipulated, Cannot manipulate constants. --------------------------------------------------------- 1. Use alone(recommend) In a sentence of code, Only do ++ Or -- ++, -- Put variables before and after, There is no difference int a = 10; a++; ++a; 2. Participate in operation and use(interview) ++before: Self increasing first, Recalculation int a = 10; int b = ++a; // a = 11, b = 11 ++After: First operation, Self increasing again The original value of the variable will be, Extract, Do operation, Then self increase int a = 10; int b = a++; // b = 10, a = 11
public class OperatorTest2 { /* Requirements: see the program and say the result */ public static void main(String[] args) { int x = 10; int y = x++; int z = --y; System.out.println("x=" + x); System.out.println("y=" + y); System.out.println("z=" + z); System.out.println("---------------------"); int a = 3; int b = (++a) + (a++) + (a * 10); System.out.println("a=" + a); System.out.println("b=" + b); } }
05 string splicing operation
- When a string is encountered during the operation of the + sign, the + sign is the string connector, not the arithmetic operator
System.out.println("5+5=" + 5 + 5); // "5+5=55" System.out.println("5+5=" + (5 + 5)); // "5+5=10"
import java.util.Scanner; public class OperatorTest1 { /* Requirement: enter a three digit number on the keyboard, split it into single digit, ten digit and hundred digit, and print it on the console The bits of integer 123 are: 3 The ten digits of integer 123 are: 2 The hundredth of integer 123 is: 1 analysis: 1. Use the Scanner keyboard to enter an integer 2. Numerical splitting Bits: value% 10 Ten digit: value / 10% 10 Hundredth: value / 10 / 10% 10; Thousands: value / 10 / 10 / 10% 10; 10000 bits: value / 10 / 10 / 10 / 10% 10; ... ------------------------------ For the highest bit (leftmost): optimize 123 / 100 ====> 1 4567 / 1000 ====> 4 33445 / 10000 ====> 3 3. Print the split results on the console */ public static void main(String[] args) { // 1. Use the Scanner keyboard to enter an integer Scanner sc = new Scanner(System.in); System.out.println("Please enter a three digit number:"); int num = sc.nextInt(); // 2. Numerical splitting int ge = num % 10; int shi = num / 10 % 10; int bai = num / 10 / 10 % 10; // int bai = num / 100; // 3. Print the split results on the console System.out.println("integer" + num + "The bits of are:" + ge); System.out.println("integer" + num + "The top ten are:" + shi); System.out.println("integer" + num + "The hundredth of is:" + bai); } }
06 - type conversion - implicit conversion
Introduction: the data or variables with small value range can be assigned directly to the variables with large value range
- Simple note: give the small one to the large one, and you can give it directly
Data value range: byte short int long float double
- Although float takes up 4 bytes, its value range is larger than 8-byte long
int a = 10; double b = a; System.out.println(b); // 10.0
Implicit conversion in operation
- The data with small value range and the data with large value range are calculated together. The small one will promote itself to the large one first, and then calculate
- byte short char will be directly promoted to int type during operation
07 - type conversion - Cast
Introduction: assign a value to a variable with a large value range or data to a variable with a small value range. Direct assignment is not allowed, and forced conversion is required
- Simple note: the big one is given to the small one. It is not allowed to give it directly. It needs to be forced
int num = (int)12.3;
- Note: try to reduce the use of forced rotation in the future, which [May] cause accuracy loss
int num = (int)12.3; System.out.println(num); // 12 loss of accuracy occurred int a = 10; byte b = (byte)a; System.out.println(b); // 10 no loss of accuracy
- Type conversion interview questions:
public class OperatorTest3 { /* byte a = 3; byte b = 4; byte c = a + b; Question: is there a problem with the above code? If so, please explain the reason and correct it Reason: A and B are two byte types. When they operate together, they will be directly promoted to int type After promotion, two ints are added. The result of addition is still int. assign the result of int type to byte type variable This belongs to the big to the small and needs to be forced Solution: byte c = (byte)(a + b); -------------------------------------------------------------- byte result = 3 + 4; Question: is there a problem with the above code? If so, please explain the reason and correct it Doubt: 3 and 4 are two integer constants. All integers are int by default. The result of adding two ints is still int. assign the result of int type to byte type variable This is big to small and needs to be forced Reason: Java has a constant optimization mechanism, which automatically adds 3 and 4 when compiling (javac) .class(Bytecode): byte b = 7; And it will automatically check whether the operation result is within the value range of byte */ public static void main(String[] args) { byte result = 3 + 4; } }
08 assignment operator
1. Basic assignment operator = : The data to the right of the equal sign, Assigned to the variable on the left int a = 10; 2. Extended assignment operator += : Put the data on the left and right sides of the symbol, Do addition, Assign the result to the variable on the left -= : Put the data on the left and right sides of the symbol, Do subtraction, Assign the result to the variable on the left *= /= %= be careful: Extended assignment operator, With strong rotation effect int a = 10; double b = 12.3; a += b; // a = (int)(10 + 12.3); System.out.println(a); // 22 -------------------------------------- int c = 10; double d = 33.3; d += c; // d = 10 + 33.3; Sysetm.out.println(d); // 43.3
09 relational operator (comparison operator)
> >= < <= == != conclusion: Relational operator, Results obtained, Only boolean Type true, false = : Assignment operation == : Comparison operation
10 - logical operator
Introduction:
- Code level: it is used to connect boolean type expressions or values
- Understanding level: it can integrate multiple judgment conditions into a whole logic
if(score >= 90 && score <= 100) {}
- Classification:
&(And) : also, meet false be false |(or) : perhaps, meet true be true !(wrong) : Reverse --------------------------------------------- ^ (XOR) : Same as false, Different as true
11 - short circuit logic operator
& : No short circuit effect, Whether the symbol is on the left true, false, Continue to execute on the right && : Short circuit effect, To the left of the symbol is false, Not on the right. If the left side of the symbol is true, Continue on the right | : No short circuit effect, Whether the symbol is on the left true, false, Continue to execute on the right || : Short circuit effect, To the left of the symbol is true, Not on the right. If the left side of the symbol is false, Continue on the right Logical operators commonly used in the future : && || !
12 ternary operator
- Introduction: the ternary operator can choose one of the two according to a judgment condition
- Demand: find the maximum of two integers
- Format: judgment condition? Value 1: value 2;
Execution process:
- Execute the judgment condition to see that the returned results are true and false
- true: a value of 1 is the final result
- false: a value of 2 is the final result
int max = a > b ? a : b;
13 operator priority
- Master the use of () with the highest priority
- Common sense: & & has priority over||
14 - Introduction to process control statement
- Introduction: control the execution flow of the program through some special statements
Classification:
- Sequential structure: default execution flow of Java program
- Branch structure: if, switch
- Loop structure: for, while, do while
15 - branch structure statement - if
if Statement format 1: if(Judgment conditions){ Statement body; } Execution process: 1. implement () Judgment conditions in, Look at the result true, false 2. true : implement {} Statement body in 3. false : Do not execute {} Statement body in matters needing attention: 1. if sentence () and {} Don't write semicolons between 2. if sentence {} If there is only one statement in, {} It can be omitted, But not recommended 3. if sentence () in, Whether simple or complex, As long as it is boolean Type true, false, It conforms to the grammatical rules
if Statement format 2: if(Judgment conditions){ Statement body 1; }else{ Statement body 2; } Execution process: 1. Execution judgment conditions, Look, the result is true, false 2. true : Execute statement body 1 3. false : Execute statement body 2
if Statement format 3: if(Judgment condition 1){ Statement body 1; }else if(Judgment condition 2){ Statement body 2; }else if(Judgment condition 3){ Statement body 3; } ... else { Statement body n+1; } Execution process: 1. Execution judgment condition 1, Look at the result true, false true : Execute statement body 1, And end the whole if sentence. false : Proceed to the second step 2. Execution judgment condition 2, Look at the result true, false true : Execute statement body 2, And end the whole if sentence false : Proceed to step 3 ... 3. If all judgment conditions given, None, The final will be executed else Statement body n+1; demand: Enter a value on the keyboard, Indicates the identity of the user (1. Member 2. Non member others: Your input is incorrect)
Required cases:
import java.util.Scanner; public class OperatorTest5 { /* Requirements: enter the test results with the keyboard, and print out different reward mechanisms according to the range of the results 95 ~ 100 : One bicycle 90 ~ 94 : Play in the playground once 89 ~ 89 : Transformers one 80 Below: beaten Robustness judgment: check the rationality of the data */ public static void main(String[] args) { // 1. Keyboard entry results Scanner sc = new Scanner(System.in); System.out.println("Please enter your grade:"); int score = sc.nextInt(); // 2. Multiple judgment conditions, using if else... if if (score < 0 || score > 100) { System.out.println("Your score is entered incorrectly!"); } else if (score >= 95 && score <= 100) { System.out.println("One bicycle"); } else if (score >= 90 && score <= 94) { System.out.println("Play in the playground once"); } else if (score >= 80 && score <= 89) { System.out.println("Transformers one"); } else { System.out.println("Be beaten"); } } }
import java.util.Scanner; public class OperatorTest6 { /* Requirements: enter the test results with the keyboard, and print out different reward mechanisms according to the range of the results 95 ~ 100 : One bicycle 90 ~ 94 : Play in the playground once 89 ~ 89 : Transformers one 80 Below: beaten Robustness judgment: check the rationality of the data */ public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Please enter your grade:"); int score = sc.nextInt(); if (score >= 0 && score <= 100) { // Reasonable results // If the code can get here, the score is definitely between 0 and 100. You only need to consider what reward to give if (score >= 95 && score <= 100) { System.out.println("One bicycle"); } else if (score >= 90 && score <= 94) { System.out.println("Play in the playground once"); } else if (score >= 80 && score <= 89) { System.out.println("Transformers one"); } else { System.out.println("Be beaten"); } } else { System.out.println("Please check your score, Is it 0~100 between"); } } }