==
- 先看Java
/**
* Author:Mr.X
* Date:2017/10/8 23:17
* Description:
*
* @==判断两个内存地址是否相同
* @基础类型有(char,byte,short,short,int,long,float,double,boolean)
* @基础类型包装类有(Char,Byte,Short...)
*
* @值相同的情况下:
* @结论1:基础类型==基础类型都为true----------------------------------->值比较
* @结论2:基础类型==基础类型包装类都为true------------------------------>值比较,一般都不会同时写(int,Integer)两种类型
* @结论3:基础类型包装类==基础类型包装类
* @Byte,Short,Integer,Long(-127~128之间true,超过这个范围为false)----->引用比较,但有缓存机制
* @Float,Double等都为false------------------------------------------>引用比较
*/
public class Test {
public static void main(String[] args) {
int a = 1;
int b = 1;
System.out.println(a == b); //true
float c = 1;
System.out.println(a == c); //true
double d = 1;
System.out.println(a == d); //true
Integer e = 1;
System.out.println(a == e); //true
/**
* Byte,Short,Integer,Long缓存范围(-127~128)
* System.out.println(Byte.MAX_VALUE);
* System.out.println(Byte.MIN_VALUE);
*/
Integer f = 10;
Integer g = 10;
System.out.println(f == g); //true
Integer h = 300;
Integer i = 300;
System.out.println(h == i); //false
Float j = 10f;
Float k = 10f;
System.out.println(j == k); //false
}
}
- 对比看看js
<script type="text/javascript">
var a = 10;
var b = "10";
console.log(a == b); //true
console.log(a === b); //false ===表示(a==b && typeof(a)==typeof(b))
console.log(typeof(a)); //number
console.log(typeof(b)); //string
</script>
equals
Integer a = 10;
Integer b = 10;
System.out.println(a.equals(b)); //true
Float c = 10f;
System.out.println(a.equals(c)); //false
true
false
equals在同一数据类型时,值相等则相等,否则为false,上例中a和c数据类型不同,所以为false
if (!a.equals("") && a != null) 和 if (a != null && !a.equals(""))的选择
// 这样写才是对的,自己思考为什么
if (a != null && !a.equals("")) {
}