JAVA basic operator (pick)

Source: Internet
Author: User
Tags bitwise

(From: Java Classic Getting Started tutorial)

Http://wenku.baidu.com/link?url= Iowi58cd5vzehn-nl4pn7gren-rfzydrhjdletabyc9l-9aninylwuwchpwiyc3hqhzpws5fv2fuh-k1zp4ixh6afvkpibvtdflpbvwp1tc

Six: operator

1: Arithmetic operations
Arithmetic operation refers to: +,-, *,/and other basic operations
It is important to note that:
% is to find the MoD operation;
Be careful about the division of integers:
5/2 = 2//Not 2.5


2: Comparison operation
Comparison operations refer to:>, <, >=, <=, = =,! =, and similar operations
It is important to note that:
Characters can be compared in size, (with their ASCII code, to integers)
Equality comparison of small impatient points
Instanceof is also a comparison operation that determines whether an object belongs to a class. (Introduction later)
In the = = operation, for the base type is the "content" of the comparison, and for the reference type, the address is compared. Careful


3: Logical operation
operator && (defined as "and") and | | (defined as "or") to execute a boolean logical expression. Take a look at the following example:
MyDate d = null;
if ((d! = null) && (D.day () > 31)) {
What to do with D
PDF file created with "Pdffactory Pro" trial version www.fineprint.cn
JAVA Private school with Me study Series--java article website: http://www.javass.cn Tel: 010-86835215

The Boolean expression that forms an if () statement argument is legal and safe. This is because when the first sub-expression is a false
, the second subexpression is skipped, and when the first subexpression is false, the entire expression is always false, so no
The value of the second sub-expression must be considered. Similarly, if the use of | | operator, and the first expression returns True, the
The second expression does not have to be evaluated because the entire expression is already considered true.
4:++ 、---Operations
The + + operation is equivalent to: the variable of the operation plus 1, such as: X + + equivalent to x=x+1;
--the exact opposite of the operation, minus 1 of the variable equivalent to the operation
Note: X + + and ++x are not the same thing. X + + is used first, then added; ++x is added first and then used.
5:= Assignment operation
X=5 equivalent to assigning a value of 5 to the variable x


6: Bitwise arithmetic
Bitwise logical operators (Bitwise Logical Operations).
Arithmetic logic operator & (with), | (or), ~ (complement complement), ^ (XOR);
Bit Operation example


7: Shift Operation
The Java programming language provides two right shift operators and one left-shift operator, one (>>) to the right, and 2 for
divided by 2 for the shift left (<<).
(1): operator >> perform arithmetic or symbolic right shift. The result of the shift is that the first operand is removed by a power of 2, and the value of the
exponent is given by the second number. For example:
>> 1 gives 128/21 = 4
>> 4 gives 256/2

=
-256 >> 4 GIVES-256/2
4
= -16
(2): logical or non-symbolic right shift operator >>> primarily acts on a bitmap rather than the arithmetic meaning of a value; it
always places 0 on the sign bit. For example:
1010 ... >> 2 gives 111010 ...
1010 >>> 2 gives 001010 ...
During the shift, the >> operator causes the symbol bits to be copied.
PDF file created with "Pdffactory Pro" trial version www.fineprint.cn
JAVA Private school with Me study Series--java article website: http://www.javass.cn Tel: 010-86835215
(3): operator << performs a left shift. The result of the shift is that the first operand is multiplied by the power of 2, and the value of the exponent
is given by the second number. For example:
<< 1 gives 128*21 =
<<2 gives 16*22 =64
(4): negative equals positive minus plus one.
Example:


Attention:
(1): The shift operator simplifies the operand modulo 32 to the right of them to the left operand of type int, and modulo 64 simplifies to long
The right-hand operand of the type. Thus, any int x, x >>> 32 will cause constant X-values, not what you might expect
Zero.
(2): The important point to be commended is that the >>> operator is allowed only for integer types, and only for int and long
Value is valid. If used on a short or byte value, the value will go through the signed up type before applying >>>
To be promoted to an int. For this reason, unsigned shifts are often symbolic shifts.


8: String link with plus sign (+)
The operator + is able to link a string object and generate a new string:
String salutation = "Dr.";
String name = "Jack" + "Arthur";
String title = salutation + name;
The result of the last line is:
Dr. Jack Arthur.
If there is one argument in the + operator that is a string object, the other arguments are converted to string. All
objects can be automatically converted to string, although the result may be ambiguous. The object that is not a string is a pass
Converted to the equivalent of a string using the ToString () member method.


9: Three mesh operation? :
Trinocular operator?: The expression is: to determine whether the conditional expression before the question mark is true, and if true, before returning a colon
Value of the polygon, otherwise the value after the colon is returned.
For example:
PDF file created with "Pdffactory Pro" trial version www.fineprint.cn
JAVA Private school with Me study Series--java article website: http://www.javass.cn Tel: 010-86835215
public class Test {
public static void Main (string[] args) {
int i = (5>3)? 6:7;
System.out.println ("The i=" +i);
}
}
Operation Result: The I=6
In fact, the basic function of the trinocular operator is equivalent to If-else (learned immediately), using the three-mesh operator because
Its expression is more concise than the if-else of the same function. The above example is changed to be expressed in If-else as follows:
public class Test {
public static void Main (string[] args) {
int i = 0;
if (5 > 3) {
i = 6;
} else {
i = 7;
}
System.out.println ("The i=" + i);
}
}
Operation Result: The I=6

JAVA basic operator (pick)

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.