Java的int -128到127的问题与解决办法

本文探讨了Java中将int类型值赋给Object类型时遇到的问题,当int值超过127,自动装箱为Integer时,无法通过`==`比较相等。分析了Java自动装箱的原理,特别是Integer缓存机制(-128到127)。为解决此问题,建议在自定义数据类型中覆盖`equals()`方法,以便通过`equals()`进行值相等的判断,而非依赖于对象地址的比较。
摘要由CSDN通过智能技术生成

目录

一、问题描述

二、问题解析

2.1 类型解析

2.2 包装局限

2.3 解决办法


一、问题描述

写了个实现单链表的代码,其中有一条按值删除,测试单链表插入int 1-1000,删除方法接受的参数是Object类型,但是发现删除的时候,只能删除到127。到128就石沉大海了。

下面是单链表结点数据类型:

public class SingleListNode {
	private Object element;
	private SingleListNode next;
	public SingleListNode(Object it,SingleListNode nextval) {element = it;next = nextval;}
	public SingleListNode(SingleListNode nextval) {next=nextval;}
	public Object getElement() {return element;}
	public void setElement(Object it) {this.element = it;}
	public SingleListNode getNext() {return next;}
	public void setNext(SingleListNode nextval) {this.next = nextval;}
}

 单链表数据类型,里面有插入和删除的功能:

public class SLList implements LinkedList {
	private SingleListNode head;
    public SLList() {
		head=new SingleListNode(null);
	}
    public void inserttoHead(Object item) {
		// 将头结点的子节点设置为新节点的子节点
		// 将新节点作为头结点的子节点
		head.setNext(new SingleListNode(item,head.getNext()));
	}
    public void removebyValue(Object item) {
		
//			System.out.println(item);
		
		/**
		 * 用一个游标来记录当前节点的父节点
		 */
		if(isEmpty()==true)return;
		SingleListNode curNode = head.getNext();
		SingleListNode curNode_father = head;
		if((int)item>127)System.out.println(curNode.getElement());
		/**
		 * 当传入的item是int,且>127时,如128,我们发现即使curNode.getElement()的值是128那也不会被判定为相等
		 * 
		 */
		while((curNode!=null)&&(curNode.getElement()!=item)) {
			if((int)item>127) {System.out.println(curNode.getElement());}
			curNode_father = curNode;
			curNode = curNode.getNext();
		}
		if((curNode!=null)&&(curNode.getElement()==item)) {
			curNode_father.setNext(curNode.getNext());
			System.out.println(item);
		}
	}
}

测试代码:

public class test {
    public static void main(String[] args) {
		// TODO Auto-generated method stub
		SLList sll = new SLList();
		long startnsTime = System.nanoTime();//精确到纳秒
		for(int i=1000;i>0;i--) {
			sll.inserttoHead(i); //单链表顺序 1、。。。、1000
		}
		long endnsTime = System.nanoTime();//精确到纳秒
		System.out.println("插入到头1~1000时间:"+(endnsTime-startnsTime)+"ns");
		int i=0;
		long startTime_removebyvalue_allfromhead = System.nanoTime();
		while((i++)<1001) {
			sll.removebyValue(i);// 删除顺序 1、...、1000
		}
		long endTime_removebyvalue_allfromhead = System.nanoTime();
		System.out.println("按值从头删除:"+(endTime_removebyvalue_allfromhead-startTime_removebyvalue_allfromhead)+"ns");}
}

运行过程中发现只能删除到127,从128开始无法删除,经过测试,是从removebyValue方法中的删除代码的这段命令开始变化:

curNode.getElement()!=item

就是超过127的int值不能与被赋予同样int值的Object再等值了。

二、问题解析

int是一种基本数据类型,是原语。不会作为对象来处理。

public class test {
	public static void main(String[] args) {
		Object it = new Object(); // Object类型
		int i
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

从零开始的智障生活

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

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

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

打赏作者

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

抵扣说明:

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

余额充值