= And = in Java

Source: Internet
Author: User

Determining whether two variables are equal is a very important operation in programming. When processing the original value, this operation is quite simple, but involves

The task is a little complicated. Ecmascript provides two sets of operators to handle this problem. equal signs and non-equal signs are used to process original values,

The full and non-full signs are used to process objects.

1. equal signs and non-equal signs

In ecmascript, the equal sign is expressed by the equal sign (=). if and only when the two arithmetic numbers are equal, true is returned. Non-equal sign is a sense

Exclamation point and equal sign (! =), Returns true if and only if the number of two operations is not equal. To determine whether the two operations are equal

All operators perform type conversion.

The basic rules for executing type conversion are as follows:

Q if the number of operations is a Boolean value, convert it to a numeric value before checking equality. Convert false to 0 and true

1.

Q if one operation is a string and the other is a number, convert the string to a number before checking for equality.

Q if one operation is an object and the other is a string, before checking for equality, try to convert the object to a string (call

Tostring () method ).

Q if one operation is an object and the other is a number, convert the object to a number before checking for equality.

During comparison, this operator also complies with the following rules:

The value of Q is equal to that of undefined.

Q cannot convert null and undefined into other values when checking equality.

Q if the number of operations is Nan, the equal sign will return false, and the non-equal sign will return true. Important: Even if both operations are Nan

, The equal sign still returns false, because according to the rule, Nan is not equal to Nan.

Q if the number of two operations is an object, they are compared with their reference values. If the two operations point to the same object

Returns true. Otherwise, the number of two operations is different.

The following table lists some special cases and their results:

 

Table reaching
 
Value
 

Null = undefined true
 

"Nan" = Nan
 
False
 

5 = Nan
 
False
 

Nan = Nan
 
False
 

Nan! = Nan
 
True
 

False = 0
 
True
 

True = 1
 
True
 

True = 2
 
False
 

Undefined = 0
 
False
 

Null = 0
 
False
 

"5" = 5
 
True
 

2. Full and non-full numbers

Similar operators of equal signs and non-equal signs are full and non-full. The two operators are the same as equal signs and non-equal signs, but they

Type conversion is not performed before equality is checked. The full equal sign is represented by three equal signs (=). It is only required when no type conversion operation is required.

True is returned only when the values are equal. For example:

 

In this Code, the first warning compares the string "55" and number 55 with the equal sign, and outputs "true ". As mentioned above, this is because of Characters

The string "55" is converted to a number 55 before being compared with another number 55. The second warning indicates that the full equal sign is used in the case of no type conversion.

When comparing strings and numbers, of course, strings are not equal to numbers, and all outputs are "false ".

The non-equal sign is added by the exclamation point and two equal signs (! =) Indicates that true is returned only when the number of type conversion operations is not equal.

. For example:

 

Here, the first warning uses a non-equal sign to convert the string "55" to a number 55 so that it is equal to the second operation number 55. Therefore

The calculation result is false because the two operations are considered equal. The second warning uses a non-full equal sign. The operation is as follows:

"Is snum different from inum ?" The answer to this question is: "Yes (true)", because snum is a string and inum is a number,

Of course they are different.

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.