Java basic syntax: Operator and type conversion

Source: Internet
Author: User
Basic Java Syntax: Operator and type conversion-general Linux technology-Linux programming and kernel information. For more information, see the following. 1. operators:
1) in the Java language, common operators include: +,-, *,/. You must be familiar with them.
2) power: Java does not have a power operator and must use the Math. pow (x, a) method to represent the power a of x. Both parameters of the pow method belong to the double type, and the returned value is also of the double type.
For example:
Double a = Math. pow (); // defines a variable a, whose value is the power of 10.
3) Increment and decrease operators:
A ++ and a use the variable value first, and then increase or decrease progressively.
+ A and a increase or decrease first and then use the value of the variable.
4) relational and boolean operators:
Relational operators: = ,! =, <,>, <=,> =
Boolean operator: & (and), | (OR ),! (No)
5) bitwise OPERATOR:
& (Plus), | (OR), ^ (same or ),~ (NO),> shift to the right, <(shift to the left), >>> (fill the position on the top with zero)
Note that Java does not < <运算符。
6) brackets and operator classification:
In Java, the default calculation sequence of operators is:
[], () (Method call) from left to right
! ~ ++ (Mona1)-(mona1) (Styling) new from right to left
+/% Left to right
+-Left to right
<>>>> Left to right
<<=>=> Instanceof from left to right
=! = Left to right
& Left to right
^ Left to right
| Left to right
& Left to right
| Left to right
? : Left to right
= + =-= * =/= & =|=^= <<=>=.> = From right to left
In Java, brackets can be used to specify the desired computing sequence, thus changing the default Operation Sequence of operators.
2. Mutual conversion of value types:
When data of different data types is involved in calculation, the conversion of different data types is involved:
The conversion of data types in Java follows the following rules:
If one of the calculation objects is of the double type, the other is also converted to the double type.
Otherwise, if one of the calculation objects is of the float type, the other will be converted to the float type.
Otherwise, if one of the calculation objects is of the long type, the other will be converted to the long type.
Otherwise, if one of the calculation objects is of the int type, the other will be converted to the int type.
Otherwise, if one of the calculation objects is of the short type, the other will be converted to the short type.
Type conversion can also be achieved through "Styling". The syntax is very simple. Place the target type in brackets, and then keep up with a variable name.
For example:
Double x = 9.99;
Int nx = (int) x; // in this case, nx is an int type data, and its value is 9. The subsequent parts are lost in the modeling process. You can use the Math. round method to insert a floating point into the nearest integer. After you use the Math. round method, you may also need to use a shape for type conversion.
Java also allows you to assign one type of variable value to another without displaying the shape. You can also perform some specific value assignment conversions, including:
Buty-> short-> int _> long-> float-> double char-> int
When values are assigned, they are converted from left to right in the above Order.
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.