Java Road (ii) operator

Source: Internet
Author: User
Tags arithmetic arithmetic operators bitwise bitwise operators

Operators are simple, here are just a few things to note and points to keep in mind.

operator takes one or more parameters and generates a new value.

In Java, almost all operators can only manipulate basic types.

Exceptions are = = = and! =, which can manipulate all objects. In addition, the string class supports + and + =.

1. Assignment =

The base type stores the actual numeric value, so assigning a value to a base type variable is the address that assigns the contents of a place to the variable.

When assigning a value to an object, the actual operation is a reference. As shown, we assume that T1 and T2 are references to two objects of the same class, pointing to their respective objects respectively. Beginning as shown in a, when the assignment t1=t2 is executed, as shown in B, it can be seen that the references T1 and T2 point to the object that the T2 points to, and the object that T1 points to is no longer accessible, which is known as the aliasing phenomenon.

How to avoid the phenomenon of aliasing?

You can use this method to assign a value, T1.length=t2.length,t1.width=t2.width, to ensure that two objects are independent of each other. But this violates the object-oriented design principle.

Similarly, when an object is passed to a method, it is actually passed as a reference, which can also cause aliasing problems.

2. Arithmetic operators

Binary operator: +-*/% + = = *=/=

Unary operator: +-

A unary minus sign used to transform the data. The unary Plus is only intended to correspond to a unary minus, and its only use is to promote the smaller type to int.

3. Self-increment and self-reduction

There are two types of prefix and suffix, which are the only operators that have side effects except those involving assignment.

4. Relational operators

The relational operator generates a Boolean result that calculates the relationship between the values in the operand.

Relational operators:< > <= >= = = =!

= = and! = Apply to all basic data types, and other comparison operators do not apply to Boolean types.

Comparisons between basic types use = = and! =.

When comparing the actual contents of two objects, you must use the Equals () method (not applicable to the base type) that applies to all objects, and the reference to the object is compared with = = and! =.

Note: the Equals () default behavior is a comparison reference, and the actual content of the object can be compared because it is overwritten in the relevant class library, so when we create our own class, we must override the Equals () method in the class.

5. Logical operators

Logical operator:&& | | !

Generates a Boolean value based on the logical relationship of the parameter.

Can only be applied to Boolean values.

You cannot use a non-Boolean value as a Boolean value in a logical expression.

Has short-circuit properties.

6. Direct constants

Sometimes the compiler does not know what type to create and can use characters related to direct constants to guide the compiler.

such as 0xff,0177,100f,100l,10d and so on.

How to represent the exponent: 1.39e-43f.

7. Bitwise operators

Bitwise operator:& | ~ &= |= ^=

Operation Bit.

8. Shift operator

Shift Operators:<< >> <<= =>>

Note: Before you shift the values of the char,byte and short types, they are converted to the int type, and the resulting result is also of type int. Only the low 5 bits of the value at the right end are useful (ranges of int values). Similarly, a long type of numeric value is performed after the shift operation, resulting in a lower 6 bit of the result at the right end useful.

9. Ternary operator

Ternary operator:? :

10. String operators

String Operator: + + =

Used to concatenate different strings.

If the expression begins with a string, all subsequent operands must be of type string,

Tip: Sometimes an empty string + primitive type variable is used to perform a conversion to a string of the base type without having to call integer.tostring () in the display.

11. Type Conversion

Truncation: When converting a floating-point value to an integer value, the fractional part is rounded off.

Rounding: You want to use the round () method in Java.lang.Math () to round up a floating-point value when it is converted to an integer value.

The round method is part of the Java.lang and does not require additional import.

Elevation: When arithmetic arithmetic or bitwise operations are performed on basic data, the type is automatically converted to int before the operation, and the result is an int type, as long as it is smaller than int (char,byte,short). In general, the largest data type that appears in an expression determines the data type of the final result of the expression. If you want to assign the result to a smaller data type, you need to use a type conversion, which can cause information loss.

12.Java no sizeof

The biggest reason for using sizeof () in C and C + + is to migrate. Because different data types may have different sizes on different machines, it is important to know how large the specific type is when doing some storage-related operations, and sizeof () can implement it.

The size of the data type in Java is fixed, the size is the same in all machines, so no sizeof () is required, and it has been designed in the language.

Java Road (ii) operator

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.