深入理解Java原始数据类型和包装类关于==和equals的比较
来源:互联网 发布:2017全球云计算大会 编辑:程序博客网 时间:2024/06/05 02:35
1.运算符 ==
对于六大Java数值类原始数据类型,==比较的是数值
对于六大Java原始数据类型对应的包装类,==比较的是内存地址
2.equals()
equals()方法只有对象才有,所以我们讨论一下原始数值类型的包装在的equals()方法吧!
对于六大Java原始数据类型对应的包装类,equals都是比较值。
有源码为证如下所示:
Byte类
Short类
Integer类
Long类
Double类
/** * Compares this object against the specified object. The result * is {@code true} if and only if the argument is not * {@code null} and is a {@code Double} object that * represents a {@code double} that has the same value as the * {@code double} represented by this object. For this * purpose, two {@code double} values are considered to be * the same if and only if the method {@link * #doubleToLongBits(double)} returns the identical * {@code long} value when applied to each. * * <p>Note that in most cases, for two instances of class * {@code Double}, {@code d1} and {@code d2}, the * value of {@code d1.equals(d2)} is {@code true} if and * only if * * <blockquote> * {@code d1.doubleValue() == d2.doubleValue()} * </blockquote> * * <p>also has the value {@code true}. However, there are two * exceptions: * <ul> * <li>If {@code d1} and {@code d2} both represent * {@code Double.NaN}, then the {@code equals} method * returns {@code true}, even though * {@code Double.NaN==Double.NaN} has the value * {@code false}. * <li>If {@code d1} represents {@code +0.0} while * {@code d2} represents {@code -0.0}, or vice versa, * the {@code equal} test has the value {@code false}, * even though {@code +0.0==-0.0} has the value {@code true}. * </ul> * This definition allows hash tables to operate properly. * @param obj the object to compare with. * @return {@code true} if the objects are the same; * {@code false} otherwise. * @see java.lang.Double#doubleToLongBits(double) */ public boolean equals(Object obj) { return (obj instanceof Double) && (doubleToLongBits(((Double)obj).value) == doubleToLongBits(value)); }
Float类
/** * Compares this object against the specified object. The result * is {@code true} if and only if the argument is not * {@code null} and is a {@code Float} object that * represents a {@code float} with the same value as the * {@code float} represented by this object. For this * purpose, two {@code float} values are considered to be the * same if and only if the method {@link #floatToIntBits(float)} * returns the identical {@code int} value when applied to * each. * * <p>Note that in most cases, for two instances of class * {@code Float}, {@code f1} and {@code f2}, the value * of {@code f1.equals(f2)} is {@code true} if and only if * * <blockquote><pre> * f1.floatValue() == f2.floatValue() * </pre></blockquote> * * <p>also has the value {@code true}. However, there are two exceptions: * <ul> * <li>If {@code f1} and {@code f2} both represent * {@code Float.NaN}, then the {@code equals} method returns * {@code true}, even though {@code Float.NaN==Float.NaN} * has the value {@code false}. * <li>If {@code f1} represents {@code +0.0f} while * {@code f2} represents {@code -0.0f}, or vice * versa, the {@code equal} test has the value * {@code false}, even though {@code 0.0f==-0.0f} * has the value {@code true}. * </ul> * * This definition allows hash tables to operate properly. * * @param obj the object to be compared * @return {@code true} if the objects are the same; * {@code false} otherwise. * @see java.lang.Float#floatToIntBits(float) */ public boolean equals(Object obj) { return (obj instanceof Float) && (floatToIntBits(((Float)obj).value) == floatToIntBits(value)); }
阅读全文
0 0
- 深入理解Java原始数据类型和包装类关于==和equals的比较
- 数值包装类型和equals和=
- 包装类的equals和==
- java equal和==的比较,尤其注意基本类型和基本类型的包装类型的比较
- 关于equals 和 == 的理解
- Java 的==和equals比较
- Java源码解析:深入理解==和equals()
- javascript 引用类型 - 基本包装类型(深入理解基本包装类型和基本类型的区别)
- 关于java的基本类型和包装类型
- 探索java基本类型和包装类型的使用运算符==进行比较的底层细节
- 关于“==”和equals()的比较
- java中原始数据类型与包装类型
- 关于java String的“==”和equals()比较、引用的小问题
- 关于java String的“==”和equals()比较、引用的小问题
- 关于java String的“==”和equals()比较、引用的小问题
- Java的基本类型和包装类型
- 关于equals()和==操作的一点理解
- 关于equals方法和“==”的个人理解
- JVM 之 GC日志分析
- 最长子数组问题、递归动态规划问题
- Android studio 点击按钮跳转到新的Activity
- C++实现顺序表&单链表&双链表
- Android编译错误:You have tried to change the API from what has been previously approved.
- 深入理解Java原始数据类型和包装类关于==和equals的比较
- Fragment的应用,实现横竖切换并兼容平板
- 数据库连接池
- AndroidManifest.xml详解
- 教程序员如何怒怼产品经理
- 自考—《数据结构导论》
- mysql全备份
- uva 227Puzzle
- Android常用命令