首页 > 其他分享 >包装类

包装类

时间:2023-06-09 18:04:31浏览次数:24  
标签:MyInteger 包装 valueOf System println Integer out

8种基本数据类型对应类

出现原因:Java为纯面向对象语言(万物皆对象),8种基本数据类型不能new对象。 就破坏了Java为纯面向对象语言的特征,所以Java又为8种基本数据类型 分别匹配了对应的类,这种类叫做包装类/封装类

包装类_System

//手动装箱:基本数据类型 -> 包装类
	    int value = 100;
		Integer integer = Integer.valueOf(value);
		System.out.println(integer);
		
		//手动拆箱:包装类 -> 基本数据类型
		int intValue = integer.intValue();
		System.out.println(intValue);
		
		//JDK1.5新特性:自动拆装箱
	
		//自动装箱:基本数据类型 -> 包装类
		int value = 100;
		Integer integer = value;//底层实现:Integer.valueOf(value);
		System.out.println(integer);
		
		//自动拆箱:包装类 -> 基本数据类型
		int intValue = integer;//底层实现:integer.intValue();
		System.out.println(intValue);
		
		//将字符串转换为int值
		String str = "123";
		int parseInt = Integer.parseInt(str);//相当于自动拆箱
		System.out.println(parseInt);

深入Integer底层原理

public static void main(String[] args) {
		
		Integer integer1 = Integer.valueOf(100);//在-128 - 127之间
		Integer integer2 = Integer.valueOf(100);
		System.out.println(integer1 == integer2);//true
		
		Integer integer3 = Integer.valueOf(200);//不在-128 - 127之间
		Integer integer4 = Integer.valueOf(200);
		System.out.println(integer3 == integer4);//false
		
		System.out.println(integer1);
		System.out.println(integer2);
		System.out.println(integer3);
		System.out.println(integer4);
		
		System.out.println("--------------------");
		
		MyInteger myInteger1 = MyInteger.valueOf(100);
		MyInteger myInteger2 = MyInteger.valueOf(100);
		System.out.println(myInteger1 == myInteger2);//true
		
		MyInteger myInteger3 = MyInteger.valueOf(200);
		MyInteger myInteger4 = MyInteger.valueOf(200);
		System.out.println(myInteger3 == myInteger4);//false
		
		System.out.println(myInteger1);
		System.out.println(myInteger2);
		System.out.println(myInteger3);
		System.out.println(myInteger4);
	}
public class MyInteger {
	
	private int value;

	public MyInteger(int value) {
		this.value = value;
	}
	
	public int intValue(){
		return value;
	}
	
	public static MyInteger valueOf(int i){
		if(i>=MyIntegerCache.low && i<=MyIntegerCache.high){
			return MyIntegerCache.cache[i - MyIntegerCache.low];
		}
		return new MyInteger(i);
	}
	
	@Override
	public String toString() {//重写toString方法
		return String.valueOf(value);
	}
	
	//缓存类
	private static class MyIntegerCache{
		
		private static final int low = -128;
		private static final int high = 127;
		private static final MyInteger[] cache;
		
		static{
			
			cache = new MyInteger[high - low];
			int j = low;
			for (int i = 0; i < cache.length; i++) {
				cache[i] = new MyInteger(j++);
			}
		}
		
	}

标签:MyInteger,包装,valueOf,System,println,Integer,out
From: https://blog.51cto.com/u_16154651/6449676

相关文章

  • java中基本数据类型和包装数据类型
    基本数据类型和包装数据类型在Java中有着重要的区别和联系,对于Java程序员来说,熟悉这两种数据类型的特点和使用方法是非常必要的。 基本数据类型 Java中的基本数据类型一共有8种,分别为:-byte-short-int-long-float-double-char-boolean基本数据类型是指可以......
  • Java开发手册中为什么禁止使用isSuccess作为布尔类型变量名以及POJO中基本类型与包装
    场景Java开发手册中关于POJO的布尔类型的变量名的要求是:【强制】POJO类中的任何布尔类型的变量,都不要加is前缀,否则部分框架解析会引起序列化错误。说明:在本文MySQL规约中的建表约定第一条,表达是与否的变量采用is_xxx的命名方式,所以,需要在<resultMap>设置从is_xxx到......
  • SAP ERP在玻璃包装行业的应用实例
    重庆昊晟玻璃股份有限公司创建于2001年;作为经重庆高新技术产业开发区管理委员会批复,按照现代股份制组建的一家专业从事玻璃瓶、玻璃晶品研发、设计、生产和销售于一体的高新技术企业,是重庆经开区、南岸区重点企业及重点扶持企业。昊晟拥有卓越的生产技术和生产设备设施,产品质量已达......
  • 2023年5月31日,包装类,Match,System
    1.包装类/** *知识点:包装类 *理解:8种基本数据类型对应类 *出现原因:Java为纯面向对象语言(万物皆对象),8种基本数据类型不能new对象。 * 就破坏了Java为纯面向对象语言的特征,所以Java又为8种基本数据类型 * 分别匹配了对应的类,这种类叫做包装类/封装类 * ......
  • 包装类
             ......
  • 当涉及到基本数据类型和包装类时,一些你需要了解、可能容易被忽略的细节。(附面试题)
    基本数据类型Java基本数据按类型可以分为四大类:布尔型、整数型、浮点型、字符型,这四大类包含8种基本数据类型。布尔型:boolean整数型:byte、short、int、long浮点型:float、double字符型:char8种基本类型取值如下:数据类型代表含义默认值取值包装类boolean布尔型false0(false)到1(......
  • java使用阿里云oss上传文件测试案例+上传策略包装类
    产品文档地址:https://help.aliyun.com/product/31815.html产品购买地址:https://www.aliyun.com/search?scene=all&k=oss在官网首先购买产品,开通oss服务后进入控制台:在https://developer.aliyun.com/ask/2061查看相关的endpoint地址(找到自己所在的区域)在控制台https://oss.c......
  • 本程序是三菱FX3U PLC编写的铝材过秤包装平台,主要功能:秤完铝材重量后,根据不同的铝材
    本程序是三菱FX3UPLC编写的铝材过秤包装平台,主要功能:秤完铝材重量后,根据不同的铝材总量,选择不同的包装速度,重量越重,包装速度越慢,包装纸就越重,反之亦然,不同重量可以随意设置不同速度,不同的速度也可以设置不同的重量,当选择段速包装时,一共5段速可以设置,当需要恒速包装时电机频率可......
  • Java的对象包装器 & 自动装箱
    有时,需要将int这样的基本类型转换为对象。所有的基本类型都有一个与之对应的类。例如,Integer类对应基本类型int。通常,这些类被称为包装器(wrapper)。这些对象包装器类拥有很明显的名字:Integer、Long、Float、Double、Short、Byte、Character、Void和Boolean(前6个类派生于公......
  • 基于显扬科技自主研发3D机器视觉HY-M5在易拉罐包装检测的应用
    行业现状:易拉罐包装行业发展迅速,是中国食品工业的重要组成部分。近年来,随着经济水平的提高和生活方式变化,各类预包装食品需求剧增,碳酸饮料和啤酒等饮料消费大幅增加,直接带动易拉罐包装行业高速发展。中国易拉罐行业产业链成熟,行业具有资本密集、技术含量高、环境要求高等特点。易拉......