因为我的java是快速上手的——没看基础,直接改别人代码。然后就开始做。所以一些基础不知道
SyncSysLink link = new SyncSysLink();
link.setReached(1);
Short one = 1;
if(link.getReached().equals(one)) {
System.out.println("yes");//<-结果
}else
System.out.println("no");
if(link.getReached().equals(1)) {
System.out.println("__yes");
}else
System.out.println("__no");//<-结果
if(link.getReached()==1) {
System.out.println("yes 1");//<-结果
}else
System.out.println("no 1");
link.setReached(128);
if(link.getReached()==128) {
System.out.println("yes 2");//<-结果
}else
System.out.println("no 2");
Short _128 = 128;
if(link.getReached().equals(_128)) {
System.out.println("yes 3");//<-结果
}else
System.out.println("no 3");
short __128 = 128;
if(__128==128) {
System.out.println("yes 4");//<-结果
}else
System.out.println("no 4");
Double d1 = 1.0;
Double d2 = 1.0;
if(d1==d2) {
System.out.println("yes 5");
}else
System.out.println("no 5");//<-结果
Double d3 = 128.0;
Double d4 = 128.0;
if(d4==d3) {
System.out.println("yes 6");
}else
System.out.println("no 6");//<-结果
Float f1 = 1.0f;
Float f2 = 1.0f;
if(f1==f2) {
System.out.println("yes 7");
}else
System.out.println("no 7");//<-结果
Float f3 = 128.0f;
Float f4 = 128.0f;
if(f3==f4) {
System.out.println("yes 8");
}else
System.out.println("no 8");//<-结果
Short s1 = 1;
Short s2 = 1;
if(s1==s2) {
System.out.println("yes 9");//<-结果
}else
System.out.println("no 9");
Short s3 = 128;
Short s4 = 128;
if(s3==s4) {
System.out.println("yes 10");
}else
System.out.println("no 10"); //<-结果
if(d1.equals(f1)) {
System.out.println("yes 11");
}else
System.out.println("no 11"); //<-结果
Integer I1 = 1;
int i1= 1;
Long L1=1L;
long l1=1;
if(d1.equals(I1)) {
System.out.println("yes 12");
}else
System.out.println("no 12"); //<-结果
if(i1==I1) {
System.out.println("yes 13");//<-结果
}else
System.out.println("no 13");
if(l1==i1) {
System.out.println("yes 14");//<-结果
}else
System.out.println("no 14");
yes
__no
yes 1
yes 2
yes 3
yes 4
no 5
no 6
no 7
no 8
yes 9
no 10
no 11
no 12
yes 13
yes 14
equals 只能比较同类型的,
也就是Integer equals Integer or int, Short equals Short or short, Long equals Long or long, Doule equals Doule or double, Float equals Float or float 会符合预期。
Integer,Short, Long, Doule, Float 去 equals int, long, short, double, float 始终不等。Short去equals Long, Doule, Float始终不等,其它同理。
==适合int,short,long,double, float 与 任意类型比较,
如果用==比较:Short==Short, Long==Long, Doule==Double, Float==Float, Integer==Integer。Short, Long值小于128会判断正确,大于就始终不等;Double,Float始终不等。
Short不能去==Long,Double,Float,会报语法错误
总结:
Integer,Short, Long, Doule, Float 同类型用equals,其它时候用==。
9:52 2021/3/26 纠正,long与int比较如果都小于 Integer.MAX_VALUE,则正常,如果 long超过Integer.MAX_VALUE则反而会比int小。
所以数字的比较其实是从地位按bit比较的
也就是
long = Integer.MAX_VALUE+1,int =1时
long = 0001 0000
int = 0001
实际比较的是右边(高位地位的称呼我不清楚),所以int更大