0413-多态、Object类方法、访问权限修饰符、装箱拆箱、128陷阱

1:A and A

2:A and A

3: A and D

4: B and A

5: B and A

6:A and D

7:B and A

8: B and A

9:A and D

java 复制代码
package 第四章对象和类;

public class ForthThir {
	//多态:父类的引用指向子类的对象,只能调父类的方法和子类重写的方法,不能调子类独有的方法;子类则可以调父类独有的方法
	//条件:必须继承关系、必须要重写,父类的引用指向子类对象
	/*
	 * public class Parent{
	 * 	public void eat(){System.out.println("Parent"); }
	 * }
	 * 
	 * public class Child1 extends Parent{
	 * 	public void eat(){System.out.println("Child1");}
	 * }
	 * 
	 *  public class Child2 extends Parent{
	 * 		public void eat(){System.out.println("Child2");}
	 * }
	 * 
	 * public class Grand1 extends Child1{
	 * 	public void eat(){System.out.println("Grand1");}
	 * }
	 * 
	 * 
	 * public class Test{
	 * 		public static void main(String[] args){
	 * 			Parent xx=new Parent();
	 * 			xx.eat();
	 * 			xx=new Child1();
	 * 			xx.eat();//执行Child1里的方法
	 * 
	 * 
	 * 			aa(new Parent());
	 * 			aa(new Child1());
	 * 		}
	 * 		public static void aa(Parent xx){
	 * 			xx.eat();
	 * 		}
	 ///强制类型转换 大碗不能自动转成小碗
	  * Parent xx =new Grand1();
	  * Child1 yy=(Child1)xx;
	  * Child2 yy=(Child2)xx;//运行报错,xx内核为Grand类型数据
	  * 
	  * 引用类型到底能不能转,做判断:
	  * instanceof 判断xx是否位Child2类型的对象,或者Child2子孙后代的对象
	  * if(xx instanceof Child2){
	  * 	Child2 yy=(Child2)xx;
	  * 
	  * }
	  * 
	  * 
	  * int a=32;
	  * short b=(short)a;
	  * 
	  * int[] arr={100,2457,120,22};
	  * for(int i=0;i<arr.length;i++){
	  * 	char j =(char)arr[i];
	  * 	System.out.println(j);
	  * }
	 * }
	 */
	//基本数据类型
	/*
	 * int a=10;
	 * Integer b=10;
	 * 
	 * int Integer String 
	 * 
	 * int->Integer new Integer():
	 * int a=10;
	 * Integer a_p=new Integer(a);
	 * 
	 * Integer->int intValue()
	 * Integer a_p=new Integer(4);
	 * int a=a_p.intValue();
	 * 
	 * Integer->String toString();
	 * Integer a_p=new Integer(4);
	 * 
	 * 
	 * 
	 * String->/integer:new Integer()
	 * String a="888";
	 * Integer b=new Integer(a)
	 * 
	 * int->String String.valueOf()
	 * int a=10;
	 * String b=String.valueOf(a);
	 * 
	 * String->int Integer.parseInt()
	 * String a="999";
	 * int b=Integer.parseInt(a);
	 * 
	 */
	 //装箱:基本类型->包装类
	 /*
	  * int a=10;
	  * Integer i=Integer.valueOf(a);
	  * 或Integer j=new Integer(a);
	  */
	//拆箱 包装类->基本类型
	/*
	 * Integer k=new Integer(10);
	 * int b=k.intValue();
	 */
	//自动装箱 直接把一个int类型的数据直接赋值到Integer(java 5之后)
	/*
	 * int c=20;
	 * Integer p_auto=a;
	 * 或 Integer m=100;
	 */
	//自动拆箱(把一个包装类型的数据变成基本类型数据)
	/*
	 * Integer l=new Integer(10);
	 * int d_auto=l;
	 */
	//128陷阱
	/*
	 * Integer num1=100;/=150 调的是valueOf方法,避免频繁创建 
	 * Integer num2=100;/=150
	 * System.out.println(num1==num2);true/false 超过128就是false
	 * Integer num3=new Integer(100);
	 * Integer num4=new Integer(100);
	 *  System.out.println(num3==num4);false
	 *  
	 *  
	 */

}
java 复制代码
package com.qc.Object;

public class Test {
	public static void main(String[] args) {
		Object aa=new Object();
		aa.equals(obj);
		aa.hashCode();
		//equals()比较内存地址指向是否相同
		//getClass()获取类的信息 反射邻域
		//hashCode()获取散列码 根据地址生成
		//哈希表:数组+链表结构
		String a="ok";
		String b=new String("ok");
		
		Person x1=new Person();
		Person x2=new Person();
		System.out.println(x1.hashCode());
		//x1,x2的hashCode不同;a,b的hashCode相同,因为String重写了方法
		
		//(Objects里)equals判断是否为同一个对象;hashCode判断是否是同一类 大部分类在重写equals方法时会重写hashCode方法
		//如果两个对象相同(equals方法返回true),那么他们的hashCode值一定相同
		//如果两个对象不同(equals方法返回false),他们的hashCode值可能相同 可能不同
		//如果两个对象hashCode值相同(存在哈希冲突),他们可能相同可能不同(equals方法可能返回false,可能返回true)
		//如果两个对象hashCode值不同,那么他们两个肯定不同(equals方法返回false)
		
		//面试题1:两个不同的对象,hashCode有没有可能相同? 可能,有极小概率
		//面试题2:重写equals方法为什么要重写hashCode方法?一定要重写吗?
		//跟hashmap 如果不使用hashmap则不需要重写hashCode;equals和hashCode在hashmap里是配合使用的
		//hashmap比较key值的时候使用equals方法比较是否重复,存入values时要使用hashCode确定位置
		
		
		//访问权限修饰符(修饰变量/方法) 当前类       同包       子类 跨包    其他包
		//public                        ✔                ✔            ✔                   ✔
		//protected                     ✔                ✔            ✔                   ✖
		//默认  不写                    ✔                ✔              ✖                 ✖
		//private                       ✖                 ✖             ✖                 ✖


		
		//wait() 线程进入等待状态 让出CPU和锁
		//notify() 唤醒等待的线程
		//toString() 输出对象的类型和内存地址
		
		//hashCode()
	}
}

"128陷阱"是Java中关于整数缓存机制的一个经典问题,主要涉及Integer类的缓存行为和自动装箱/拆箱机制。

  1. Integer缓存机制:

    • Java对-128127之间的Integer对象进行了缓存

    • 当使用自动装箱或Integer.valueOf()方法时,会优先从缓存中获取对象

底层原理

  1. IntegerCache类:

    • Java在Integer类内部维护了一个静态的IntegerCache

    • 默认缓存了-128到127之间的Integer对象

  2. 自动装箱行为:

    • 当使用Integer i = 100这样的语法时,实际调用的是Integer.valueOf(100)

    • valueOf()方法会先检查是否在缓存范围内

目的:

  • 减少对象创建:对于常用的小整数(-128~127),通过缓存复用对象,避免重复创建
相关推荐
Scott9999HH2 小时前
【IIoT流量实战】蒸汽管道阀门全关却仍有流量?用 Python 实现涡街信号 FFT 频谱分析与温压全补偿积算网关,深度拆解靠谱的涡街流量计厂家硬核技术标准
开发语言·python
腻害兔2 小时前
【若依项目-产品经理视角】深度拆解 RuoYi-Vue-Pro 商城模块:从商品管理到交易引擎,50 张表撑起一整套电商系统
java·大数据·vue.js·产品经理·ai编程
码智社3 小时前
AES加密原理详解及Java实现加解密实战
java·开发语言
AI云海3 小时前
python 列表、元组、集合和字典
开发语言·python
萧瑟余晖4 小时前
JDK 26 新特性详解
java·开发语言
马优晨5 小时前
Freemarker 完整讲解(后端 Java 模板引擎)
java·开发语言·freemarker·freemarker 完整讲解·freemarker模板引擎
人邮异步社区7 小时前
怎么把C语言学到精通?
c语言·开发语言
心平气和量大福大7 小时前
C#-WPF-控件-TextBox 数据绑定
开发语言·c#·wpf
ttwuai7 小时前
Cursor 生成 CRUD 后,Go 后台接口别只测 200:JWT、RBAC 和 tenant_id 怎么验
开发语言·后端·golang
维天说7 小时前
CLI-Switch 2026年3月版历史设计:Hook、TTY 隔离与 JSON 状态
java·服务器·json