浅浅和平等有什么区别?这是如何应用于缓存的? [英] What is the difference between being shallowly and deeply equal? How is this applied to caching?

查看:239
本文介绍了浅浅和平等有什么区别?这是如何应用于缓存的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的笔记中找到以下内容,但我无法理解:

Found the following in my notes, but I am unable to make sense of it:


原始类型包装类实现缓存有限数量
的值。

这保证了有限数量的深度相等的包装器对象
也非常相等:
如果 o1.equals (o2)然后 o1 == o2

例如, new Integer(0) == new Integer(0)

一般来说这并不总是有用。

例如,new Integer(666)== new Integer( 666)

可能不成立。

缓存的原因是它节省了内存。

一般缓存适用于小原始值。

Primitive type wrapper classes implement caching for a limited number of values.
This guarantees that a limited number of deeply equal wrapper objects are also shallowly equal: If o1.equals( o2 ) then o1 == o2.
For example, new Integer( 0 ) == new Integer( 0 ).
In general this does not always work.
For example, new Integer( 666 ) == new Integer( 666 )
may not hold.
The reason for caching is that it saves memory.
In general caching works for "small" primitive values.

我不明白这是什么意思,或深(.equals())和浅(=)之间的区别(= =)等于。我知道在实践中,.equals必须用于对象和==用于积分值,但实际的推理可以包括我。

I don't understand what is meant by this, or what the difference is between a deep (.equals()) and shallow(==) equals. I know in practice, .equals must be used to objects and == for Integral values, but the actual reasoning for this alludes me.

我假设浅的名字也许只是检查两个值是否具有相同的类型和名称,深层检查两个变量是否指向同一个对象?我不知道缓存会如何在这里发挥作用,或者为什么它会有用。

I assume by the names that shallow maybe just checks that both the values are of the same type and name, which deep checks that both variables point to the same object? I don't see how the caching would come into play here though, or why it would be useful.

推荐答案

嗯,实际上浅/深解剖不同于== /平等解剖:

Well, actually shallow/deep dissection is different from ==/equal dissection:


  1. ==比较对象身份,即检查是否
    操作数实际上是相同的
    (对同一内存区域的两个引用),
    等于比较对象
    等价,即两个可能不完全相同的
    对象的逻辑值
    是相同的。如果是两个
    对象

  1. == compares for object identity, that is you checking whether operands are the same in fact (two references to the same area of memory), whereas equals compares for object equivalence, that is "logical" value of two, possibly not identical objects, is the same. If for two objects

a == b

那么确实

a.equals(b) // if a != null

,但在所有
案例中相反的情况并非如此。

, but opposite isn't true in all cases.

所有这一切都是你必须的永远不要使用 == 来比较两个复合对象,除非你认为它们是相同的,只有它们是相同的。

Morale of all of this is that you must never use == to compare two compound objects, unless you consider them equal only if they are the same.

这篇关于浅浅和平等有什么区别?这是如何应用于缓存的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆