General goal: learn JAVA
Goal of this week: getting started with Java (6.5-6.12)
Today's goal: (June 11)
- Master the usage of various operators
- Master the usage of various expressions
- Master the relationship between expressions and operators
- Master the conversion skills of data types in expressions
- Master the use of increasing and decreasing operators
operator
The Java language supports the following operators
-
Arithmetic operator
Symbol name effect + plus Add up — minus sign subtract * multiplication sign Multiply / division sign be divided by % model Surplus ++ Increasing Variable value plus 1 - - Diminishing Variable value minus 1 -
Assignment operation
Symbol name effect = Assignment operator assignment -
Relational operator
Symbol name effect > greater than greater than < less than less than == be equal to be equal to != Not equal to Not equal to instanceof Test whether the object on its left is the instance of the class on its right, and return the data type of boolean -
Logical operator
Symbol name effect && And A and B, AB in series || or A or B, AB in parallel ! wrong Logical non -
Bitwise Operators
-
Conditional operator
The conditional operator consists of "?: "Composition", whose basic form is a?x:y, that is, if the value of a is not 0, the value of the whole expression is x, otherwise it is y.
-
Extension operator
operator
Arithmetic operator
package operator; //Operator operator public class Demo01 { public static void main(String[] args) { //Binary operator int a = 10; int b = 20; int c = 25; int d = 25; System.out.println(a+b); System.out.println(a-b); System.out.println(a*b); System.out.println(a/b);//Strong conversion to double (a/(double)b); System.out.println(a%b); } }
Operation results
30
-10
200
0
10
data type
package operator; public class Demo02 { public static void main(String[] args) { long a = 121212123123l; int b = 123; short c = 10; byte d = 8; System.out.println(a+b+c+d);//long System.out.println(b+c+d);//int System.out.println(c+d);//int //cast conversion } }
Operation results
121212123264
141
18
Relational operator
package operator; public class Demo03 { public static void main(String[] args) { //Results returned by key operators: correct, wrong, Boolean int a = 10; int b = 20; int c = 21; //Remainder - also known as modular operation System.out.println(c%a);//Residual c/a 21/10=2...... 1 System.out.println(a>b);//false System.out.println(a<b);//true System.out.println(a==b);//false System.out.println(a!=b); } }
Increment and decrement operators
Increment and decrement operators | significance |
---|---|
++ | Increment, variable value plus 1 |
- | Decrement, variable value minus 1 |
package operator; public class Demo04 { public static void main(String[] args) { //++Self increasing -- self decreasing unary operator int a = 3; //After executing this line of code, assign a value to b first, and then increase it by itself int b = a++;//A + + = a = a + 1, assign value first and then add one System.out.println(a);//4 int c = ++a;//Before the execution of this line of code, increase it by itself, and then assign a value to b System.out.println(a);//5 System.out.println(b);//3 System.out.println(b);//3 System.out.println(c);//5 System.out.println(c);//5 //Power operation 2 ^ 3 = 2 * 2 * 2 = 8 double pow = Math.pow(3,2); System.out.println(pow);//9.0 System.out.print("a="+a); //Output a System.out.println(",a++="+(a++)+",a="+a);//Output a + + and a //a=5,a++=5,a=6 System.out.print("b="+b); //Output b System.out.println(",++b="+(++b)+",b="+b);//Output + + b and b //b=3,++b=4,b=4 } }
Logical operator
Logical operator | significance |
---|---|
&& | AND, AND |
|| | OR, OR |
! | !, Negative, negative |
package operator; //Logical operator public class Demo06 { public static void main(String[] args) { //And (and) or (or) not (negative) boolean a = true; boolean b = false; System.out.println("a && b:"+(a&&b));//Logic and operation: if both variables are true, the result will be true System.out.println("a || b:"+(a||b));//Logical or operation: if one of the two variables is true, the result is true System.out.println("!(a && b):"+!(a && b));//If true, the output is false; If false, the output is true //Short circuit operation: b&&a because B is false, and will not be calculated again //Verify short circuit operation int c = 5; boolean d = (c<4)&&(c++<4);//If c + + < 4 is executed, the output of c is 6 System.out.println(d);//false System.out.println(c);//5 } }
Operation result
a && b:false
a || b:true
!(a && b):true
false
5
Bitwise Operators
Bitwise Operators | significance |
---|---|
& | Logic and |
\ | Logical or |
^ | Logical XOR |
~ | Logical inversion |
>> | sftr |
<< | Bit shift left |
package operator; public class Demo07 { public static void main(String[] args) { /** * A = 0011 1100 * B = 0000 1101 --------- * A&B = 0000 1100 * A|B = 0011 1101 * A^B = 0011 0001 The XOR is 0 and the difference is 1 * ~B = 1111 0010 * >> Shift right / 2 * << Shift left * 2 * */ System.out.println(2<<3); // 2 =0000 0010 //2 shift left 3 bits = 0001 0000 = 16 } }
***
Operators, expressions and statements are the main components of programming, which can make the system operate the memory directly, and then improve the execution ability of the program. Today, learn the use of arithmetic operators, which can assign values to variables and constants, monocular operations (variables) and arithmetic operations; Relational operators, learning the use of judgment statements and increasing and decreasing; Logical operators, deepen the use and understanding of conditional statements, and understand the generation of some operation errors through the use of operator priority and; Bitwise operators are used directly for memory, and the scope is binary. They can perform faster operations.
-
Bracket operators are used to handle the priority of expressions.
-
The result of an arithmetic expression is a numeric value.
-
Rules for Java expression type conversion
Adhering to the principle of "no loss of data as the premise", different types of conversion can be carried out, so that different types of data and expressions can continue to be stored.
1. The type with less occupied bytes can be transformed into the type with more occupied bytes.
2. char will be converted to int
3. int type will be converted to float type
4. If one operand in the expression is of double type, the other operand will also be converted to double type
5. boolean type cannot be converted to other types -
Evaluate the value of the expression "((123456799) > (976543213))? true:false".
public class conditiondemo6 { public static void main(String[] args) { boolean t = ((12345679*9)>(97654321*3))?true:false; System.out.println("(123456789*9)>(97654321*3):"+t);//false } }