------<a href= "http://www.itheima.com" target= "blank" >java training, Android training, iOS training,. NET training </a>, look forward to communicating with you! -------
1: operator (master)
(1) Arithmetic operators
a:+,-, *,/,%,++,--
Note: Integers can only be divided into integers, and if you want decimals, you must first change the data to a floating-point number type. If 3/4 results are 0,3*0.1/4=0.75
The use of b:+
A: Addition
B: plus sign
C: String connector: ' A ' +1=98, ' hello ' + ' a ' +1=helloa1, ' a ' +1+ ' Hello ' =98hello
The difference between c:/and%
When the data is in division operation,/get is quotient,% get is remainder
The use of d:++ and--
A: Their function is to increment the variable by 1 or subtract 1 from it. Constants cannot be self-increasing or self-reducing.
B: Use
* * Used alone
Put in front and back of the action data.
The a++ or ++a effect is the same.
* * Participate in operation using
Put in front of operand: increment or decrement first, then participate in operation
int a = 10;
int b = ++a;
The output is a=11 b=11
At the back of the operand: take part in the operation first, then self-increment or decrement
int a = 10;
int b = a++;
The output is a=11 b=10
Exercise: int x=4; int y= (x + +) + (++x) + (x*10) =4+6+6*10=70 output is x=6,y=70.
(2) Assignment operator
a:=,+=,-=,*=,/=,%=, etc.
B:= is called the assignment operator and is the most basic assignment operator
int x = 10; Assign 10 to the variable x of type int.
C: Extended assignment operator +=,-=,*=,/=,%=
+ =, add to the left and right, then assign to the left variable.
-=, subtract left and right, then assign to left variable.
*=, multiply the left and right, then assign to the left variable.
/=, divide the left and right, then assign to the left variable.
%=, divide the left and right, then assign to the left variable.
D: The extended assignment operator implies an automatic cast.
Interview questions:
Short S = 1;
s = s + 1;
Short S = 1;
s + = 1;
Can you tell me which of the above code is wrong?
Answer: Short s = 1; s = s + 1; there is a problem because this will result in loss of precision. Because s = s + 1, s+1 is of type int. If you don't think of a problem, you need a cast. That is s= (short) (s+1)
Short S = 1;s + = 1; No problem, because the extended assignment operator implicitly implies a cast of the coercion type. S+=1 is not equivalent to s=s+1, but is equivalent to s= (the data type of s) (s+1).
(3) Comparison operators
a:==,!=,>,>=,<,<=
B: Regardless of whether the operator is simple or complex, the end result is a Boolean type.
C: Don't write = =
(4) Logical operators
a:&,|,^,!, &&,| |
B: Logical operators are used to concatenate Boolean types
C: Conclusion
& Logic and: False if False (Cantay is true)
| logical OR: TRUE (Real is true)
^ Logical XOR: The same is false, and the difference is true.
Example: A couple relationship, with the same sex as false.
Logical non: False if not true, true
&&: The result is the same as the &, except that it has a short-circuit effect. The left side is false and the right side is not executed.
| |: The result and | are the same, but with short-circuit effect. The left is true and the right side is not executed.
Example: int a=3; int b=4; Boolean c= (++a==3) && (b++==4) results a=4.b=4.c=false, the b++ on the right is not executed.
(5) bitwise operator (LEARN)
A: To do a bitwise operation, the first thing to do is to convert the data into binary, to the twos complement & | ^ ~
Analysis: Because it is a bitwise operation, we must first change the data into binary.
3 binary: 11
00000000 00000000 00000000 00000011 (complement and original code)
4 binary: 100
00000000 00000000 00000000 00000100 (complement and original code)
& Bitwise operations: 0 0 (similar to false in logic)
00000000 00000000 00000000 00000011
&00000000 00000000 00000000 00000100
------------------------------------------
00000000 00000000 00000000 00000000
So the result of 3&4 is 0.
| or bitwise operation: there are 1 1
00000000 00000000 00000000 00000011
|00000000 00000000 00000000 00000100
------------------------------------------
00000000 00000000 00000000 00000111 So the result of 3|4 is 7.
^ Bitwise XOR operation: Same as 0, different 1
00000000 00000000 00000000 00000011
^00000000 00000000 00000000 00000100
------------------------------------------
00000000 00000000 00000000 00000111 So the result of 3^4 is 7.
~ Bitwise inverse Operator: 0 change to 0
~00000000 00000000 00000000 00000011
11111111 11111111 11111111 11111100 (3 twos complement)
------------------------------------------
Complement: 11111111 11111111 11111111 11111100
Anti-code: 11111111 11111111 11111111 11111011 (complement minus 1 anti-code)
Original code: 10000000 00000000 00000000 00000100 (reverse code to take the original code)
So the result is-4.
B:
One: The special usage of ^
One data is different for another data bit or two times, which is the same number. Example: int a=10; int b=20; a^b^b=10 a^b^a=20
Two:
<<: Left to the left the highest bit discarded, 0 on the right side, << the left side of the data multiplied by 2 to move the power such as: 3<<2 =3*2^2=12; int A=3 (--a) <<a equals 2<<2=2*2^2=8
>> right shift the highest bit is 0, the left side is 0. The highest bit is 1 and the left side is 1. Divide the data on the left of >> by 2 to move the power as 24>>2 =24/2^2=6
>>>: Unsigned Right SHIFT, whether the highest bit is 0 or 1, the left side 0; 3>>>1 =3/2^1=1
C: Face question
A: Please implement the exchange of two variables
Mode one: Use of third-party variables (in development) such as: int a=10;int b=20; int c=a; A=b; B=c;
Mode two: Use bitwise XOR OR operator. (used in the interview) left A,b,a, right a^b
A=a^b;
B=a^b;//a^b^b=a
A=a^b;//a^b^a=b
Method Three: The method of adding variables (understanding)
A=a+b;//a=30
b=a-b;//b=10
A=a-b;//a=30-10=20
Way four: One sentence to get it done. Know
b= (a+b)-(a=b);//b=30-20=10,a=20;
B: Please calculate the result of 2 times 8 in the most efficient way.
2<<3
(6) Ternary operator
A: Format
Compare Expressions 1: expression 2;
B: Execution Process:
The value of the comparison expression is evaluated first to see if it is true or false.
If true, the expression 1 is the result.
If False, the expression 2 is the result.
C: Case:
A: Compare two data for equality
int A=100;int b=200; Boolean flag= (M==n)
B: Get the maximum value from two data
int A=100;int b=200; int max= (A>B?A:B);
C: Get the maximum value from three data
int a=10; int b=20; int c=30; int temp= (A>B?A:B); int max= (TEMP>C?TEMP:C);
2: Keyboard entry (Master)
(1) in the actual development, the data is changed, in order to improve the flexibility of the program, we add keyboard input data.
(2) How to achieve it? Now remember
A: Guide Package
Import Java.util.Scanner;
Position: On top of class
B: Create an Object
Scanner sc = new Scanner (system.in);
C: Get Data
int x = Sc.nextint ();
(3) Adding the case of ternary operator to keyboard input improvement.
3: Process Control statement
(1) sequential structure from top to bottom, executed sequentially
(2) Select structure Execute different code according to different choices
(3) Loop structure to do some repetitive code
4:if Statement (Master)
(1) Three formats
A: Format 1
if (comparison expression) {
Statement body;
}
Execution process:
Judge the value of the comparison expression to see if it is true or false
If true, the statement body is executed
If False, the statement body is not executed
B: Format 2
if (comparison expression) {
Statement body 1;
}else {
Statement body 2;
}
Execution process:
Judge the value of the comparison expression to see if it is true or false
If true, the statement body is executed 1
If False, the statement body 2 is executed
C: Format 3
if (comparison expression 1) {
Statement body 1;
}else if (comparison expression 2) {
Statement body 2;
}
...
else {
Statement body n+1;
}
Execution process:
Determine the value of the comparison expression 1 to see if it is true or false
If true, the statement body is executed 1
If false, continue to judge the value of the comparison expression 2 to see if it is true or false
If true, the statement body is executed 2
If false, continue to judge the value of the comparison expression 3 to see if it is true or false
...
If all is not satisfied, execute the statement body n+1
(2) Precautions
A: Comparing expressions whether simple or complex, the result is a Boolean type
B:if statements control the body of a statement if it is a statement, you can omit the curly braces, if it is more than one, cannot be omitted.
Suggestion: Never omit.
C: Generally, there are left curly braces, there is no semicolon, there is a semicolon, there is no left curly brace.
D:else If there is no if, the comparison expression is not present.
E: Three If statements are actually a statement, as long as there is one execution, the other is no longer executed.
(3) Case:
A: Compare whether two numbers are equal
B: Get the maximum value in two numbers
C: Gets the maximum value in three digits (nested if statement)
D: According to the grade of the result output corresponding
E: According to the month, the corresponding season of the output
F: Calculates the value corresponding to Y according to X and outputs
(4) The relationship between ternary operator and the second form of if statement
All ternary operators can be implemented, and the second form of the IF statement can be implemented.
Conversely, it is not established.
If the statement body that is controlled by the second format of the IF statement is an output statement, it is not possible.
Because the ternary operator is an operator, you must have a result returned and cannot be an output statement.
5,switch Statement (Master)
(1) Format:
switch (expression) {
Case value 1:
Statement body 1;
Break
Case Value 2:
Statement body 2;
Break
...
Default
Statement body n+1;
Break
}
Description of format explanation:
Switch: Indicates that this is a switch statement.
Expression: can be Byte,short,int,char
JDK5 can be enumerated later
JDK7 can be a string later
Case: The value that follows is the value to compare to the expression
Break: Indicates that the program is interrupted here, jumping out of the switch statement
Default: If all cases do not match, execute here, equivalent to the else in the IF statement
(2) Face test
Can a switch statement be an expression of byte?
Could it be a long?
Can it be a string? JDK7 can later
(3) Execution process:
A: Calculate the value of an expression first
B: Match each case, and if there is one, execute the corresponding statement body and see the break end.
C: If there is no match, execute the default statement body n+1.
(4) Precautions:
A:case can only be constants, cannot be variables, and values after multiple case cannot appear the same
Can B:default be omitted?
It can be omitted, but it is not recommended, because it is used to prompt for incorrect conditions.
Special cases:
Case optional values are fixed only a few. That is, case can fix the value.
If only a,b,c,d four options for single radio.
Can c:break be omitted?
can be omitted, but the result may not be what we want.
There is a phenomenon: case penetrating.
In the end, we recommend not omitting
D:default must be at the end?
No, it can be anywhere. But the suggestion at the end.
The end condition of the E:switch statement
A: It's over when you hit the break.
B: Execution ends at the end.
(5) Case:
A: Keyboard input a number (1-7), the output corresponds to the day of the week.
B: Single Choice questions
C: Keyboard Input A string problem
String s = sc.nextline ();
D: According to the given month, the corresponding season of the output
(6) If statement and switch statement respective scene
A:If
For Boolean-type judgments
For a range of judgments
Judging for a few constants
B:switch
Judging for a few constants
------<a href= "http://www.itheima.com" target= "blank" >java training, Android training, iOS training,. NET training </a>, look forward to communicating with you! -------
Black Horse Programmer----Java basic operator, keyboard entry, if switch statement, attached to the question of related surface