Well off with you to learn JAVA -------- operators and expression type conversion, java -------- Operators

Source: Internet
Author: User
Tags bitwise operators

Well off with you to learn JAVA -------- operators and expression type conversion, java -------- Operators

The following lists the priorities of operators. Smaller numbers indicate higher priority.

Java also has some very concise writing methods, which combine Arithmetic Operators and value assignment operators into new operators. These operators are listed below.

 

When the int type meets the float type, who is the "Winner? We have mentioned data type conversion before. Here, we will discuss the type conversion of expressions in detail.

Java is a very flexible programming language. When the above situation occurs, you only need to adhere to the principle of "no data loss as the premise" to do different types of conversion, store different types of data and expressions. According to the general principle, when Java finds that there are type inconsistencies in program expressions, it will process type conversion according to the following rules.

 

1. convert a type that occupies less bytes to a type that occupies more bytes.

2. The character type is converted to the int type.

3. The int type is converted to the float type.

4. If the type of an operand in an expression is double, another operation number is converted to double.

Type.

5. The boolean type cannot be converted to another type.

 

01 // The following program illustrates the automatic conversion of expression types

02 public class TestJava3_22

03 {

04 public static void main (String [] args)

05 {

06 char ch = 'a ';

07 short a =-2;

08 int B = 3;

09 float f = 5.3f;

10 double d = 6.28;

11

12 System. out. print ("(ch/a)-(d/f)-(a + B) = ");

13 System. out. println (ch/a)-(d/f)-(a + B ));

14}

15}

Output result:

(Ch/a)-(d/f)-(a + B) =-50.18490561773532.

Don't rush to view the results. You can think about this complex expression (ch/a)-(d/f)-(B + a) before running the program) what is the final output type? How does one convert different data types to the same one? The following is an analysis of the execution structure of the program:

 


Java Operators

Java operators can be classified into four types: Arithmetic Operators, Relational operators, logical operators, and bitwise operators.

1. Arithmetic Operators
Java Arithmetic Operators include unary operators and binary operators. The unary operator has only one operand. The binary operator has two operands, which are located between two operands. The operands of arithmetic operators must be numerical values.

(1) unary operator:
Unary operators include: positive (+), negative (-), plus 1 (++), and minus 1.
The addition and subtraction operators can only be used for numeric variables, but cannot be used in expressions. 1. The subtraction operator can be placed before a variable (such as ++ I) or after a variable (such as I ++). The difference between the two is: if it is placed before the variable (for example, ++ I), the variable value is first added with 1 or minus 1, and then other corresponding operations (mainly value assignment operations ); if it is placed after the variable (such as I ++), perform other operations first, and then add or subtract 1 to the variable value.
For example:
Int I = 6, j, k, m, n;
J = + I; // obtain the original value, that is, j = 6
K =-I; // negative value, that is, k =-6
M = I ++; // first m = I, then I = I + 1, that is, m = 6, I = 7
M = ++ I; // first I = I + 1, then m = I, that is, I = 7, m = 7
N = j --; // first n = j, then j = J-1, that is, n = 6, j = 5
N = -- j; // first j = J-1, then n = j, that is, j = 5, n = 5
Note that space is not allowed between the unary operator and the operations before and after it; otherwise, an error occurs during compilation.

(2) binary Operators
Binary operators include addition (+), subtraction (-), multiplication (*), Division (/), and remainder (% ). Here, the plus, minus, multiplication, and Division operations are completed. % is the remainder of the two operands after division.

Examples of % remainder operations:
A % B = a-(a/B) * B
The remainder operator can be used when both operands are integers or when both operands are floating-point (or one operand is floating-point. When both operands are floating-point numbers, for example, 7.6% 2.9, the calculation result is: 7.6-2*2.9 = 1.8.
When both operands are of the int type, the formula for calculating a % B is:
A % B = a-(int) (a/B) * B
When both operands are of the long type (or other integer type), the calculation formula of a % B is similar.

When the data types of the two operands involved in the binary operation are different, the Data Type of the result is the same as that of the Data Type with higher precision (or longer digits.

For example:
7/3 // division, the calculation result is 2
7.0/3 // division, the calculation result is 2.33333, that is, the result is consistent with the type with high accuracy.
7% 3 // obtain the remainder and the calculation result is 1
7.0% 3 // obtain the remainder and the calculation result is 1.0
-7% 3 // obtain the remainder and the operation result is-1. That is, the symbol of the operation result is the same as that of the left operand.
7%-3 // obtain the remainder and the operation result is 1, that is, the symbol of the operation result is the same as that of the left operand.

2. Relational operators
Relational operators are used to compare the values between two values. The calculation result is a logical value. There are six Relational operators: equal to (=) and not equal (! =), Greater than (>), greater than or equal to (> =), less than (<), less than or equal to (<= ).
... The remaining full text>

Type conversion in java

B is because String is the reference type, and int Is the value type, so comparison cannot be performed.
C I + s uses an integer to add A String. This is not allowed. A is correct because the String is prior to the plus sign and the + operator rewritten in the String class is called, is equivalent to concatenating a new string with s and I
A parameter is missing after D +. If ++ is used here, it is correct.
E there is no problem with the three orders operator. If the string is null or not, if it is null, I = 0 is not null, then I is equal to the length of the string.

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.