-
包装类、装箱、拆箱
程序设计已经发展了几十年了,从面向过程到面向对象,再到现在的面向函数等,最核心的还是将现实世界转换成数字世界。什么是现实世界呢,比如描述一个男孩带女朋友回家,有一个会说话的另据是这样夸的“这孩子真像他爸会找对象”,结果把他们一家四人都夸上了,真牛。怎么描述这个男孩呢,应该是类似这样的,付宇奇,男,36岁,软件工程学士,大数据开发技术专家/物联网开发技术专家。大家看到了,这些人,这些事,在数字世界里进行抽象后,可以用数字、字符、列表、元组、集合等来表达。在Java中,表达数字的有byte, short, int, long, float, double, 它们背后都有一个包装类来表达在面向对象世界中的实体,分别是Byte、Short、Integer、Float、Double。干什么事都是有成本的,创建对象也不例外。学过计算机的圈内人士都知道,在算法界有“空间换时间”和“时间换两间”两个思想的存在。JVM在设计时,也利用了很多这种原则,所以有IntegerCache的存在,将-128~127这256个整数缓存了起来。在C语言面向过程的世界里,有指针和值两个慨念。在Java面向对象的世界了,有对应的引用和值这两个概念,引用指的是一个实例,值是指这个实例所描述的实体信息,比如Integer age = 36, 这个age变量就代表一个对象引用,大家知道它被JVM缓存了,所以你如果再声明一个Integer jamesAge = 36, 那么这两个引用(指向同一个值)其实是同一个,值也相等。那么当把值分别改成1000和1000后呢,他们两个引用就不一样了,值虽然一样,但是计算机不会优化这个事情,他会给你创建两个对象出来,所以他们两个变量的引用是不一样的。但是我们在做计算时,需要知道他们背后的值是否一样来实现我们的逻辑,在Java中需要用Object#equals方法来判断值是否相等。Object是所有类的基类,所以它身上有大量的方法、机制来供我们编程人员使用。
说了这么多,你应该明白了吧?来看个真实的编程案例:
类定义
这里使用到的Integer\Byte\Boolean都是包装类。为什么一定要使用包装类呢?因为现实世界的情况是非常复杂的,不是真的像信息世界中只存在0和1。比如在数据库中我们经常用NULL来表达不确定、未知等情况,当然我们也可以用空字符串-1-999等来表达。。。

请大家仔细看434行,这里会发生什么?知道的请call 1。不知道的麻烦发个红包。
作者期望知道这个对象的“项目类型”是否是“机关单位”,也就是这个对象的“项目类型”这个属性的值是不是数字1。真实情况是这个字面量整数1会被自动装箱为一个整数包装类,它也是一个对象。注意两个类型不同的对象是一定不会相等的,因为这个规则是“詹姆斯·高斯林”定的:

Byte继承于Object, Object#equals只会比较引用,Byte#equals会先判断类型、再判断值。
/**
* Indicates whether some other object is "equal to" this one.
* <p>
* The {@code equals} method implements an equivalence relation
* on non-null object references:
* <ul>
* <li>It is <i>reflexive</i>: for any non-null reference value
* {@code x}, {@code x.equals(x)} should return
* {@code true}.
* <li>It is <i>symmetric</i>: for any non-null reference values
* {@code x} and {@code y}, {@code x.equals(y)}
* should return {@code true} if and only if
* {@code y.equals(x)} returns {@code true}.
* <li>It is <i>transitive</i>: for any non-null reference values
* {@code x}, {@code y}, and {@code z}, if
* {@code x.equals(y)} returns {@code true} and
* {@code y.equals(z)} returns {@code true}, then
* {@code x.equals(z)} should return {@code true}.
* <li>It is <i>consistent</i>: for any non-null reference values
* {@code x} and {@code y}, multiple invocations of
* {@code x.equals(y)} consistently return {@code true}
* or consistently return {@code false}, provided no
* information used in {@code equals} comparisons on the
* objects is modified.
* <li>For any non-null reference value {@code x},
* {@code x.equals(null)} should return {@code false}.
* </ul>
* <p>
* The {@code equals} method for class {@code Object} implements
* the most discriminating possible equivalence relation on objects;
* that is, for any non-null reference values {@code x} and
* {@code y}, this method returns {@code true} if and only
* if {@code x} and {@code y} refer to the same object
* ({@code x == y} has the value {@code true}).
* <p>
* Note that it is generally necessary to override the {@code hashCode}
* method whenever this method is overridden, so as to maintain the
* general contract for the {@code hashCode} method, which states
* that equal objects must have equal hash codes.
*
* @param obj the reference object with which to compare.
* @return {@code true} if this object is the same as the obj
* argument; {@code false} otherwise.
* @see #hashCode()
* @see java.util.HashMap
*/
public boolean equals(Object obj) {
return (this == obj);
}
TestCase


- MySQL 表设计、字符编码
- MySQL 间隙锁
- 一个递归Bug
- Java程序发布、优雅停机
以上主题留着慢慢写,有兴趣的同学来看
看明白的请call 1,没看明白的请加我微信给你开个小灶。
内容创作不易,请大家捧场。请有想法的朋友加我,尤其是销售、客户成功经理们。就这样吧,想聊的来我的“知识星球”。
限制你发展的不是你的学历,也不是你的智商,而是你所身处的生活圈和身边的人,你接近什么样的人就会走什么样的路。所谓贵人并不是直接把钱给你的人,而是开拓你的眼界,纠正你的格局,给你正能量的人。人生最大的运气是有人愿意指引你走向更高、更大的舞台!
网友评论