Dark Horse programmer ---- java-based operators, keyboard input, if switch statements, related Interview Questions, ---- javaswitch

Source: Internet
Author: User

Dark Horse programmer ---- java-based operators, keyboard input, if switch statements, related Interview Questions, ---- javaswitch

------ Java training, Android training, iOS training, and. Net training. We look forward to communicating with you! -------

1: Operator (master)

(1) Arithmetic Operators
A: +,-, *,/, %, ++ ,--
Note: Only integers can be obtained for integer division. If you want to get a decimal number, you must first change the data to the floating point type. For example, the 3/4 result is 0.1*0.75/4 =.
B: + usage
A: Addition
B: positive.
C: String connector: 'A' + 1 = 98, "hello" + 'A' + 1 = helloa1, 'A' + 1 + "hello" = 98 hello
C: // and %
When the data is used for division, the/operator is obtained, and the % operator is the remainder.
D: ++ and -- Usage
A: They are used to increase or decrease the number of variables by 1. Constants cannot be auto-incrementing or auto-subtracted.
B: Use
** Used separately
Put the data in front of the operation and the effect is the same.
A ++ or ++ a has the same effect.
** Participate in Operation
Put it before the operand: First Auto-increment or auto-increment, and then participate in the operation
Int a = 10;
Int B = ++;
The output result is a = 11 B = 11.
Placed behind the operand: First involved in the operation, then auto-increment or auto-Increment
Int a = 10;
Int B = a ++;
The output result is a = 11 B = 10.
Exercise: int x = 4; int y = (x ++) + (++ x) + (x * 10) = 4 + 6 + 6*10 = 70 the output result is x = 6, y = 70.
(2) value assignment operator
A: =, + =,-=, * =,/=, % =, etc.
B: = the value assignment operator is also the most basic value assignment operator.
Int x = 10; assign 10 to the int type variable x.
C: Extended value assignment operator + =,-=, * =,/=, % =
+ =, Add the left and right, and assign the value to the left variable.
-=, Subtract the left and right, and assign the value to the variable on the left.
* =, Multiply the left and right, and assign the value to the left variable.
/=, Divide the left and right, and assign the value to the left variable.
% =, Perform division on the left and right, and assign the remainder value to the variable on the left.

D: The Extended value assignment operator implies automatic forced conversion.
Interview Questions:
Short s = 1;
S = s + 1;

Short s = 1;
S + = 1;
Which of the above Code has a problem?
A: short s = 1; s = s + 1; there is a problem, because it will lose precision. In s = s + 1, s + 1 is of the int type. Forced conversion is required if you don't want any problems. That is, s = (short) (s + 1)
Short s = 1; s + = 1; no problem, because the extended value assignment operator actually implies a forced type conversion. S + = 1 is not equivalent to s = s + 1, but is equivalent to s = (Data Type of s) (s + 1 ).
(3) Comparison Operators
A: = ,! =,>, >=, <, <=
B: whether the two ends of the operator are simple or complex, the final result is of the boolean type.
C: Do not write = As =
(4) logical operators
A: &, |, ^ ,!, &, |
B: logical operators are used to connect operators of the boolean type.
C: Conclusion
& Logic and: false if false (true only)
| Logic or: true if true (true if true)
^ Logical XOR: if the logic is the same, false is used. If the logic is different, true is used.
For example, the gender of a couple is false.
! Non-logical: false if not true, true if not false

&: The result is the same as &, but it has a short circuit effect. False on the left and no execution on the right.
|: The result is the same as that of |, but the result has a short circuit effect. True on the left and no execution on the right.
Example: int a = 3; int B = 4; boolean c = (++ a = 3) & (B ++ = 4) Result a = 4. B = 4.c= false, B ++ on the right is not executed.
(5) bitwise operators (understanding)
A: To perform bitwise operations, first convert the data to binary to complement the binary ~
Analysis: because it is a bit operation, we must first Replace the data with binary.
Binary 3: 11
00000000 00000000 00000000 00000011 (same as the original code)
Binary 4: 100
00000000 00000000 00000000 00000100 (same as the original code)

& Bitwise operation: 0 equals to 0 (similar to false in logic)
00000000 00000000 00000000 00000011
& 00000000 00000000 00000000 00000100
------------------------------------------
00000000 00000000 00000000 00000000
Therefore, the result of 3 & 4 is 0.

 
| Bitwise operation: one digit is 1.
00000000 00000000 00000000 00000011
| 00000000 00000000 00000000 00000100
------------------------------------------
00000000 00000000 00000000 00000111 SO 3 | 4 the result is 7


^ Bitwise exclusive or operation: 0 for the same bitwise AND 1 for different bitwise
00000000 00000000 00000000 00000011
^ 00000000 00000000 00000000 00000100
------------------------------------------
00000000 00000000 00000000 00000111 so the result of 3 ^ 4 is 7

~ Bitwise Inverse Operator: 0 to 1, 1 to 0
~ 00000000 00000000 00000000 00000011
11111111 11111111 11111111 11111100 (Binary complement of 3)
------------------------------------------
Makeup: 11111111 11111111 11111111 11111100
Anti-code: 11111111 11111111 11111111 11111011 (complement minus 1 to obtain the anti-code)
Original code: 10000000 00000000 00000000 00000100 (reverse code obtained)
So ~ The result of 3 is-4.


B:
I. Special usage of ^
The number of one data bit is the same for another data bit or twice. For example: int a = 10; int B = 20; a ^ B = 10 a ^ B ^ a = 20
II:
<: Left shift: the highest bit on the left is discarded, and 0 is filled on the right. <the data on the left is multiplied by the moving power of 2, for example, 3 <2 = 3*2 ^ 2 = 12; int a = 3 (-- a) <a is equivalent to 2 <2 = 2*2 ^ 2 = 8
> The maximum value of the right shift is 0, and the value of the Left is 0. the highest bit is 1, and the left side is filled with 1. divide the data on the left by the moving power of 2, for example, 24> 2 = 24/2 ^ 2 = 6
>>>: Unsigned shift to the right, whether the highest bit is 0 or 1, fill in 0 on the left; 3 >>> 1 = 3/2 ^ 1 = 1


C: Interview Questions
A: implement the exchange of two variables.
Method 1: Use third-party variables (used in development) such as: int a = 10; int B = 20; int c = a; a = B; B = c;
Method 2: Use bitwise OR operators. (used in the interview) a, B, a, and a ^ B on the left
A = a ^ B;
B = a ^ B; // a ^ B =
A = a ^ B; // a ^ B ^ a = B
Method 3: add variables (understanding)
A = a + B; // a = 30
B = a-B; // B = 10
A = a-B; // a = 30-10 = 20
Method 4: Get it done in one sentence. (Understanding)
B = (a + B)-(a = B); // B = 30-20 = 10, a = 20;

B: Calculate the result of multiplying 2 by 8 in the most efficient way.
2 <3
(6) ternary Operators
A: Format
Comparison expression? Expression 1: expression 2;
B: Execution Process:
First, calculate the value of the comparison expression to see whether it is true or false.
If it is true, expression 1 is the result.
If it is false, expression 2 is the result.
C: case:
A: compare whether the two data items are equal.
Int a = 100; int B = 200; boolean flag = (m = n)
B: obtain the maximum value of two data items.
Int a = 100; int B = 200; int max = (a> B? A: B );
C: obtain the maximum value among the three data items.
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 actual development, data changes. To improve program flexibility, we add a keyboard to input data.
(2) how to implement it? Remember now
A: import package
Import java. util. collections;
Position: above the class
B: Create an object
Pipeline SC = new pipeline (System. in );
C: Get Data
Int x = SC. nextInt ();
(3) Add the case of the ternary operator to the keyboard for improvement.


3: Process Control statement
(1) The sequential structure is executed from top to bottom.
(2) Select the structure and execute different codes according to different options.
(3) duplicate code in the Loop Structure


4: if statement (master)
(1) three formats
A: Format 1
If (comparison expression ){
Statement body;
}

Execution Process:
Determines whether the value of the comparison expression is true or false.
If it is true, the statement body is executed.
If it is false, the statement body is not executed.

B: Format 2
If (comparison expression ){
Statement body 1;
} Else {
Statement body 2;
}

Execution Process:
Determines whether the value of the comparison expression is true or false.
If it is true, execute statement body 1
If it is 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:
Judge the value of comparison expression 1, whether it is true or false
If it is true, execute statement body 1
If it is false, continue to judge the value of comparison expression 2, to see if it is true or false.
If it is true, the statement body 2 is executed.
If it is false, continue to judge the value of comparison expression 3. check whether it is true or false.
...
If none of them are met, execute the statement body n + 1.
(2) Considerations
A: The comparison expression is of the boolean type, whether simple or complex.
B: if the statement body controlled by the if statement is a statement, braces can be omitted. if there are multiple statements, they cannot be ignored.
Suggestion: never omit it.
C: In general, there are left braces, there is no semicolon, there is no left braces.
D: if there is no if after else, no comparison expression will appear.
E: The three if statements are actually one statement. if one is executed, the other statements will not be executed.
(3) case:
A: compare whether two numbers are equal.
B: obtain the maximum value of two numbers.
C: obtain the maximum value among the three values (if statement nesting)
D: output the corresponding grade based on the score
E: outputs the corresponding season Based on the month.
F: Calculate the corresponding y value based on x and Output
(4) Relationship between the ternary operator and the second format of the if statement
All the three-element operators can be implemented, and the second format of the if statement can be implemented.
Otherwise.

If the statement body controlled by the second format of the if statement is an output statement, this is not acceptable.

The ternary operator is an operator and must have a result returned. It 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;
}

Format description:
Switch: This is a switch statement.
Expression: It can be byte, short, int, char
After JDK 5, it can be an enumeration.
After JDK 7, it can be a string.
Case: The following value is the value to be compared with the expression.
Break: indicates that the program is interrupted here and the switch statement exists.
Default: if none of the conditions match, execute this statement, which is equivalent to the else in the if statement.
(2) Interview Questions
Can the expression of the switch statement be byte? Yes,
Can it be long? No
Can it be a String? After JDK 7, you can
(3) Execution Process:
A: Calculate the expression value first.
B: match with each case. If yes, execute the corresponding statement body and the break ends.
C: If no match exists, execute the default statement body n + 1.
(4) considerations:
A: A case can only be followed by A constant, not A variable, and the values after multiple cases cannot be the same
B: Can default be omitted?
It can be omitted, but it is not recommended because it serves as a prompt for incorrect situations.
Special cases:
The optional values of case are only fixed. That is, the value can be fixed in case.
For example, only one choice is available for the, B, C, and D options.
C: Can break be omitted?
It can be omitted, but the result may not be what we want.
There will be a phenomenon: case penetration.
In the end, we recommend that you do not omit
D: Must default be at the end?
No, it can be anywhere. However, we recommend that you complete the process at the end.
E: End condition of the switch statement
A: The break is over.
B: The execution ends at the end.
(5) case:
A: enter A number (1-7) on the keyboard and output the day of the week.
B: Single-choice questions
C: Question about inputting a string on the keyboard
String s = SC. nextLine ();
D: outputs the corresponding season Based on the given month.
(6) scenarios of if and switch statements
A: if
Boolean Type Determination
Determination of a specific range
Determination of several Constants
B: switch
Determination of several Constants



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.