【java基础】Java常用类———包装类

news2024/9/23 11:19:43

包装类

wrapper

装箱与拆箱

  • 装箱:基本类型->包装类; 拆箱: 包装类->基本类型
 
 	
 	public class Integer01 {
 	    public static void main(String[] args) {
 	        //演示int <--> Integer 的装箱和拆箱
 	        //jdk5前是手动装箱和拆箱
 	        //手动装箱 int->Integer
 	        int n1 = 100;
 	        Integer integer = new Integer(n1);
 	        Integer integer1 = Integer.valueOf(n1);
 	        //手动拆箱
 	        //Integer -> int
 	        int i = integer.intValue();
 	        //jdk5后,就可以自动装箱和自动拆箱
 	        int n2 = 200;
 	        //自动装箱 int->Integer
 	        Integer integer2 = n2; //底层使用的是 Integer.valueOf(n2)
 	        //自动拆箱 Integer->int
 	        int n3 = integer2; //底层仍然使用的是 intValue()方法
 	    }
 	}
 

如果频繁拆装箱的话,也会严重影响系统的性能。我们应该尽量避免不必要的拆装箱操作。

8种包装类

针对8种基本数据类型相应的引用类型——包装类

基本数据类型包装类
byteByte
shortShort
intInteger
longLong
charCharacter
floatFloat
doubleDouble
booleanBoolean

所有包装类都是抽象类Number的子类

缓存机制

Java 基本数据类型的包装类型的大部分都用到了缓存机制来提升性能。

Byte,Short,Integer,Long 这 4 种包装类默认创建了数值 [-128,127] 的相应类型的缓存数据,

只有自动装箱的才有缓存

Character 创建了数值在 [0,127] 范围的缓存数据,
Boolean 直接返回 True or False
两种浮点数类型的包装类 Float,Double 并没有实现缓存机制。

Integer a1=new Integer(1);  
Integer a2=new Integer(1);  
System.out.println(a1==a2);//false  
  
Integer b1=2;  
Integer b2=2;  
System.out.println(b1==b2);//true,在[-128,127]之间的自动装箱valueOf使用了缓冲机制,不会创建新的对象  
  
Integer c1=200;  
Integer c2=200;  
System.out.println(c1==c2);//false  
//因此,包装类对象的比较要用equals
System.out.println(c1.equals(c2));//true
  
Boolean d1=true;  
Boolean d2=true;  
System.out.println(d1==d2);//true

Integer 缓存源码:

public static Integer valueOf(int i) {
    if (i >= IntegerCache.low && i <= IntegerCache.high)
        return IntegerCache.cache[i + (-IntegerCache.low)];
    return new Integer(i);
}
private static class IntegerCache {
    static final int low = -128;
    static final int high;
    static {
        // high value may be configured by property
        int h = 127;
    }
}

Character 缓存源码:

public static Character valueOf(char c) {
    if (c <= 127) { // must cache
      return CharacterCache.cache[(int)c];
    }
    return new Character(c);
}

private static class CharacterCache {
    private CharacterCache(){}
    static final Character cache[] = new Character[127 + 1];
    static {
        for (int i = 0; i < cache.length; i++)
            cache[i] = new Character((char)i);
    }

}

如果超出对应范围仍然会去创建新的对象,缓存的范围区间的大小只是在性能和资源之间的权衡。

常用方法

在这里插入图片描述

  1. Number子类实现的方法
MethodDescription
byte byteValue() , short shortValue(), int intValue() , long longValue(), float floatValue(), double doubleValue()Converts the value of this Number object to the primitive data type returned.
把包装类对象转换成基本数据类型
int compareTo(Byte anotherByte) , int compareTo(Double anotherDouble), int compareTo(Float anotherFloat), int compareTo(Integer anotherInteger), int compareTo(Long anotherLong), int compareTo(Short anotherShort)Compares this Number object to the argument.
比较函数
boolean equals(Object obj)Determines whether this number object is equal to the argument. The methods return true if the argument is not null and is an object of the same type and with the same numeric value. There are some extra requirements for Double and Float objects that are described in the Java API documentation.
是否相等

Integer的方法

MethodDescription
static Integer decode(String s)Decodes a string into an integer. Can accept string representations of decimal, octal, or hexadecimal numbers as input.
static int parseInt(String s)Returns an integer (decimal only).
static int parseInt(String s, int radix)Returns an integer, given a string representation of decimal, binary, octal, or hexadecimal (radix equals 10, 2, 8, or 16 respectively) numbers as input.
String toString()Returns a String object representing the value of this Integer.
static String toString(int i)Returns a String object representing the specified integer.
static Integer valueOf(int i)Returns an Integer object holding the value of the specified primitive.
static Integer valueOf(String s)Returns an Integer object holding the value of the specified string representation.
static Integer valueOf(String s, int radix)Returns an Integer object holding the integer value of the specified string representation, parsed with the value of radix. For example, if s = “333” and radix = 8, the method returns the base-ten integer equivalent of the octal number 333.

eg

public class WrapperMethod {
    public static void main(String[] args) {
        System.out.println(Integer.MIN_VALUE); //返回最小值
        System.out.println(Integer.MAX_VALUE);//返回最大值
        System.out.println(Character.isDigit('a'));//判断是不是数字
        System.out.println(Character.isLetter('a'));//判断是不是字母
        System.out.println(Character.isUpperCase('a'));//判断是不是大写
        System.out.println(Character.isLowerCase('a'));//判断是不是小写
        System.out.println(Character.isWhitespace('a'));//判断是不是空格
        System.out.println(Character.toUpperCase('a'));//转成大写
        System.out.println(Character.toLowerCase('A'));//转成小写
    }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/357067.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

学海记录项目测试报告

⭐️前言⭐️ 本篇文章是博主基于学海记录的个人项目所做的测试报告&#xff0c;用于总结运用自动化测试技术&#xff0c;应用于自己的项目。 &#x1f349;欢迎点赞 &#x1f44d; 收藏 ⭐留言评论 &#x1f4dd;私信必回哟&#x1f601; &#x1f349;博主将持续更新学习记录…

【查找算法】解析学习四大常用的计算机查找算法 | C++

第二十二章 四大查找算法 目录 第二十二章 四大查找算法 ●前言 ●查找算法 ●一、顺序查找法 1.什么是顺序查找法&#xff1f; 2.案例实现 ●二、二分查找法 1.什么是二分查找法&#xff1f; 2.案例实现 ●三、插值查找法 1.什么是插值查找法&#xff1f; 2…

数据结构:基数排序

基数排序(radix sorting) 实现排序主要是通过关键字之间的比较和移动记录这两种操作来完成的,而实现基数排序不需要进行关键字间的比较,而是利用“分配”和“收集”两种基本操作。 例如,我们可以用分配和收集的方法来对扑克牌进行“排序” 已知扑克牌中 52 张牌面的次序关系为…

Time-distributed 的理解

前言 今天看到论文中用到 Time-distributed CNN&#xff0c;第一次见到 Time-distributed&#xff0c;不理解是什么含义&#xff0c;看到代码实现也很懵。不管什么网络结构&#xff0c;外面都能套一个TimeDistributed。看了几个博客&#xff0c;还是不明白&#xff0c;问了问C…

Python数据挖掘基础

一、Matplotlib 画二维图表的python库&#xff0c;实现数据可视化 &#xff0c; 帮助理解数据&#xff0c;方便选择更合适的分析方法1、折线图1.1引入matplotlibimport matplotlib.pyplot as plt %matplotlib inlineplt.figure() plt.plot([1, 0, 9], [4, 5, 6]) plt.show()1.2…

知识探索项目测试报告

⭐️前言⭐️ 本篇文章是博主基于知识探索项目所做的测试报告&#xff0c;主要涉及到的测试知识有设计测试用例、自动化测试等测试知识。 &#x1f349;欢迎点赞 &#x1f44d; 收藏 ⭐留言评论 &#x1f4dd;私信必回哟&#x1f601; &#x1f349;博主将持续更新学习记录收获…

基于springboot+vue的药物咨询平台

基于springbootvue的药物咨询平台 ✌全网粉丝20W,csdn特邀作者、博客专家、CSDN新星计划导师、java领域优质创作者,博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域和毕业项目实战✌ &#x1f345;文末获取项目下载方式&#x1f345; 一、项目背景介绍&…

二阶段提交事务的实现和缺点

背景 说起分布式事务&#xff0c;我们最绕不开的一个话题就是该不该使用分布式事务&#xff0c;而要理解为什么做出使用与否的决定&#xff0c;就必须要提到分布式事务中的最经典的实现&#xff1a;两阶段提交事务,本文我们就简答介绍下这个两阶段提交事务以及它的优缺点 技术…

【Opencv 系列】 第6章 人脸检测(Haar/dlib) 关键点检测

本章内容 1.人脸检测&#xff0c;分别用Haar 和 dlib 目标&#xff1a;确定图片中人脸的位置&#xff0c;并画出矩形框 Haar Cascade 哈尔级联 核心原理 &#xff08;1&#xff09;使用Haar-like特征做检测 &#xff08;2&#xff09;Integral Image : 积分图加速特征计算 …

SpringSecurity的权限校验详解说明(附完整代码)

说明 SpringSecurity的权限校是基于SpringSecurity的安全认证的详解说明(附完整代码) &#xff08;https://blog.csdn.net/qq_51076413/article/details/129102660&#xff09;的讲解&#xff0c;如果不了解SpringSecurity是怎么认证&#xff0c;请先看下【SpringSecurity的安…

【1792. 最大平均通过率】

来源&#xff1a;力扣&#xff08;LeetCode&#xff09; 描述&#xff1a; 一所学校里有一些班级&#xff0c;每个班级里有一些学生&#xff0c;现在每个班都会进行一场期末考试。给你一个二维数组 classes &#xff0c;其中 classes[i] [passi, totali] &#xff0c;表示你…

0xL4ugh 2023

这回跟着个队伍跑&#xff0c;不过还是2X以后的成绩&#xff0c;前边太卷了。自己会的部分&#xff0c;有些是别人已经提交了的。记录一下。Cryptocrypto 1给了一些数据&#xff0c;像这样就没有别的了ct [0, 1, 1, 2, 5, 10, 20, 40, 79, 159, 317, 635, 1269, 2538, 5077, 1…

2023.02.19 学习周报

文章目录摘要文献阅读1.题目2.摘要3.介绍4.本文贡献5.方法5.1 Local Representation Learning5.2 Global Representation Learning5.3 Item Similarity Gating6.实验6.1 数据集6.2 结果7.结论深度学习1.对偶问题1.1 拉格朗日乘数法1.2 强对偶性2.SVM优化3.软间隔3.1 解决问题3.…

尚医通 (十八)微信登录

目录一、生成微信登录二维码1、准备工作2、后端开发service_user3、前端显示登录二维码4、二维码出现不了进行调试二、开发微信扫描回调1、准备工作2、后台开发3、前台开发三、分析代码四、bug一、生成微信登录二维码 1、准备工作 1、注册 2、邮箱激活 3、完善开发者资料 4、…

JSP中http与内置对象学习笔记

本博文讲述jsp客户端与服务器端的http、jsp内置对象与控制流和数据流实现 1.HTTP请求响应机制 HTTP协议是TCP/IP协议中的一个应用层协议&#xff0c;用于定义客户端与服务器之间交换数据的过程 1.1 HTTP请求 HTTP请求由请求行、消息报头、空行和请求数据4部分组成。 请求行…

ThreeJS 之界面控制

文章目录参考描述界面自适应问题resize 事件修改画布大小修改视锥体的宽高比全屏显示dblclick 事件检测全屏显示状态进入全屏显示状态退出全屏显示状态尾声参考 项目描述ThreeJS官方文档哔哩哔哩老陈打码搜索引擎BingMDN 文档document.mozFullScreenElementMDN 文档Element.re…

LeetCode题目笔记——6359. 替换一个数字后的最大差值

文章目录题目描述题目链接题目难度——简单方法一&#xff1a;替换代码/Python代码优化总结题目描述 给你一个整数 num 。你知道 Danny Mittal 会偷偷将 0 到 9 中的一个数字 替换 成另一个数字。 请你返回将 num 中 恰好一个 数字进行替换后&#xff0c;得到的最大值和最小值…

CTK学习:(一)编译CTK

CTK插件框架简介 CTK Plugin Framework是用于C++的动态组件系统,以OSGi规范为模型。在此框架下,应用程序由不同的组件组成,遵循面向服务的方法。 ctk是一个开源项目,Github 地址:https://github.com/commontk。 源码地址commontk/CTK: A set of common support code for…

信小程序点击按钮绘制定制转发分享图

1. 说明 先上代码片断分享链接&#xff1a; https://developers.weixin.qq.com/s/vl3ws9mA72GG 使用 painter 画图 按钮传递定制化信息 效果如下&#xff1a; 2. 关键代码说明 文件列表如下&#xff1a; {"usingComponents": {"painter": "/com…

基于springboot的停车场管理系统(程序+文档)

大家好✌&#xff01;我是CZ淡陌。将再这里为大家分享优质的实战项目&#xff0c;本人在Java毕业设计领域有多年的经验&#xff0c;陆续会更新更多优质的Java实战项目&#xff0c;希望你能有所收获&#xff0c;少走一些弯路。 &#x1f345;更多优质项目&#x1f447;&#x1f…