==如何比较内存位置?

发布于 2024-12-15 06:48:23 字数 321 浏览 1 评论 0 原文

我被告知永远不要对字符串使用 == ,而是对其他所有内容使用 == ,因为 .equals 会比较值而不是对象的实例。 (我理解其中的区别)。

根据一些网站, == 比较内存位置?

我不明白的是,如果您将一个整数与另一个整数进行比较,为什么它会比较内存位置,或者这只是针对字符串?

如果您将 int 3 与 int 4 进行比较,显然它不会位于同一内存位置,但是如果您将 int 4 与 int 4 进行比较,这是否意味着所有值为 4 的整数都存储在同一个内存位置中内存位置?

I have been told to never use == for strings but for everything else because .equals would compare the values rather than the instances of the object. (Which I understand the difference of).

According to some sites, == compares memory locations?

What I don't understand is if you're comparing an integer with another, why would it compare memory locations, or is this just for strings?

If you're comparing int 3 to int 4 obviously it wouldn't be in the same memory location, but then if you're comparing int 4 to int 4, does that mean all integers with the value of 4 is stored in the same memory location?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

十年不长 2024-12-22 06:48:23

根据一些网站,==比较内存位置?

表达式a == b比较 ab 的内容,无论其类型如何。

我不明白的是,如果您将一个整数与另一个整数进行比较,为什么要比较内存位置,或者这只是针对字符串?

如果 ab 是引用,== 运算符将比较“内存位置”,因为这就是变量所包含的内容。

如果 ab 是原始类型,例如 intdouble,则变量将包含实际值,因此,将比较这些值(而不是它们的位置)。

(请注意,变量永远不能包含一个对象,例如String,它最多可以指向一个对象。)

这是否意味着所有值为 4 的整数都存储在同一内存位置?

不。如上所述,int 会进行比较“直接地”。当谈到Integer时,情况略有不同。首先,new 保证您获得新的引用,即

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

... i == j ...

始终会产生 false。

但是,如果您进行自动装箱:

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

... i == j ...

您会得到 true,因为自动装箱会通过缓存获取 -128-127 范围内的值。 (例如,参见这个问题:比较两个整数:为什么 == true?

According to some sites, == compares memory locations?

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

What I don't understand is if you're comparing an integer with another, why would it compare memory locations, or is this just for strings?

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

In case a and b are of primitive types, such as int or double the variables will contain actual values, consequently these values (and not their locations) will be compared.

(Note that a variable can never contain an object such as a String, it can at most point at an object.)

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

No. As explained above, ints are compared "directly". When it comes to Integer the story is slightly different. First of all new guarantees that you get hold of a fresh reference, that is

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

... i == j ...

will always yield false.

If you go through auto-boxing however:

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

... i == j ...

you'll get true due to the fact that auto-boxing goes through a cache for values in the range -128-127. (See for instance this question: Compare two Integer: why is == true?)

相守太难 2024-12-22 06:48:23

== 比较操作数的值,无论它是原始类型还是引用类型。

  1. 如果操作数是原始操作数,则将比较操作数的值。

  2. 作为引用的操作数包含值,即访问它们所引用的对象的地址。字符串不是原始数据类型,它们在java中被视为对象,当您比较字符串类型的两个引用时,只有当操作数的值(即字符串对象的地址)相等时,结果才会为真(这意味着它们引用到同一个 String 对象)。

== compares the values of the oparands whether it is primitive or reference type.

  1. If the operands are primitive the values of the operands will be compared.

  2. Operands which are references contains values i.e. address to access the object they are referring to. String are not primitive data type, they are considered as objects in java, When you are comparing two references of type string, the result will be true only when the values of the operands i.e. address of the String objects are equal ( which means they refer to the same String object).

遥远的绿洲 2024-12-22 06:48:23

简而言之:int 是原始类型,而 String 是对象。基本类型的值可以与 == 进行比较,因为变量指向值本身而不是对值的引用。

Simply put: the thing is that int is a primitive type whereas String is an object. The values of primitive types can be compared with == since the variable points to the value itself rather than the reference to the value.

一枫情书 2024-12-22 06:48:23

int 是 java 中的原始类型,因此它们不代表对象“引用”,而是直接代表值。

int's are primitive types in java and as such they don't represent an object "reference" but the value directly.

地狱即天堂 2024-12-22 06:48:23

== 比较引用类型。 int 是原始类型。

so:

int x = 3;
int y = 3;

x==y is true

但使用 Integer 引用类型

Integer x = new Integer(3);
Integer y = new Integer(3);

x == y is false

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

so:

int x = 3;
int y = 3;

x==y is true

but using the Integer reference type

Integer x = new Integer(3);
Integer y = new Integer(3);

x == y is false
久隐师 2024-12-22 06:48:23

== 运算符比较内存中对象的引用,字符串是对象 - 基元不是对象,因此只要它们具有相同的类型,那么 == 就可以工作。正如您所说,如果它们是基元的对象变体(例如 int 的整数),则 java(> 5)自动框以便进行比较。

The == operator compares the references of objects in memory and Strings are objects - primitives aren't objects so as long as they are of the same type, then == will work. As you say, if they are the object variants of primitives (e.g. Integer for int) then java (>5) autoboxes in order to do the compare.

橙幽之幻 2024-12-22 06:48:23

来自 Java 规范 15.21 相等运算符

15.21 相等运算符

相等运算符在语法上是左结合的(它们将
从左到右),但这个事实基本上没有用;为了
例如,a==b==c 解析为 (a==b)==c。 a==b 的结果类型是
始终是布尔值,因此 c 必须是布尔类型或 a
发生编译时错误。因此,a==b==c 不会测试是否
a、b、c 都相等。

等式表达式:
关系表达式
等式表达式 == 关系表达式
EqualityExpression != RelationalExpression ==(等于)和 !=(不等于)运算符类似于关系表达式
运算符,除非它们的优先级较低。因此,一个

在所有情况下,a!=b 都会产生与 !(a==b) 相同的结果。平等
如果操作数表达式没有边,则运算符是可交换的
效果。

15.21.1 数值相等运算符 == 和 !=

如果相等运算符的操作数都是数字类型,或者
一个是数字类型,另一个可以转换(§5.1.8)为
数字类型,对操作数进行二进制数字提升
(第 5.6.2 节)。如果操作数的提升类型为 int 或 long,则
执行整数相等测试;如果提升的类型是 float 或
double,然后执行浮点相等测试。注意
二进制数字提升执行值集转换(第 5.1.13 节)并且
拆箱转换(第 5.1.8 节)。比较是准确进行的
浮点值,无论其代表的值是什么
值是从中提取的。

浮点相等测试是按照
IEEE 754 标准规则:

如果任一操作数为 NaN,则 == 的结果为 false,但
!= 的结果为 true。事实上,测试 x!=x 为真当且仅当
x 的值为 NaN。 (Float.isNaN 和 Double.isNaN 方法也可以
用于测试某个值是否为 NaN。)正零和负数
零被认为是相等的。因此,例如,-0.0==0.0 为真。
否则,两个不同的浮点值被视为不相等
由相等运算符。特别地,有一个值
代表正无穷大,一个值代表负无穷大
无穷大;每个比较只等于它自己,并且每个比较
不等于所有其他值。根据这些考虑因素
浮点数,则以下规则适用于整数
操作数或除 NaN 之外的浮点操作数:值
如果左侧的值是 == 运算符产生的结果,则为 true
操作数等于右侧操作数的值;否则,
结果是假的。 != 运算符产生的值为 true,如果
左侧操作数的值不等于
右手操作数;否则,结果为假。
15.21.2 布尔相等运算符==和!=

如果相等运算符的操作数都是布尔类型,或者
如果一个操作数是布尔类型,另一个操作数是布尔类型,
那么操作就是布尔相等。布尔相等运算符
是关联的。如果其中一个操作数是布尔类型,那么它是
进行拆箱转换(§5.1.8)。

如果操作数(在任何所需的拆箱之后),则 == 的结果为 true
转换)均为 true 或均为 false;否则,结果是
错误。

如果操作数均为 true 或均为 false,则 != 的结果为 false;
否则,结果为 true。因此 != 的行为与 ^ 相同
(§15.22.2) 当应用于布尔操作数时。

15.21.3 引用相等运算符 == 和 !=

如果相等运算符的操作数都是任一引用
type 或 null 类型,则操作为对象相等。一个
如果无法转换类型,则会出现编译时错误
通过强制转换将任一操作数转换为另一个操作数的类型
(第 5.5 节)。两个操作数的运行时值必然是
不平等。

在运行时,如果操作数值均为 true,则 == 的结果为 true
null 或两者都引用相同的对象或数组;否则,结果
是假的。

如果操作数值均为 null 或均为 null,则 != 的结果为 false
引用同一个对象或数组;否则,结果为 true。

虽然 == 可用于比较 String 类型的引用,例如
相等测试确定两个操作数是否引用
相同的 String 对象。如果操作数不同,结果为 false
字符串对象,即使它们包含相同的字符序列。
两个字符串 s 和 t 的内容可以通过以下方式测试是否相等
方法调用 s.equals(t)。另请参阅第 3.10.5 节。

From the Java Specification 15.21 Equality Operators:

15.21 Equality Operators

The equality operators are syntactically left-associative (they group
left-to-right), but this fact is essentially never useful; for
example, a==b==c parses as (a==b)==c. The result type of a==b is
always boolean, and c must therefore be of type boolean or a
compile-time error occurs. Thus, a==b==c does not test to see whether
a, b, and c are all equal.

EqualityExpression:
RelationalExpression
EqualityExpression == RelationalExpression
EqualityExpression != RelationalExpression The == (equal to) and the!= (not equal to) operators are analogous to the relational
operators except for their lower precedence. Thus, a

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

15.21.1 Numerical Equality Operators == and !=

If the operands of an equality operator are both of numeric type, or
one is of numeric type and the other is convertible (§5.1.8) to
numeric type, binary numeric promotion is performed on the operands
(§5.6.2). If the promoted type of the operands is int or long, then an
integer equality test is performed; if the promoted type is float or
double, then a floating-point equality test is performed. Note that
binary numeric promotion performs value set conversion (§5.1.13) and
unboxing conversion (§5.1.8). Comparison is carried out accurately on
floating-point values, no matter what value sets their representing
values were drawn from.

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

If either operand is NaN, then the result of == is false but the
result of != is true. Indeed, the test x!=x is true if and only if the
value of x is NaN. (The methods Float.isNaN and Double.isNaN may also
be used to test whether a value is NaN.) Positive zero and negative
zero are considered equal. Therefore, -0.0==0.0 is true, for example.
Otherwise, two distinct floating-point values are considered unequal
by the equality operators. In particular, there is one value
representing positive infinity and one value representing negative
infinity; each compares equal only to itself, and each compares
unequal to all other values. Subject to these considerations for
floating-point numbers, the following rules then hold for integer
operands or for floating-point operands other than NaN: The value
produced by the == operator is true if the value of the left-hand
operand is equal to the value of the right-hand operand; otherwise,
the result is false. The value produced by the != operator is true if
the value of the left-hand operand is not equal to the value of the
right-hand operand; otherwise, the result is false.
15.21.2 Boolean Equality Operators == and !=

If the operands of an equality operator are both of type boolean, or
if one operand is of type boolean and the other is of type Boolean,
then the operation is boolean equality. The boolean equality operators
are associative. If one of the operands is of type Boolean it is
subjected to unboxing conversion (§5.1.8).

The result of == is true if the operands (after any required unboxing
conversion) are both true or both false; otherwise, the result is
false.

The result of != is false if the operands are both true or both false;
otherwise, the result is true. Thus != behaves the same as ^
(§15.22.2) when applied to boolean operands.

15.21.3 Reference Equality Operators == and !=

If the operands of an equality operator are both of either reference
type or the null type, then the operation is object equality. A
compile-time error occurs if it is impossible to convert the type of
either operand to the type of the other by a casting conversion
(§5.5). The run-time values of the two operands would necessarily be
unequal.

At run time, the result of == 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 of != is false if the operand values are both null or both
refer to the same object or array; otherwise, the result is true.

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

温馨耳语 2024-12-22 06:48:23

== 运算符按值比较 int 并按地址比较对象。因此,== 对于 int 是正确的,但(通常)对于 String 则不正确。

请注意,如果您知道 String.intern 方法已返回两个 String,则 == 可以正常工作,因为 intern 保证为相同的字符串返回相同的地址。

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

Note that if you know that both Strings have been returned by the String.intern method, == works correctly, as intern is guaranteed to return the same address for identical strings.

不再见 2024-12-22 06:48:23

对象在值上是相等的,但对象具有的值是对内存位置的引用。基元(即 int、boolean、char、double)不使用引用,但存储它们的值。所以当使用==时,它会比较两者的值。对于对象来说,它是一个引用;然而,对于基元来说,它是它存储的值。

Objects are equality by value, but the value that objects have is a reference to the memory location. Primitives (i.e. int, boolean, char, double) do not use references, but store their value. So when == is used, it compares the value of the two. In the case of objects it's a reference; however, in the case of primitives it is the value it stores.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文