How == compares memory location?

I was told never to use == for strings, but for everything else, because .equals will compare values, not object instances. (Which I understand the difference).

According to some sites, == compares memory locations?

What I do not understand if you are comparing an integer with another, why is it comparing memory cells or is it just for strings?

If you compare int 3 with int 4, it is obvious that it will not be in the same memory location, but if you compare int 4 with int 4, does this mean that all integers with the value 4 are stored in the same memory location?

+4
source share
9 answers

== compares oparands values, whether it is primitive or reference.

  • If the operands are primitive, the values ​​of the operands will be compared.

  • The operands that are references contain values, that is, the address for accessing the object to which they refer. A string is not a primitive data type; it is treated as objects in java. When you compare two type string references, the result will be true only when the values ​​of the operands, that is, the address of the String objects, are equal (which means that they refer to the same String object).

+4
source

According to some sites, == compares memory locations?

The expression a == b compares the contents of a and b , regardless of their types.

I do not understand that if you are comparing an integer with another, why would it compare in memory locations or is it just for strings?

In the case where a and b are references, the == operator will compare the "memory cells", since this is what the variables contain.

If a and b have primitive types such as int or double , the variables will contain the actual values, so these values ​​(and not their locations) will be compared.

(Note that a variable can never contain an object, such as String , it can be no more than the point of the object).

Does this mean that all integers with a value of 4 are stored in the same memory location?

No. As explained above, int compared "directly". When it comes to Integer , the story is a little different. First of all, new ensures that you get a new link, i.e.

 Object i = new Integer(5); Object j = new Integer(5); ... i == j ... 

will always give false.

If you continue auto-boxing:

 Object i = (Integer) 5; Object j = (Integer) 5; ... i == j ... 

you will get the truth because auto-box goes through the cache for values ​​in the range -128-127. (See, for example, this question: Compare two Integer: why is this true? )

+11
source

Simply put: the fact is that int is a primitive type, while String is an object. The values ​​of primitive types can be compared with == , since the variable points to the value itself, and not to a reference to the value.

+2
source

int are primitive types in java, and therefore they are not a link object, but a value directly.

+1
source

== compares link types. int is a primitive type.

So:

 int x = 3; int y = 3; x==y is true 

but using the reference type Integer

 Integer x = new Integer(3); Integer y = new Integer(3); x == y is false 
+1
source

The == operator compares object references in memory, and strings compare objects. Primitives are not objects, if they are of the same type, then == will work. As you say, if they are object variants of primitives (for example, Integer for int), then java (> 5) autoboxes for comparison.

+1
source

The == operator compares int by value and objects by address. Thus, == is correct for int , but (usually) not for String s.

Note that if you know that both String methods were returned by the String.intern method, == works correctly, since intern guaranteed to return the same address for identical strings.

0
source

From the Java specification 15.21 Equality operators :

15.21 Equality Operators

Equality operators are syntactically left-associative (they are grouped from left to right), but this fact is essentially never useful; for example, a == b == c analyzes how (a == b) == c. The result type a == b is always boolean, so c must be of type boolean or a compilation error. So a == b == c does not check, a, b and c are equal.

Equality Expression: Relationship Expression EqualityExpression == RelationalExpression EqualityExpression! = RelationalExpression The == (equal) and a = = (not equal) operators are similar to relational operators, except for their lower priority. In this way,

In all cases, a! = B gives the same result as (a == b). Equality operators are commutative if the operand expressions have no side effects.

15.21.1 The operators of numerical equality == and! =

If the operands of the equality operator are both numeric, and one is of the numeric type and the other is convertible (Section 5.1.8) to the numeric type, binary numeric promotion is performed along the operands (Β§5.6.2). If the advanced type of operands is int or long, then an integer equality test is performed; if the advanced type is floating or double, then a floating point equality test is performed. Note that binary numeric promotion performs a conversion on a set of values ​​(Β§5.1.13) and unpacks the conversion (Β§5.1.8). Comparison is performed exactly on floating point values, no matter what value sets their representation, the values ​​were taken from.

Floating-point equality testing is performed in accordance with the IEEE 754 standard:

If either the operand is NaN, then the result == is false, but the result! = Is true. Indeed, the test x! = X is true if and only if the value of x is NaN. (The Float.isNaN and Double.isNaN methods can also be used to check if the value is NaN.) Positive zero and negative zero are considered equal. Therefore, -0.0 == 0.0 is true, for example. Otherwise, two different floating point values ​​are considered unequal equality operators. In particular, there is one value representing positive infinity, and one value representing negative infinity; each compares the equal only with itself, and each compares the unequal to all other meanings. Given these considerations of floating point numbers, then for integer operands or operands of a floating point other than NaN: the value created by the == operator is true if the value of the left operand is equal to the value of the right operand; otherwise, the result will be false. The value created by the statement! =, True, if the value of the left operand is not equal to the value of the right operand; otherwise the result will be false. 15.21.2 Logical equality operators == and! =

If the operands of the equality operator are both Boolean, and if one operand is of type boolean and the other is of type Boolean, then the operation is a logical equality. Boolean equality operators are associative. If one of the operands is of type Boolean, it is subjected to unpacking (section 5.1.8).

Result == - true if the operands (after any necessary unpacking conversion) are true or both false; otherwise the result is false.

The result if = is false if both operands are true or both are false; otherwise the result will be true. Thus,! = Behaves in the same way as ^ (Β§15.22.2) with respect to Boolean operands.

15.21.3 equality of references Operators == and! =

If the operands of the equality operator are both references, and the type or null type, then the operation is the equality of the object. a compilation error occurs if it is not possible to convert the type or operand to the type of another by converting customization (Β§5.5). The run-time values ​​of the two operands would necessarily be unequal.

At run time, the result == is true if the operand values ​​are both null or both refer to the same object or array; otherwise the result is false.

The result if = = false if the operand values ​​are zero or both refer to the same object or array; otherwise the result is correct.

While == can be used to compare references of type String, such an equality test determines whether two operands refer to the same String object. The result is false if the operands are different String objects, even if they contain the same sequence of characters. The contents of the two lines s and t can be checked for equality by calling the s.equals (t) method. See also Β§3.10.5.

0
source

Objects represent equality in value, but the value that objects have is a reference to a memory cell. Primitives (i.e. Int, boolean, char, double) do not use references, but retain their value. Therefore, when == is used, it compares the value of the two. In the case of objects, this is a link; however, in the case of primitives, this is the value that it stores.

0
source

Source: https://habr.com/ru/post/1380770/


All Articles