当前位置: 首页 > news >正文

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

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
	 *  
	 *  
	 */

}
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),通过缓存复用对象,避免重复创建

相关文章:

  • 车载以太网-SOMEIP
  • python 微博爬虫 01
  • Java学习手册:Java I/O与NIO
  • 【题解-洛谷】P1824 进击的奶牛
  • LangSmith 设置指南
  • 安装树莓派3B+环境
  • 山东大学软件学院创新项目实训开发日志(12)之将对话记录保存到数据库中
  • OSI 七层模型与 TCP/IP 协议栈详解
  • 【JS】关于原型/原型链
  • 【玩泰山派】MISC(杂项)- 使用vscode远程连接泰山派进行开发
  • 0x05.为什么 Redis 设计为单线程?6.0 版本为何引入多线程?
  • 移动端适配
  • javaSE.Lambda表达式
  • 【TI MSPM0】Printf重定向学习
  • GitLab 17.x 配置 https
  • NLP 梳理02 — 标点符号和大小写
  • BUUCTF-web刷题篇(25)
  • 【Ansible自动化运维】五、ansible 的高级特性与扩展:应对复杂场景
  • 互联网三高-数据库高并发之分库分表ShardingJDBC
  • 【CVE-2024-7881】ARM CPU漏洞安全通告
  • 摄影师|伊莎贝尔·穆尼奥斯:沿着身体进行文化溯源
  • 多少Moreless:向世界展示现代中式家具的生活美学
  • 官方通报汕头违建豪宅“英之园”将强拆:对有关人员严肃追责问责
  • 广西壮族自治区党委副书记、自治区政府主席蓝天立接受审查调查
  • 腾讯一季度净利增14%:AI直接拉动广告收入增长,王者荣耀流水创新高
  • “三个集中”之后:图说浦东新区28次撤乡并镇