Home >Java >javaTutorial >Example tutorial of operators and type conversion in java
Automatic type conversion (implicit conversion) of sorting from small to large
When a small data type is operated on a large data type, the result will be automatically converted to the large type
byte char short -->int -->long -->float -->double
Note: byte char short are not converted to each other. They participate in operations and are first converted to int type
Format: data type variable with a large range = data type value with a small range
Forced type conversion (display conversion)
Large type data can be converted if the loss of precision can be tolerated Force conversion to small type data
Format: Small range data type Variable = (Small range data type) Large range data type
For constants The symbols that operate on variables are called operators
Using operators to connect constants and a formula that conforms to Java syntax can be called an expression
Commonly used operators: arithmetic operators, assignment operators, relational operators, logical operators, ternary operators
Arithmetic operators: + - * / % ++ --
in java The result of the division operation between two int type data is also an int. The decimal point is cut off directly
Code demonstration:
public static void main(String[] args) {
int a = 10;
int b = 20;
System.out.println(a + b); // 30
System.out.println(a - b); // -10
System.out.println(a * b); // 200
System.out.println(a / b); // 0
System.out.println(1/2.0); //0.5
System.out.println(1/3);//0
System.out.println(2/3);//0
System.out.println("======= ====================");
// When + is used between a string and a number, it indicates a link and ultimately a new string is obtained
int d = 10;
System.out.println("Heyhey"+10);//Heyhey 10
System.out.println("Heyhey"+10+10);//Heyhey 1010
System.out.println("Heyhey"+(10+10));//Heyhey 20
System.out.println(10+10+"Heyhey");//20Heyhey
System .out.println("===========================");
System.out.println(10%3); // Find the remainder (modulo)
System.out.println(7%2);// 1
System.out.println("============== =========");
// 'a' ---> 97 'b'---> 98
// 'A' ---> 65
// '0'---> 48
System.out.println('a' + 10);// 107
System.out.println('A' + 10);// 75
System.out.println('0' + 10);// 58
System.out.println((char)('a' + 1));//b
}
When the ++ operator is used alone, whether the ++ symbol is on the left or right side of the variable, it means that the variable will be incremented by 1
When ++ When operators are used together, if the ++ symbol is on the left side of the variable, the emphasis is on changing it first (incrementing it by 1) and then matching it,
If it is on the right side of the variable, it emphasizes matching it first and then changing it (incrementing it by 1)
When -- is used alone, whether -- is on the left or right side of a variable, it means decrementing the variable by 1
When -- is used in combination, if -- is on the left side of the variable, the emphasis is to change (decrement by 1) first and then match it,
If in On the right side of the variable, emphasize matching first and then changing (decrease by 1)
Code demonstration:
public static void main(String[] args) {
int b = 10;
System.out.println(b++);//10
System.out.println(b);/// 11
System.out.println(" ==================");
int c = 20;
System.out.println(--c);// 19
System .out.println(c);// 19
int d = 30;
System.out.println(d--);//30
System.out.println(d) ;// 29
}
Basic assignment operator:=
Extended assignment operator:+= -= *= /= %=
Change the left and the result on the right is assigned to the left
Note: the left cannot be a constant
implies a forced type conversion
Benefits: more efficient
Code demonstration:
public static void main(String[] args) {
int a = 10;
a+=2; // a = a + (2) --- > a = 10 + (2) ---> a = 12
System.out.println(a);// 12
int b = 20;
b-=2; // b = b - (2) ---> b = 18
System.out.println(b);//18
short c = 10;
//c = (short)(c + 10); // short = short + int ---> short = int
c+=10;
System.out.println(c);//20
}
==(equal) !=(not equal) >(greater than) <(less than) >=(greater than or equal to) <=(less than or equal to)
Relational operators are all of boolean type, either true or false
Code demonstration:
public static void main(String[] args) {
// TODO Auto-generated method stub
int a = 10;
int b = 20;
int c = 10;
System.out.println( a == b);// false
System.out.println( a == c);// true
System.out.println("======================");
System.out. println( a != b);// true
System.out.println( a != c);// false
System.out.println("============ ===========");
System.out.println( a >= b);// false
System.out.println( a >= c);/ / true
System.out.println("======================");
System.out.println( a <= b);// true
System.out.println( a <= c);// true
System.out.println("================ =======");
System.out.println( a > b); // false
System.out.println( a > c); // false
System .out.println("======================");
System.out.println( a < b);// true
System.out.println( a < c);// false
System.out.println("======================" );
}
Logical operators are used to connect Boolean expressions and the final result value is Boolean.
In Java, it cannot be written as 3
Not: ! Operation rule: true Change false False to true
XOR: ^ Operation rules: If both sides are the same, it is false, if they are different, it is true
Code demonstration:
public static void main(String[] args) {
System.out.println(false & true );// F
System.out.println(true & false );// F
System .out.println(false & false );// F
System.out.println("=========================") ;
System.out.println(true | true ); // T
System.out.println(false | true );// T
System.out.println(true | false );/ / T
System.out.println(false | false );// F
System.out.println("====================== ====");
System.out.println(!true);// F
System.out.println(!false);// T
System.out.println("= ========================");
System.out.println(true ^ true); // F
System.out. println(false ^ true );// T
System.out.println(true ^ false );// T
System.out.println(false ^ false );// F
}
Short circuit and: &&
Short circuit or: ||
Advantage: higher efficiency
Short circuit and: &&
The basic operation rules are similar to &, the difference is that if the left is false, the right side is not executed, the result is false
short circuit or: ||
The basic operation rules are similar to |, the difference is that if the left side is true, the right side is not executed, the result true is directly returned
三Meta-expression:
Expression 1 ? Result 1 : Result 2
Execution process:
First calculate the result of expression 1
If the result is true, return result 1, otherwise return Result 2
Code demonstration: (Get the larger of two numbers. )
(int x=3,y=4,z;z = (x>y)?x:y;//The z variable stores the large number of the two numbers.)
public class Demo09Operator {
public static void main(String[] args) {
int a = 10;
int b = 20;
int c = (a > b) ? a : b;
System.out.println("c:" + c);
}
}
(Compare whether two data are the same)
public class Demo10Operator {
public static void main(String[] args) {
// Define two variables of type int
int a = 10;
int b = 20;
boolean flag = (a == b) ? true : false;
// boolean flag = (a == b);
System. out.println(flag);
}
}
(Get the maximum value among three integers)
public class Demo11Operator {
public static void main(String[] args ) {
// Define three int type variables
int a = 10;
int b = 30;
int c = 20;
// Compare the two first Large value of integer
int temp = ((a > b) ? a : b);
int max = ((temp > c) ? temp : c);
System.out.println ("max:" + max);
}
}
The above is the detailed content of Example tutorial of operators and type conversion in java. For more information, please follow other related articles on the PHP Chinese website!