Basic two: operator

Source: Internet
Author: User
Tags arithmetic operators bitwise operators

Using Java Operators

The operator takes one or more arguments and generates a new value.

operator is used for operands to generate a new value. Other operators may change the value of the operand itself, which is called a "side effect."

The most common effect of these operators that change the operands is to produce side effects.

Almost all operators can only operate on "basic types". The exception operator is = = = and! =

These operators are capable of manipulating all objects. In addition, the string class supports + and + =.

Priority level

When there are multiple operators in an expression, the precedence of the operators determines the order in which each part is evaluated. The simplest rule is to first multiplication and then add and subtract. In addition, the role of parentheses should be clarified.

Assign value

The assignment uses the operator "=".
It means to take the right value and assign it to the left.

The right can be any number, variable, or expression (as long as you can generate a value), and the left must be a definite named variable. That is, you must have a physical space to store the value to the right of the equals sign.

Like what:

a = 4 ;

But you can't assign anything to a constant, and a constant cannot be an lvalue. For example (4 = A; Worong!)

Assignment for the base data type:

    基本类型存储了实际的数值,而并非指向一个对象的引用,所以在为其赋值的时候,是直接将一个地方的内容复制到了另一个地方。 另外,对基本数据类型使用 a = b ,那么b的内容就复制给a,若紧接着修改a,b的值不受这种修改的影响

However, when assigning a value to an object, the situation has changed:

        对一个对象进行操作时,我们真正操作的是对象的引用。 所以倘若“将一个对象赋值给另外一个对象”,实际上是将“引用”从一个地方复制到另外一个地方。  这意味着假若对对象使用c = d ,那么c和d都指向原来只有d指向的那个对象。

The tank class has two instances T1 and T2, assigning different values to the Level field, assigning T2 to T1, and then modifying T1.

Because the assignment operation is a reference to an object, all modifications to the T1 also change the T2. This is because T1 and T2 contain the same references, they point to the same object (the original T1 contains a reference to the object, which is a pointer to an object with a value of 9, when assigning a value to T1, the reference is overwritten, which is lost, and the object that is no longer referenced is automatically cleaned up by the garbage collector).

The phenomenon of this special Envoy, known as the "aliasing phenomenon", is a basic way for Java operators to avoid it. Can write like this

t1.level = t2.level;

This keeps two objects separate from each other, rather than binding T1 and T2 to the same object.

Arithmetic operators

+ - * /Subtraction and modulo operators % .

To X+4, and assign the result to x, you can writex +=4 ;

Through the random class of objects, the program can generate many different types of random numbers, only need to call

The parameter passed to Netxtint () sets the resulting random number to the line, and its lower bound is 0. But this lower limit is not what we want, we can do +1 of the results.

Auto Increment and decrement

++–

For example, if a is an int value, ++a can be equivalent to a = a +1;

The increment and decrement operators not only change the variable, but also use the value of the variable as the result of the build.

Each of these two operators is used in two ways, often referred to as prefix and suffix.

Prefix increment: Indicates that the + + operator precedes the variable or expression, whereas the suffix increment + + operator is behind the variable or expression.

Similarly, prefixes increment and decrement the same as:

* * For prefix form: The value is obtained after performing the operation.
For suffix form: Gets the value * * before the operation.

They are the only operators that have "side effects" in addition to those that design assignment operators. That is, they change the operands, not just their own values.

int i =1;System. out. println("I:"+ i);//1System. out. println("=======================");System. out. println("++i:"+ ++i);//2 perform operation I first add 1, at this time I is 2, and the value of the variable (that is, i=2) as the resultSystem. out. println("I:"+ i);//2System. out. println("i++:"+ i++);///2 Assign a value to the variable i (i=2) because it is the value of the variable as the result, so the value of i++ is 2, after performing the operation, the Operation I+1 becomes 3System. out. println("I:"+ i);//3System. out. println("=======================");System. out. println("I:"+-I.);//2 first operation, after Operation 3-1 I is 2, then assign value, at this time I is 2System. out. println("I:"+ i);//2System. out. println("i--:"+ i--);//2System. out. println("I:"+ i);//1

Relational operators

The relational operator generates a Boolean result.

True is true, whereas false.

Mainly include the following symbols:

<, >, <=, >=, = = and! =

The equivalence of test objects

Although the object's content is the same, the object references are really different, and = = and! = comparison is a reference to an object.

If you compare the actual contents of two objects, you need to use Equals ().

Let's say we created our own class, just like the following

What's the fuck? False again .....

This is because the default behavior of equals () is to compare references, so unless you override the Equals method in your new class, it is not possible to show the behavior we want.

Most Java libraries implement the Equals method so that they can be used to compare the contents of an object, rather than a reference to an object.

logical operators

With && or | | Non -! , returns a Boolean value.

Direct constants

The suffix character following the direct constant identifies its type.

If uppercase or lowercase l, represents a long
F, representing float
D, the delegates have changed.

Hex applies to all integer data types, prefixed by 0x or 0X, followed by 0~9 or uppercase (or lowercase) a~f.

The octal number is represented by the prefix 0 and the number of subsequent 0~7.

Bitwise operators

Used to manipulate binary

&= Bitwise AND Assignment
|= Bitwise OR Assignment
^= bitwise non-assigned value

Shift operator

Also used to manipulate binary

= Right Shift Assignment

= Unsigned Right Shift assignment
<<= Assignment Shift Left

Tri-Mesh Expression

Boolean-exp? Value0:value1

Boolean-exp is true, this calculates the value0, and the inverse calculates the value1.

Basic two: operator

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.