Java-各种类型的等于比较

Integer、Long、Float、Double、BigDecimal、String 等于的比较方式

public static void main(String[] args) {
	intCompare();
	longCompare();
	floatCompare();
	doubleCompare();
	bigDecimalCompare();
	stringCompare();
}

public static void intCompare() {
	int int1 = 1;
	int int2 = 1;
	Integer integerNew1 = new Integer(1);
	Integer integerNew2 = new Integer(1);
	Integer integerNew3 = new Integer(2);
	Integer integer1 = 2;
	Integer integer2 = 2;
	System.out.println("int1 == int2 -> " + (int1 == int2));
	System.out.println("int1 == integerNew1 -> " + (int1 == integerNew1));
	System.out.println("integerNew1 == integerNew2 -> " + (integerNew1 == integerNew2));
	System.out.println("integerNew3 == integer1 -> " + (integerNew3 == integer1));
	System.out.println("integer1 == integer2 -> " + (integer1 == integer2));
	/*运行结果:
	int1 == int2 -> true
	int1 == integerNew1 -> true
	integerNew1 == integerNew2 -> false
	integerNew3 == integer1 -> false
	integer1 == integer2 -> true*/
}

private static void longCompare() {
	long long1 = 1;
	long long2 = 1;
	Long longNew1 = new Long(1);
	Long longNew2 = new Long(1);
	Long longNew3 = new Long(2);
	Long longA = 2L;
	Long longB = 2L;
	System.out.println("long1 == long2 -> " + (long1 == long2));
	System.out.println("long1 == longNew1 -> " + (long1 == longNew1));
	System.out.println("longNew1 == longNew2 -> " + (longNew1 == longNew2));
	System.out.println("longNew3 == longA -> " + (longNew3 == longA));
	System.out.println("longA == longB -> " + (longA == longB));
	/*运行结果:
	long1 == long2 -> true
	long1 == longNew1 -> true
	longNew1 == longNew2 -> false
	longNew3 == longA -> false
	longA == longB -> true*/
}

private static void floatCompare() {
	float float1 = 10.222222225f;
	float float2 = 10.222222229f;
	System.out.println("float1 == float2 -> "+(float1 == float2));
	System.out.println("Math.abs(float1-float2) > 0 -> " + (Math.abs(float1-float2) > 0) );
	System.out.println("Math.abs(float1-float2) < 0.00000001 -> " + (Math.abs(float1-float2) < 0.00000001) );
	Float floatNew1 = new Float(float1);
	Float floatNew2 = new Float(float2);
	System.out.println("floatNew1.compareTo(floatNew2) == 0 -> " +(floatNew1.compareTo(floatNew2) == 0));
	BigDecimal decimal1 = new BigDecimal(float1);
	BigDecimal decimal2 = new BigDecimal(float2);
	System.out.println("decimal1.compareTo(decimal2) == 0 -> " + (decimal1.compareTo(decimal2) == 0));
	/*运行结果:
	float1 == float2 -> true
	Math.abs(float1-float2) > 0 -> false
	Math.abs(float1-float2) < 0.00000001 -> true
	floatNew1.compareTo(floatNew2) == 0 -> true
	decimal1.compareTo(decimal2) == 0 -> true*/
}

private static void doubleCompare() {
	double double1 = 0.0;
	for (int i = 1; i <= 11; i++) {
		double1 += 0.1;
	}
	double double2 = 0.1 * 11;
	System.out.println("double1 = " + double1 + "; double2 = " + double2);
	Double doubleNew1 = new Double(double1);
	Double doubleNew2 = new Double(double2);
	System.out.println("doubleNew1.compareTo(doubleNew2) == 0 -> " +(doubleNew1.compareTo(doubleNew2) == 0));
	BigDecimal decimal1 = new BigDecimal(doubleNew1.toString());
	BigDecimal decimal2 = new BigDecimal(doubleNew2.toString());
	System.out.println("decimal1.compareTo(decimal2) == 0 -> " + (decimal1.compareTo(decimal2) == 0));
	/*运行结果:
	double1 = 1.0999999999999999; double2 = 1.1
	doubleNew1.compareTo(doubleNew2) == 0 -> false
	decimal1.compareTo(decimal2) == 0 -> false*/
}

private static void bigDecimalCompare() {
	BigDecimal decimal1 = new BigDecimal("1");
	BigDecimal decimal2 = new BigDecimal("1.00");
	BigDecimal decimal3 = new BigDecimal("2");
	System.out.println("decimal1.compareTo(decimal2) == 0 -> " + (decimal1.compareTo(decimal2) == 0));
	System.out.println("decimal1.compareTo(decimal3) == 0 -> " + (decimal1.compareTo(decimal3) == 0));
	/*运行结果:
	decimal1.compareTo(decimal2) == 0 -> true
	decimal1.compareTo(decimal3) == 0 -> false*/
}

private static void stringCompare() {
	String string1 = "A";
	String string2 = "A";
	String string3 = "B";
	System.out.println("string1.equals(string2) -> " + string1.equals(string2));
	System.out.println("string1.equals(string3) -> " + string1.equals(string3));
	/*运行结果:
	string1.equals(string2) -> true
	string1.equals(string3) -> false*/
}

文章仅作为个人学习整理,欢迎指正

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值