Integer的“==”

Integer的“==”

使用Integer时,我们可能会遇到以下令人困惑的现象`

public static void main(String[] args)
	{		 
	Integer a=11;
	Integer b=11;
	if(a==b)
		System.out.println("true");
	else
		System.out.println("false");
	}

输出结果为:true输出结果1

但是,如果变为以下:

public static void main(String[] args)
	{		 
	Integer a=1111;
	Integer b=1111;
	if(a==b)
		System.out.println("true");
	else
		System.out.println("false");
	}

输出结果就变为了:false
输出结果2

这是神马情况??
其实,Integer与int不一样,它是一个类(对象包装器),而int是基本类型,不属于类。比较两个对象是否相当,需要使用equals方法。而“==”是检测两个对象是否指向同一个存储区域。因此比较a和b应该使用equals方法:

public static void main(String[] args)
	{		 
	Integer a=1111;
	Integer b=1111;
	if(a.equals(b))
		System.out.println("true");
	else
		System.out.println("false");
	}

结果为:true
输出3

那为什么第一次使用“==”比较,输出也为true呢?
那是因为,介于-127~128的int会被包装到固定的对象中,它们的存储区域是相同的,检测结果自然是“true”

Integer.valueOf(int) 方法,,也是同样的,会去固定的对象。下面i、j是相同的。

Integer i = Integer.valueOf(1);
Integer j = Integer.valueOf(1);

还有一种情况:

Integer a = new Integer(1);
Integer b = 1;
if(a == b){
    System.out.println(true);
}else{
    System.out.println(false);
}

结果是:

false

Process finished with exit code 0

new一定是创建新对象。

### Integer Comparison Operations In programming, integer comparisons are fundamental operations used to evaluate relationships between integers. Common comparison operators include `==` (equality), `!=` (inequality), `<` (less than), `>` (greater than), `<=` (less than or equal to), and `>=` (greater than or equal to). These operators return boolean values indicating whether the specified condition is true or false. For example, when comparing two integers: ```python a = 5 b = 10 print(a == b) # False print(a != b) # True print(a < b) # True print(a > b) # False print(a <= b) # True print(a >= b) # False ``` Integer types also support derivation from certain traits such as `PartialEq` and `PartialOrd`, which provide functionality for equality and ordering respectively[^1]. This means that without manual implementation, these derived traits allow direct use of comparison operators on custom enum types containing integers. When working with pandas in Python, especially concerning categorical data represented by integers, methods like one-hot encoding transform non-numerical labels into binary vectors suitable for machine learning algorithms[^2]. ### Example Code Demonstrating Integer Comparisons Below is an illustrative code snippet showing how integer comparisons work within conditional statements: ```python def compare_integers(x, y): if x < y: result = f"{x} is less than {y}" elif x > y: result = f"{x} is greater than {y}" else: result = "Both numbers are equal" return result # Testing function with sample inputs print(compare_integers(7, 9)) # Output: '7 is less than 9' print(compare_integers(-3, -8)) # Output: '-3 is greater than -8' print(compare_integers(4, 4)) # Output: 'Both numbers are equal' ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_43751710

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值