我被告知永远不要对字符串使用 ==
,而是对其他所有内容使用 ==
,因为 .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?
发布评论
评论(9)
表达式
a == b
比较a
和b
的内容,无论其类型如何。如果
a
和b
是引用,==
运算符将比较“内存位置”,因为这就是变量所包含的内容。如果
a
和b
是原始类型,例如int
或double
,则变量将包含实际值,因此,将比较这些值(而不是它们的位置)。(请注意,变量永远不能包含一个对象,例如
String
,它最多可以指向一个对象。)不。如上所述,
int
会进行比较“直接地”。当谈到Integer
时,情况略有不同。首先,new
保证您获得新的引用,即始终会产生 false。
但是,如果您进行自动装箱:
您会得到 true,因为自动装箱会通过缓存获取 -128-127 范围内的值。 (例如,参见这个问题:比较两个整数:为什么 == true?)
The expression
a == b
compares the content ofa
andb
, regardless of their types.In case
a
andb
are references, the==
operator will compare "memory locations" since that is what the variables contain.In case
a
andb
are of primitive types, such asint
ordouble
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.)No. As explained above,
int
s are compared "directly". When it comes toInteger
the story is slightly different. First of allnew
guarantees that you get hold of a fresh reference, that iswill always yield false.
If you go through auto-boxing however:
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?)
== 比较操作数的值,无论它是原始类型还是引用类型。
如果操作数是原始操作数,则将比较操作数的值。
作为引用的操作数包含值,即访问它们所引用的对象的地址。字符串不是原始数据类型,它们在java中被视为对象,当您比较字符串类型的两个引用时,只有当操作数的值(即字符串对象的地址)相等时,结果才会为真(这意味着它们引用到同一个 String 对象)。
== compares the values of the oparands whether it is primitive or reference type.
If the operands are primitive the values of the operands will be compared.
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).
简而言之:
int
是原始类型,而String
是对象。基本类型的值可以与==
进行比较,因为变量指向值本身而不是对值的引用。Simply put: the thing is that
int
is a primitive type whereasString
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.int
是 java 中的原始类型,因此它们不代表对象“引用”,而是直接代表值。int
's are primitive types in java and as such they don't represent an object "reference" but the value directly.== 比较引用类型。 int 是原始类型。
so:
但使用 Integer 引用类型
== compares reference types. int is a primitive type.
so:
but using the Integer reference type
== 运算符比较内存中对象的引用,字符串是对象 - 基元不是对象,因此只要它们具有相同的类型,那么 == 就可以工作。正如您所说,如果它们是基元的对象变体(例如 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.
来自 Java 规范 15.21 相等运算符 :
From the Java Specification 15.21 Equality Operators:
==
运算符按值比较int
并按地址比较对象。因此,==
对于int
是正确的,但(通常)对于String
则不正确。请注意,如果您知道
String.intern
方法已返回两个String
,则==
可以正常工作,因为intern
保证为相同的字符串返回相同的地址。The
==
operator comparesint
s by value and objects by address. Thus,==
is correct forint
s but (usually) not forString
s.Note that if you know that both
String
s have been returned by theString.intern
method,==
works correctly, asintern
is guaranteed to return the same address for identical strings.对象在值上是相等的,但对象具有的值是对内存位置的引用。基元(即 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.