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

查看:126
本文介绍了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,因为Integer是不可变的.如果不是真的,我将不得不研究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.

这是一个稍微复杂的例子.假设我有一些类似于Integer但可变的对象.我们称之为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指向同一对象.所有的加法和减法实际上都作用于相同的值,而不是两个不同的值,因此,当我们进行测试时,它将始终相等.如果对象是可变的,则上面的代码将永远无法工作.如果它们是不可变的,则加法和减法将必须返回结果对象,而不是就地更新,这将起作用.

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天全站免登陆