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.

Here's a simple example:

Integer i=Integer.valueOf(17);

foo(i);

bar(i);

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

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.

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!");

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.

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中对象有什么优点,Java:示例中不可变对象的优点相关推荐

  1. Java多线程--设计模式(二、Immutable Object(不可变对象)模式)

    一.Immutable Object 模式简介 多线程共享变量的情况下,为了保证数据的一致性,往往需要对这些变量的访问进行加锁.而锁本身又会带来一些问题和开销.Immutable Object 模式使 ...

  2. 关于hash哈希以及为什么python中dict和set的key必须为不可变对象

    引用自-vczh 源:https://segmentfault.com/q/1010000004077532 哈希值是由哈希函数从一个给定的数据计算出来的.哈希函数H和数据v满足如下要求: 1.需要有 ...

  3. 如何优雅的创建一个Java不可变对象类,JDK源码中也是这么干的!

    前面有篇文章当介绍了Java的不可变对象的一些特性,以及它的一些好处,但是并没有介绍如何实现一个不可变对象类.今天就来看看如何实现一个不可变对象类. Java中常用的不可变对象类 String类应该是 ...

  4. java对象的我可变属性,不可变对象的所有属性都必须是最终的吗?

    不可变对象(所有属性都是final)和有效不可变对象(属性不是final,但不能更改)之间的主要区别是安全发布. 由于Java内存模型为最终字段提供了保证,因此您可以在多线程上下文中安全地发布不可变对 ...

  5. java高并发(八)不可变对象

    有一种对象一旦发布了,那么他就是安全对象,这就是不可变对象. 不可变对象需要满足的条件: 对象创建以后其状态就不能修改 对象所有的域都是final类型 对象是正确创建的(在对象创建期间,this引用没 ...

  6. Java高并发编程详解系列-不可变对象设计模式

    导语   在开发中涉及到的所有关于多线程的问题都离不开共享资源的存在.那么什么是共享资源,共享资源就是被多个线程共同访问的数据资源,而且每个线程都会引起它的变化.伴随共享资源而生的新问题就是线程安全, ...

  7. Java™ 教程(不可变对象)

    不可变对象 如果一个对象的状态在构造后不能改变,则该对象被认为是不可变的,对不可变对象的最大依赖被广泛认为是一种创建简单.可靠代码的合理策略. 不可变对象在并发应用程序中特别有用,由于它们不能改变状态 ...

  8. 合理利用Java不可变对象,让你的代码更加优雅

    如果一个对象的状态在创建以后就不会再改变,那么就可以认为它是不可变对象.因为这些对象无法被修改,所以不需要考虑多线程下的同步问题,这使得不可变对象在多线程程序中能够发挥重大作用. 如下所示,Strin ...

  9. java io面试题_【Java面试】Java常见IO面试题!

    字符串分解==============OutStream==========>write()方法写到文件中 2:描述I/O流的基本接口和类的结构 InputStream OutputStream ...

  10. java 网络爬虫_如何用Java实现网络爬虫

    原标题:如何用Java实现网络爬虫 微信公众号"书圈"后台回复[Javapachong1],下载本例的PPT和源码 作品描述 本章作品是一个能够抓取指定网站ACM比赛信息的爬虫.A ...

最新文章

  1. mysql in边界_mysql中 where in 用法详解
  2. 线程中应该注意的问题
  3. 互联网协议 — OAuth2 第三方授权协议
  4. python 打开文件-Python open()文件处理使用介绍
  5. BZOJ.3218.a + b Problem(最小割ISAP 可持久化线段树优化建图)
  6. 物理光学11 衍射的基本概念与惠更斯原理
  7. 图解LSTM神经网络架构及其11种变体(附论文)
  8. Multisim14.0 安装教程
  9. 【软考-软件设计师】数据表示
  10. 微机原理汇编之部分重要知识整合包括:判断解释程序运行(新手有利)并画存储结构,字节变量,寻址方式
  11. linux mysql dump命令_linux下mysql命令
  12. Vivado FIFO IP核接口信号介绍
  13. html获取url后面的参数_【python量化】用Python获取基金历史净值数据
  14. 600个超实用ICON图标矢量
  15. 最长的指定瑕疵度的元音字串 —— 最优解法(C++实现)
  16. pyPDF2或pyPDF4做pdf页面提取出错
  17. 10个H5页面制作工具
  18. Qt应用程序开发九:上传文件
  19. 关于java的搞笑段子_让人笑到抽筋的10条搞笑段子
  20. 【项目】用户可自定义简易宏键盘

热门文章

  1. PKU ACM 1008 玛雅历
  2. 电子计算机特征具有什么功能,电子计算机的基本特征有哪些?
  3. c语言搭积木游戏,我们一起搭积木,做游戏,就这就简单
  4. Windows Server 2016-Win Ser 2016已删减内容
  5. 艾宾浩斯遗忘曲线函数
  6. 64位win7下Android SDK Manager闪退的解决方法
  7. linux桌面虚拟化-KVM
  8. Redis学习(二):redis集群之cluster模式下的跨节点的批量操作 I
  9. JS逆向解析案例-巨潮证券市场数据库(python)
  10. 国内大多数网站的密码在 post 传输过程中都是明文的,这正常吗?