java:示例中不可变对象的优点 [英] java: advantages of of immutable objects in examples

查看:76
本文介绍了java:示例中不可变对象的优点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请举例说明我可以看到不可变对象的优点。我在互联网上发现的信息集中在线程中。我还不知道线程。如果示例使用简单的原则会很棒

give me please examples where I could see advantages of immutable objects. Info I found in internet are concentrated in threads. I don't know about threads yet. would be great if the examples would use simple principles

推荐答案

不可变性在多线程程序中很重要,因为那时你知道一个线程不会破坏另一个线程中使用的值。但它在单线程程序中也很有用。

Immutability is important in multi-threaded programs, because then you know that one thread won't corrupt a value used in another thread. But it's also useful in a single-threaded program.

这是一个简单的例子:

Integer i=Integer.valueOf(17);
foo(i);
bar(i);

您可能想知道,传递给bar()的值是多少?

You might well want to know, What value is passed to bar()?

假设foo()是一个复杂的大函数。在这个例子中,我知道一个绝对的事实,当foo完成时,我仍然等于17,因为整数是不可变的。如果不是这样,我将不得不研究foo来判断它是否可能被改变。

Suppose that foo() is a big, complex function. In this example, I know for an absolute fact that when foo completes, i is still equal to 17, because an Integer is immutable. Were that not true, I would have to study foo to tell if it might be changed or not.

这是一个稍微复杂的例子。假设我有一些类似于整数的对象,但是它是可变的。我们称之为MutableInteger。然后说我写这个:

Here's a slightly more complex example. Suppose I have some object that resembles an Integer, but is mutable. Let's call it MutableInteger. Then say I write this:

MutableInteger currentInventory=findQtyInInventory();
MutableInteger neededInventory=currentInventory; // copy current for starters
... bunch of other code ...
neededInventory.subtract(allocatedToSales);
currentInventory.add(arriving);
... bunch of more code ...
if (neededInvenory.compareTo(currentInventory)>0)
  display("Shortage!");

您是否看到上述问题? neededInventory和currentInventory指向同一个对象。所有的加法和减法都是在相同的值上运行,而不是两个不同的值,所以当我们进行测试时,它总是相等的。如果对象是可变的,上面的代码将永远不会工作。如果它们是不可变的,则add和subtracts必须返回一个结果对象而不是更新到位,这样就可以了。

Do you see the problem with the above? neededInventory and currentInventory point to the same object. All the adds and subtracts are really acting on the same value, not two different values, so when we get to the test, it will always be equal. The above code would never work if the objects are mutable. If they are immutable, the adds and subtracts would have to return a result object rather than updating in place, and that would work.

多年前我使用了Fortran编译器整数是可变的。我们有一个函数接受了几个参数,其中一个是整数。在极少数情况下,函数更新整数。然后有一天有人写了一个对这个函数的调用,将常量2作为整数传递。该函数决定更新参数,从而将常量2改为1!程序中使用常量2的每个其他位置现在神秘地得到值1。这需要很长时间来调试。

Years ago I used a Fortran compiler where integers WERE mutable. We had a function that accepted several parameters, one of them an integer. In some rare cases, the function updated the integer. Then one day someone wrote a call to this function passing the constant "2" as the integer. The function decided to update the parameter, thus changing the "constant" 2 to 1! Every other place in the program that used a constant 2 now mysteriously got the value 1 instead. This took a long time to debug.

这篇关于java:示例中不可变对象的优点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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