The 3rd chapter of the Java Development Handbook learning process operator

Source: Internet
Author: User

Operator overloading means that the same operator performs different operations under different circumstances.

For example, the "+" operator has different capabilities in Java:

    • Addition operation
    • Numeric Plus
    • String connection

It is particularly important to note the connection problem of strings to variables of other base data types in the string-concatenated expression.

For example:

(1) System.out.println (A + M + N), A is a string object, and M and n are of type int (or other basic data types such as Byte,short,float). According to the precedence of the operators in this expression, a is connected with M first, merged into a string, and then concatenated with N and output as a string. Instead of M and N are added and then concatenated with string A.

(2) System.out.println (m + n + a), M and N are added first and then connected to string A. This is contrary to the situation above.

There are several things to note in the expressions that have the take-up operator participation:

    • The "15%-4" Operation results in "3", and the resulting symbol is independent of the right operand
    • The " -15%4" Operation results in "3", and the result symbol is related to the left operand
    • "6.8%6.3" operation result is "0.5", floating-point number can also be redundant
    • The "15.0%0" Operation results in "NaN", indicating that you do not know what the result is
    • "15%0" Run error, same as divide by 0

The self-increment, decrement operator does not type-promote it.

"Nan" means "do not know", so "Nan" can not be compared with "= =" and "! =" to compare with each other, only return "false".

It makes sense to make a value with Infinity , while comparing to "Nan" makes no sense, and Java specifies that any numeric value compared to "Nan" will always return "false".

The result expressions 1 and 2 in the ternary operator are required to be compatible if they are basic types. If it is a reference type, it is always possible, because the return type of the ternary expression is set to the smallest common parent class type of two expression types.

In the shift operation , some situations do not conform to our normal thinking. For example, the following:

 Public class sample3_14{    publicstaticvoid  main (string[] args)    {         int i = <<n;         long L = >>;        System.out.println ("i =" + i);        System.out.println ("L =" + l);}    }

According to our normal understanding of the shift operation, the 32-bit integer value 88 is shifted left 32 bits, the entire bit pattern is displayed as 0, so the final result is 0, but in the final result I will find that the value of I does not change. This is because before the shift, the Java system first takes the number of bits shifted and the number of bits to be shifted, and then shifts based on the remainder.

In the assignment operation , pay attention to a small detail. The assignment operator "=" is ordered from right to left, that is, the expression to the right of the "=" sign is finished, and then assigns its value to the variable to the right of "=". So "int k + = 1" cannot be compiled. Because "int k + = 1" is actually equivalent to "int k = k + 1", and this formula first calculates "K + 1", but K is not defined, so "int k + = 1" is obviously wrong. It is a good practice to initialize "K" to a value before "+ =" operation.

In addition, the assignment operators of type "+ =" also have "-=", ">>>=" and so on. The entire "X +=y" is equivalent to "x= (<x type >) (x+y)", which has an implicit coercion type conversion.

The 3rd chapter of the Java Development Handbook learning process 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.