Chapter III: Expressions and Flow Control

Chapter III: Expressions and Flow Control
One: local variables and instance variables
define the variable refers to the name and data type of a variable set of variables,
the Java language requirements defined variables follow the first, then initialize, then the rules of use.
Scope: refers to the range of its existence, and only in this context, the program code to access it.
Life-cycle variables refers to a variable is created and assigned memory space from the beginning
to the process of this variable is destroyed and cleared it take up memory space of
local variables (parameter variables can also be seen as a local variable):
1) Location: Definitions or {} in the method in method
2): first assignment using
3) scope: the method defined or as defined in {}
4) life cycle: from the definition of the variables to the method call ends
instance variable:
1 ) position: the method defined outside, inside the class
2): the system will default initialization
3) scope: the role of the entire class
4) the life cycle: the object created when the memory recovery end gc
example local variables:
public void the method1 () {
int a = 0; // local variables, the entire scope method01 method;
{int B = 0; // local variables, the scope of which the code block;
B = a;
}
B = 20 is; / / compiler error, b can not be accessed;
}
examples instance variable:
class {the Test
private int n1=0;
private int n2=0;

public int add() {
int result = n2 + n2;
return result;
}
}

II: Operation: Operator expression can be composed of a corresponding type of data, corresponding to complete the operation.
a) mathematical operators
+ data type value addition or concatenation;
System.out.println (. 1 + 2 + "A"); // output. 3A
System.out.println (. 1 + 2.0 + "A") ; // output 3.0a
System.out.println (. 1 + 2.0 + "A" + to true); // output 3.0atrue
System.out.println ( "A" +. 1 + 2); // output A12
the System.out .println (1+ "a" +2) ; // output 1A2
/ divisible, such as the operands are integers, the operation result is an integer part of the quotient
int a1 = 12/5; // a1 variable value is 2
int a2 = 13/5; // a2 variable value is 2
int A3 = -12 /. 5; the value of variable // a3 -2
int A4 = -13 /. 5; the value of variable // a4 - 2
int. 1 A5 = /. 5; // a5 variable value is 0
Double A6 = -12 /. 5; // a6 variable value is -2.0
Double A7 = -12 / 5.0; // a7 variable value -2.4
% modulus operator, such as the operands are integers, the operation result is an integer part of the quotient
int a1 = 1% 5; value // a1 is variable. 1
int = 13 is A2. 5%; // variables A2 the threshold is. 3
Double A3 =. 5. 1%; // a3 variable value is 1.0
double a4 = 12% 5.1; // a4 variable value is 1.8000000000000007

b) assignment operator:
=: int X = 0, I =. 1, J =. 1;
* =: where "* =" the operator "*" and "=" composite, which is equivalent to a = a * b;
this compound operator can program becomes more compact.
/ =: a / = b is equivalent to A = A / B;
% =:% A = B is equivalent to A% B = A;
...
Note: + + = and distinction
as: short a = 0;
b = 123456 int;
a = b +; and a = a + b, the difference
+ = system implicit data type conversion, the data type conversion = left.
a + b to be the high data type conversion type

c) comparison operators
> Greater than
> = Greater than or equal to
<Less than
<= Less than or equal to
the above operator applies only to the type integer and floating-point type;
int. 1 = A, B =. 1;
Double D = 1.0;
Boolean RESULT1 = A> B ; // result1 value to false;
Boolean result2 = a <B; // result2 value to false;
Boolean result3 = a> = D; // result3 value to true;
Boolean result4 = a <= B; // result4 value of true;

instanceof: determining whether a reference object types is referenced by an instance of the class.
The operator of an object is left, the right is a class or interface name. The following form:
as: STR String = "Hello"
System.out.println (STR the instanceof String);
System.out.println (STR the instanceof Object);
System.out.println (STR the instanceof Student); // to false
the java.lang .Object is the parent class of all classes,
each class will by default inherit Object
subclass is a class that is a parent's relationship

d) the equality operator
== equal
! = not equal to
either a base type, the type of reference may be:
basic data type is real value comparison
reference type is an address comparison, if you want to compare the actual data using the reference type equals ( ) method
as: A = int. 1, B =. 1;
System.out.println (A == B); // output true;
as: String S1 = "Hello";
String S2 = "Hello";
String S3 = new new String ( "Hello");
String String new new S4 = ( "Hello");
System.out.println (== S1 S2); to true //
System.out.println (== S3 S4); // to false
the System. Out.println (== S1 S3); to false //
System.out.println (s3.equals (S4)); // to true

e) the shift operator for binary operations
>> arithmetic right shift operation, also called a signed right shift operation. Up to fill the sign bit.
>>> logical right shift operation, also known as unsigned right shift operation.
<< left shift operation, also known as unsigned left shift operation.

f) Bitwise Operators
&: the arithmetic computation rule:. 1 & l->. 1,. 1 & 0-> 0, 0 & l-> 0, 0 & 0-> 0;
|: or calculation, calculation rule: 1 | 1-> 1 , 1 | 0-> 1, 0 | 1-> 1, 0 | 0-> 0;
^: XOR calculation rule: ^ 1 1-> 0, 1 ^ 0-> 1, 1- 0 ^ > 1, 0 ^ 0-> 0;
values are the same as 0, is different from 1;
-: negated operation, ~ 1-> 0, ~ 0-> 1;

例如:8>>2====>8/2^2=2
8:0000...00001000
0000.........10 ====>2
8>>>3=====>8/2^3=1
8:0000...00001000
00000000.000001=====>1
8<<2======>8*2^2=32
8:0000...00001000
000000000100000======>32
1&1=1 1&0=0 0&0=0
1|1=1 1|0=1 0|0=0
1^1=0 1^0=1 0^0=0
~1=0
~0=1
8&2=0 8|2=10 8^2=10
~8=
~2=

1010
10 ===> === third position 1> 14
10 ===> === Second Cleared>. 8
10 ===> a first position trans ===>. 11
10 ===> Output the corresponding binary

f) Logical Operators
shorting operator, if the left operating according to the Boolean expression
can calculate the entire Boolean expressions, the operation is not performed
on the right breaks Boolean expression;
&&: Boolean expression is left false, affirmed the value of the entire expression is false,
this time ignores Boolean expressions to perform right.
||: the left Boolean expression evaluates to true, the entire expression value is certainly true,
this time ignores Boolean expressions to perform right.
if (&& Condition 1 Condition 2)} {
IF condition 1 is false, the execution condition is not 2
IF Condition 1 is true, the condition 2 is executed
if (|| Condition 1 Condition 2)} {
IF Condition 1 is true, does not perform condition 2
IF condition 1 is false, performs condition 2

g) the conditional operator
Boolean expressions Expression 1:? 2 expression
if the Boolean expression evaluates to true, the expression returns the value of 1, otherwise it returns the value of expression 2.
Score = 61 int;
String = the Result Score> = 60 "failed": "fail";?

H) +, -
the front and rear ++ ++
pre - and post -
int = 10 A;
B = A + ================> =. 11 A, B =. 11, calculated first, after the assignment
b = a ++ =====> a = 11, b = 10, the first assignment, the calculation
System.out.println ( "a:" + a + "b:" + b);

Three data type to
a) basic data type conversion
implicit data type conversion: small precision data to a precision large data
force (explicit) data type conversion: (type) greater precision data to precision fractional data
System.out.println ((int) (char) (byte) -1);
data type conversion time Note:
a) If the data into small precision greater precision data.
If before the turn is signed, the sign bit in front of the fill
before forwarding if the number is unsigned, up front 0
B) if large data into a precision small precision data.
Steal bits from the lower
2) reference data type conversion
implicit data type conversion: Conversion parent subclass
force (explicit) data type conversion: Conversion parent subclass
String STR = "Hello";
Object STR = O;
; String str2 = (String) o
four statements.
will be executed in some code that only certain condition is satisfied: a) conditional statement
1) if the else ...
a behind if expression must be a Boolean expression, and. It can not be a numeric type, for example, the following if (x) is illegal.
B. If the block of code if statement or else statement includes multiple statements, it must be placed inside the braces {}.
If only one block of program code statement can not braces {}. Flow control statements (such as if ... else statement)
can be viewed as a statement.
c. may only execute a branch, it is impossible to perform a number of branches

2) switch
. A condition of the switch type must be byte, short, char or int;
may be used after a String jdk1.7.
. B clause different values in each case, the data types must switch the data type and conditions of the same;
C when the value of the switch expression does not match any case clause, program execution default clause.
If there is no default clause , the program directly out of the switch statement.
default clause can be located anywhere in the switch statement.
d. If the switch expression matches a case expression, or match the default case,
it started from this case or default clause clause.
If you encounter break, withdrew from the entire switch statement, otherwise the switch statement in order to perform a subsequent case clause
no longer check the value of the case expression.
e. switch statement function can also be achieved with the if ... else statement.
But the switch statement makes the program more concise, more readable. And if ... else more powerful.

b) loop
action of the loop is repeatedly performed a piece of code, until the conditions are not satisfied until the cycle.
Loop should generally include the following four parts:
I) initialization section: to set the number of cycles of initial conditions,
such as the loop control variable initial value;
II) Cycling conditions: This is a Boolean expression,
each cycle to be the expression is evaluated
to determine the cycle continues or terminates the loop in the end.
iii) loop: This is the main content loop operation, a statement may be, or may be multiple statements;
IV) portion of the iteration: used to change the value of the loop control variable,
thereby changing the value of the loop condition expression;
1) for
syntax: for (initialization section; cycling conditions; iteration section) {
loop
}
2) the while
syntax: [initialization part]
the while (loop condition) {
loop body portion comprises an iterative
}

When the loop condition is true, then repeat the cycle, otherwise terminate the loop;


3) do..while
and while very similar, except that the first iteration of the loop, and then determine the loop condition.
Syntax: [initialization part]
do {
loop body portion comprises an iterative
} while (loop condition);
4) the flow goes loop statement
a) break: terminating the current or specified cycle;
B) Continue: skip this cycle, performed the next cycle,
or a loop identified by numeral performed.
c) label: label used to identify the program statement label name can be any legal identifier.
continue statement identification must be defined while, do ... while the front and the for loop statement;
BREAK statement identification must be defined while, do ... while or for loop statement in front of the switch statement;

Guess you like

Origin www.cnblogs.com/Diyo/p/11079286.html