Java language supports the following operators:
Arithmetic operators: +, -, *, /,%, +, -
Relational operators:>, <,> =, <=, =,! = =
Logical Operators:! , &, |, ......, &&, ||
Bitwise operators: &, |, ^, ~, >>, <<, >>
Assignment operator: =
Extended operator: + =, - =, * =, / =
Even string operators: +
// ^: XOR
From Canada and decrement operators:
public class Test{ public static void main(String[] args){ int i1 = 10, i2 = 20; int i = (i2++); System.out.print("i=" + i); System.out.println("i2=" + i2); i = (++i2); System.out.print("i=" + i); System.out.print(" i2=" + i2); i = (--i1); System.out.print("i=" + i); System.out.println(" i1=" + i1); i = (i1--); System.out.print("i=" + i); System.out.println(" i1=" + i1); } }
Logical Operators
Short-circuit and / or short-circuit: As long as the first operand is identified, the second operand is not evaluated
The assignment operators extended assignment operator
Assignment operator (=)
When the "=" both data types do not match, a default type converter can be applied or cast process principles
long 1 = 100; int i = (int)1;
Note: integer constant can be assigned directly to a byte, short, char type variable, without the need for cast, as long as the number does not exceed the range of the table
byte b = 12; char c = 100;
x byte bb = 256; X short s = -32769;
String connector
"+" Except for the arithmetic addition, it also can be used for string connection operation
int id = 800 + 90;
String s = "hello" + "world";
"+" On both sides of an operand as long as there is a string (String) type, the system will automatically change the other operand to a string and then grip connection
int c = 12;
System.out.println("c=" +c);
Trinocular conditional operator
"Three unary conditional operator, syntax":
x?y:z
Wherein x is a boolean type expression, to calculate the value of x, if it is true, the result of computing the entire three head value of the expression of y, or the whole calculation result is a value of the expression z
example:
int score = 80; int x = -100; String type = score < 60 ? "不及格" : "及格" int flag = x > 0 ? 1 : (x == 0 ? 0 : -1); System.out.println("type=" + type); System.ouit.println("flag=" + flag);
Statement
Conditional statements - according to different conditions, perform different statements.
- if
- if..else
- if..else if
- if..else if..else if ..else
- switch
Loop - repeat certain actions
- for
- while
- do..while;
The if statement
- if
- if..else
- if..else if
- if..else if..else if
- if..else if..else if..else
- Only when a statement is to be executed may be omitted {}
public class TestIF{ public static void main(String[] args){ int = 20; if(i <20){ System.out.println("<20"); System.out.println("<20"); }else if (i < 40 ){ System.out.println("<40"; }else if(i < 60){ System.out.println("<60"); }else ( System.out.println(">=60"); System.out.println(">=60"); } }
{} else to bring
for loop
- for statements of the form:
for (Expression 1; 2 Expression; Expression 3) {statement; ....;}
- Implementation process
First, even if the expression 1, expression 2 then performed, if the value of the expression 2 = ture, the statement is executed, and then evaluate the expression 3, and then determines the expression value of 2; followed by repeated, the value of the expression 2 = guide flase
for three expressions can be omitted statement
Example: Calculation result = 1 +2 + ... + 10!!!
public class Test{ public static void main(String[] args){ long result = 0; long f = 1; for(int i = 1;i <= 10; i++){ f = f * i; result += f; } System.out.println("reslut=" + result); } }
Programming, the value of 1 + ...... + 3 + 5 + 7 + 99 is calculated by a for loop, and outputs the calculation result. (OddSum.java)
public class OddSum{ public static void main(String[] args){ long reslut = 0; for(int i = 1;i < 100;i=+2){ result += i; } System.out.println("reslut=" + result); } }
while & do while 语句
public class TestWhile{ public static void main(String[] args){ int i = 0; while(i < 10){ System.out.println(i); i++; } i = 0; do{ System.out.println(i); i++; }while(i < 10); } }
Conditions are used () enclosed, statements are enclosed by {}.
break & Continue statement
break statement terminates execution of a block of statements used in the loop can be forced to exit the loop:
E.g:
public class Test{ public static void main(String agrs[]){ int stop = 4; for(int i = 1; i <= 10; i++){ //当i等于stop,退出循环 if( i == stop)break; System.out.println("i = " +i); } } }
continue statement is used in the loop to terminate a process cycle, the cycle is skipped continue statement following the loop is not executed, the next cycle begins:
E.g:
public class the Test { public static void main (String [] args) { int Skip =. 4 ; for ( int i =. 1; i <=. 5; i ++ ) { // if i is equal to skip, jump level loop IF (i = Skip =) Continue ; System.out.println ( "I =" + I); } } }
continue: terminate the cycle, the next cycle begins.
Loop example
// output may be a number divisible by 3 before 5 1 ~ 100.
public class Test{ public static void main(String[] args){ int num = 0, i = 1; while(i<=100){ if (i % 3 ==0){ System.out.print(i +" "); num++; } if(num == 5){ break; } i++; } } }
// output prime number within the 101 to 200:
public class Test{ public static void main(String[] args){ for(int i = 101; i<200; i+=2){ boolean f = true; for(int j = 2; j<i; j++){ if(i % j == 0){ f = false; break; } } if(!f){continue;} System.out.print(" " + i); } } }
switch statement (conditional statement supplement)
switch(){
case xx:
....
case xx:
....
default:
....
}
Be careful case penetrate recommended break statement
Together may be incorporated into a plurality of case
default can be omitted, but not recommended omitted
switch
java switch statement can only be detected in an int value
public class TestSwitch{ public static void main(Sting[] args){ int i = 18l switch(i){ case 8; //System.out.println("2"); //break; cast 3: //System.out.println("3"); //break; case 2 : System.out.println("8"); case 9: System.out.println("9"); break; default: System.out.println("error"); } } }
method
The method is similar to Java function of other languages, was a fragment of code to accomplish a specific function, the statement format:
[1 Modifier Modifier 2.] A method return type names (form parameter list) {
Java statement: ... ... ...
}
Formal parameters: external input for receiving data to be used in the methods section.
Argument: the actual data passed to the method when calling the method.
Returns: method to return to the calling environment in its data after the implementation.
Return Value: a pre-agreed return value data types, such as non-return value, the return value must be given type void.
Java language using the following method call form: object name, method name (argument list)
The number of actual parameters, data types, and the order must be declared method called matching parameter list,
Run termination method return statement and specify the data returned.
Java in principle performed when parameters are passed in a function call, following the transfer of:
Basic types of data transfer that the value itself. Reference types passed a reference to the object, not the object itself.
example:
public class TestMethod{ public static void main(String[] args){ m(); m2(2); m3('3',4); m4(4, 6); int i = m4(4, 6); System.out.println(i); } public static void m(){ //return; System.out.println("ok"); System.out.println("hello"); } public static void m2(int i){ if(i > 3) return; System.out.println(i); } public static void m3(int i ,int j){ System.out.println(i + j); } public static int m4(int i , int j){ return i > j ? i : j; } }