Thinking in Java----reading Note (3)

Source: Internet
Author: User
Tags arithmetic arithmetic operators bitwise bitwise operators case statement goto integer division logical operators

# Thinking in Java 4th
# reading Note
# Victor
# 2016.03.13

Chapter 3 Control Program Flow

3.1 Java Operators
Almost all operators can only manipulate "base value types". " = "," = = "and"! = "can manipulate all objects, and the string class supports" + "and" + = ".

3.1.1 Priority level
The precedence of an operator determines the order in which a part of an expression is evaluated when there are multiple operators. It is recommended to use "()" to define the order of calculation.

3.1.2 Assignment Value
The assignment is done using the "=" operator, which means "get the value on the right and copy it to the left." The value on the right can be any constant, variable, or expression, as long as you can produce a value. But the value on the left must be a definite named variable. In other words, it must have a physical space to hold the value on the right.
For an assignment of a base value type, the above copy operation is implemented because the base value type holds the actual value.
However, when the object is assigned a value, the situation has changed. Because when we operate on an object, we manipulate its handle, so a=b is actually copying the handle. So a and B point to the same object.
In addition, when an object is passed as a method parameter, it is actually passed a handle.

3.1.3 Arithmetic operators
The basic arithmetic operators of Java include "+", "-", "*", "/" and Modulus "%" (take-away). Where integer division removes decimals directly, rather than rounding.
Shorthand: Combine the basic arithmetic operators with the equals sign and implement assignment operations. " + = ","-= "," *= ","/= "and"%= ".
Unary operators "+" and "-", respectively, represent positive negative values.

3.1.4 Auto Increment and decrement
++a,--A: The prefix form is executed before the value is obtained.
a++,a--: A value is obtained before the suffix form operation.

3.1.5 Relational operators
Relational operators evaluate the relationship between operand values and generate a Boolean value. Operators include "<", ">", "<=", ">=", "= =", and "! =".
Usage: Determine if objects are equal
Code
Integer n1 = new Integer (47);
Integer n2 = new Integer (47);
SYSTEM.OUT.PRINTLN (n1 = = N2);
SYSTEM.OUT.PRINTLN (n1! = n2);
Out:false, True
Reason: Although the objects are the same, but the handles are different, and "= =" and "! =" Compare exactly the object handle.
Solution: If you want to compare the contents of two objects, you can use the method equals () for all objects. But for the base value type, use the = = and! = directly. Note that the Equals () method in your own class needs to implement its own comparison of object content (@ Override).

3.1.6 logical operators
"&&" (and), "| |" (OR), "!" (not) can generate a Boolean value.
Short Circuit: The expression is logically evaluated only if it is a definite conclusion that the whole expression is true or false. Therefore, all parts of a logical expression may not be evaluated.

3.1.7 bitwise operator
The bitwise operator allows us to manipulate a single bit, that is, bits.
"&" (and) and, "|" (or) or, "~" (non) Not, "^" (XOR).
Note: Boolean bitwise operators have the same effect as logical operators, except that they do not "short-circuit" midway

3.1.8 shift operator
The operand to which the shift operator is oriented is also the binary "bit". The left shift operator ("<<") moves the operand to the left of the operator to the left with the number of digits specified to the right of the operator (low 0).
The "signed" Right Shift operator (>>) moves the operand to the left of the operator to the right by the number of digits specified to the right of the operator. " The signed right shift operator uses the symbol extension: If the value is positive, insert 0 at the high, or 1 if the value is negative. Java has also added an "unsigned" right shift operator (>>>) that uses the "0 extension": either positive or negative, it inserts 0 in the high position.
If Char,byte or short are shifted, they are automatically converted to an int before the shift is made. Only the 5 lows on the right will be used. This prevents us from moving an impractical number of digits in an int number. If a long value is processed, Finally, the result is long. Only the 6 lows on the right are used to prevent moving beyond the ready-made digits in the long value. However, you may also encounter a problem when you move the "unsigned" right shift. If a right shift operation is performed on a byte or short value, May not be the right result. They are automatically converted to the int type and are shifted right.

3.1. $93 If-else operator
Boolean expression? Value 0: value 1. If the Boolean expression is true, calculate "value 0", or "value 1" if False.

3.1.10 comma operator
The comma operator in Java is used only in the For loop.

3.1.11 string operator "+"
This operator is used in Java to concatenate different strings.
Note: If an expression begins with a string, all subsequent operands must be strings.

3.1.12 modelling operator
The effect of "styling" (cast) is to convert one data type into another data type in Java.
For example: int i = 200; Long i = (long) i;
Java allows us to "sculpt" any of the main types into any other main type, except for the Boolean. Typically, the largest data type in an expression is the type that determines the final result size of the expression. If you multiply a float value by a double value, the result is a double, and if you add an int and a long value, the result is long. Obviously, the modeling of large data types to small data types can result in loss of data.

3.2 Execution Control
If-else
While
Do-while
For
Switch

3.2.1 True and False
All conditional statements use the true or false of a conditional expression to determine the execution process. An example of a conditional expression is a==b. It uses the conditional operator "= =" to determine whether a value is equal to the B value. The expression returns TRUE or false.

3.2.2 If-else
if (Boolean expression) {}
else{}

3.2.3 While
while (boolean-expression)
{}

3.2.4 Do-while
do{}
while (boolean-expression)

3.2.5 for
for (initial expression; boolean-expression; stepping)
{}

3.2.6 interrupts and continues
In the body part of any loop statement, you can also use break and continue to control the flow of the loop. Where break is used to forcibly exit the loop without executing the remaining statements in the loop. The continue then stops executing the current iteration and then returns to the start of the loop, starting with a new iteration.
Goto is not implemented in Java, but the two keywords, break and continue, use the same mechanism as goto: tags.
Example:
Label1:
for{
for{
if () {
...
Break Label1;
...
}
}
}

3.2.7 Switch
Switch (integer selection factor) {
Case Integer value 1: statement; Break
Case Integer value 2: statement; Break
...
Default: statement;
}

In the definition above, you will notice that each case ends with a break. This allows the execution process to jump to the end of the switch body. This is a traditional way of building a switch statement, but break is optional. If you omit break, the following case statement continues Code until a break is encountered. Although this is not usually the case, it may be good for experienced programmers.
Note that the last default statement has no break because the execution process has reached the break's jump destination. Of course, if you consider the programming style, you can put a break at the end of the default statement.

Thinking in Java----reading Note (3)

Related Article

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.