Conversion of operators and types in java, and conversion of java Operators

Source: Internet
Author: User

Conversion of operators and types in java, and conversion of java Operators
Type conversion:

Automatic type conversion (implicit conversion) for sorting from small to large)
When small data types and large data types are computed, the results are automatically converted to large data types.
Byte char short --> int --> long --> float --> double
Note: byte char short does not convert each other. They are involved in the operation and are first converted to the int type.

Format: large data type variable = Small Data Type Value


Forced type conversion (display conversion)
You can forcibly convert large data types into small data types when you can tolerate the lack of precision.
Format: Small Data Type Variable = (small data type) Large Data Type

Concepts of operators:

Operators are symbols used to operate constants and variables.

Expression concept:

Operators are used to connect constants. Expressions are expressions that conform to the java syntax.

Common operators: Arithmetic Operators value assignment operators Relational operators logical operators ternary Operators


Arithmetic Operator: +-*/% + + --
In java, the result of the division operation on two int-type data is that int is used to directly remove the decimal point.

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, the link is used to obtain a new string.
Int d = 10;
System. out. println ("" + 10); // 10 10
System. out. println (" hey" + 10 + 10); // 1010
System. out. println ("" + (10 + 10); // 20 20
System. out. println (10 + 10 + ""); // 20

System. out. println ("========================= ");
System. out. println (10% 3); // obtain 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

}

++ OPERATOR:

When the ++ operator is used independently, whether the ++ symbol is on the left or right of the variable, it indicates that the variable is incremented by 1.
When the ++ operator is used together, if the ++ symbol is on the left of the variable, it is emphasized that it is changed first (auto-increment 1) and then used together,
If it is on the right side of the variable, emphasize cooperation first and then change (auto-increment 1)

-- Operator:

When -- used independently, whether -- on the left or right of the variable, it indicates that the variable is auto-reduced by 1.
When -- used together, if -- on the left of the variable, it is emphasized that the variable is changed first (minus 1) and then cooperated,
If it is on the right side of the variable, emphasize cooperation first and then change (Auto minus 1)
Code Demonstration:
PublicStatic 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
}


Assignment operator:


Basic Value assignment operator: =
Extended value assignment operator: + =-= * =/= % =
Assign the left and right results to the left.
Note: The left side cannot be a constant.
Implicit 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

}

 


Relational operators:

= (Equal )! = (Not equal to)> (greater than) <(less than) >=( greater than or equal to) <= (less than or equal)
All Relational operators are boolean, 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 (! = B); // true
System. out. println (! = 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:

The logical operator is used to connect a Boolean expression and the final result value is boolean.
In java, 3 <x <6 should be written as x> 3 & x <6
And: & operation rule: if either side is false, the result of the entire expression is false, and true is true only when both sides are true.
Or: | operation rule: if either side is true, the result of the entire expression is true. Only when both sides are false, the result is false.
Non :! Calculation rule: true or false or true
XOR: ^ operation rule: the two sides are the same as false, and the difference is true.
Code Demonstration:
Public static void main (String [] args ){

System. out. println (true & true); // T
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: |
Benefits: higher efficiency
Short circuit and :&&
The basic operation rules are similar to the operation rules. The difference is that if the value on the left is false and the operation on the right is not executed, the result is directly false.
Short circuit or: |
The basic operation rule is similar to |. The difference is that if true is left and no execution is performed on the right, true is returned directly.
Ternary 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: (obtain the large numbers in two numbers .)
(Int x = 3, y = 4, z; z = (x> y )? X: y; // The z variable stores the large numbers of 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 the two data items are the same)
Public class Demo10Operator {
Public static void main (String [] args ){
// Define two int-type variables
Int a = 10;
Int B = 20;

Boolean flag = (a = B )? True: false;
// Boolean flag = (a = B );
System. out. println (flag );
}
}

(Obtain the maximum value among the 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 two integers first
Int temp = (a> B )? A: B );
Int max = (temp> c )? Temp: c );
System. out. println ("max:" + max );
}

}

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.