数据类型之Integer与int

数据类型之Integer与int

基本数据类型

众所周知,Java是面向对象的语言,一切皆对象。但是为了兼容人类根深蒂固的数据处理习惯,加快常规数据的处理速度,提供了9种基本数据类型,他们都不具备对象的特性,没有属性和行为。Java的9种基本数据类型包括 boolean、byte、char、short、int、long、double和refvar。前8种数据类型表示生活中的真假、字符、整数和小数,最后一种refvar是面向对象世界中的引用变量,也叫引用句柄。此处认为它也是一种基本数据类型。
前8种都有其对应的包装数据类型,除了char的对应包装类名为 Character, int为 Integer外,其它所有对应的包装类名就是把首字母大写即可。
下面列出了这8种基本数据类型的空间占用大小(bit)及对应的包装类等信息。

  • boolean/1/Boolean
  • byte/8/Byte
  • char/16/Character
  • short/16/Short
  • int/32/Integer
  • float/32/Float
  • long/64/Long
  • double/64/Double

包装类型

前8种基本数据类型都有相应的包装类,因为Java的设计理念是一切皆是对象,在很多情况下,需要以对象的形式操作,比如hashCode()获取哈希值,或者getClass获取类等。包装类的存在解决了基本数据类型无法做到的事情:泛型类型参数、序列化、类型转换、高频区间数据缓存。
重点要说的就是最后一项:缓存池

缓存池

事实上除了Float和Double外,其他包装类型都会缓存。接下来我以介绍Integer的方式来讲解缓存。
先看一段代码

new Integer(123);
Integer.valueOf(123);

new Integer(123) 与 Integer.valueOf(123) 的区别在于:

  • new Integer(123) 每次都会新建一个对象
  • Integer.valueOf(123) 会使用缓存池中的对象,多次调用会取得同一个对象的引用。

valueOf() 方法的源码。

public static Integer valueOf(int i) {if (i >= IntegerCache.low && i <= IntegerCache.high)return IntegerCache.cache[i + (-IntegerCache.low)];return new Integer(i);
}

如上源代码,赋值数据i在缓存区间内直接返回缓存池中的Integer对象,否则就会new一个对象。
因此比较时会出现如下问题:

public class TestIntegerCache {public static void main(String[] args) {Integer i1 = 100;Integer i2 = 100;System.out.println(i1 == i2);//trueInteger i3 = 500;Integer i4 = 500;System.out.println(i3 == i4);//falseSystem.out.println(i3.equals(i4));//true}
}

所以推荐所有包装类型对象之间的比较,全部使用equals方法。

在 Java 8 中,Integer 缓存池的大小默认为 -128~127。

static final int low = -128;
static final int high;
static final Integer cache[];static {// high value may be configured by propertyint h = 127;String integerCacheHighPropValue =sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");if (integerCacheHighPropValue != null) {try {int i = parseInt(integerCacheHighPropValue);i = Math.max(i, 127);// Maximum array size is Integer.MAX_VALUEh = Math.min(i, Integer.MAX_VALUE - (-low) -1);} catch( NumberFormatException nfe) {// If the property cannot be parsed into an int, ignore it.}}high = h;cache = new Integer[(high - low) + 1];int j = low;for(int k = 0; k < cache.length; k++)cache[k] = new Integer(j++);// range [-128, 127] must be interned (JLS7 5.1.7)assert IntegerCache.high >= 127;
}

基本类型对应的缓冲池如下:

  • boolean values true and false
  • all byte values
  • short values between -128 and 127
  • int values between -128 and 127
  • char in the range \u0000 to \u007F

相关参考:
StackOverflow : Differences between new Integer(123), Integer.valueOf(123) and just 123

转载于:https://www.cnblogs.com/jiaweixie/p/9931226.html

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

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

相关文章

PCA(主成分分析)思想及实现

PCA的概念&#xff1a; PCA是用来实现特征提取的。 特征提取的主要目的是为了排除信息量小的特征&#xff0c;减少计算量等。 简单来说&#xff1a; 当数据含有多个特征的时候&#xff0c;选取主要的特征&#xff0c;排除次要特征或者不重要的特征。 比如说&#xff1a;我们要…

【安富莱二代示波器教程】第8章 示波器设计—测量功能

第8章 示波器设计—测量功能 二代示波器测量功能实现比较简单&#xff0c;使用2D函数绘制即可。不过也专门开辟一个章节&#xff0c;为大家做一个简单的说明&#xff0c;方便理解。 8.1 水平测量功能 8.2 垂直测量功能 8.3 总结 8.1 水平测量功能 水平测量方…

深度学习数据更换背景_开始学习数据科学的最佳方法是了解其背景

深度学习数据更换背景数据科学教育 (DATA SCIENCE EDUCATION) 目录 (Table of Contents) The Importance of Context Knowledge 情境知识的重要性 (Optional) Research Supporting Context-Based Learning (可选)研究支持基于上下文的学习 The Context of Data Science 数据科学…

熊猫数据集_用熊猫掌握数据聚合

熊猫数据集Data aggregation is the process of gathering data and expressing it in a summary form. This typically corresponds to summary statistics for numerical and categorical variables in a data set. In this post we will discuss how to aggregate data usin…

IOS CALayer的属性和使用

一、CALayer的常用属性 1、propertyCGPoint position; 图层中心点的位置&#xff0c;类似与UIView的center&#xff1b;用来设置CALayer在父层中的位置&#xff1b;以父层的左上角为原点&#xff08;0&#xff0c;0&#xff09;&#xff1b; 2、 property CGPoint anchorPoint…

GridView详解

快速预览&#xff1a;GridView无代码分页排序GridView选中&#xff0c;编辑&#xff0c;取消&#xff0c;删除GridView正反双向排序GridView和下拉菜单DropDownList结合GridView和CheckBox结合鼠标移到GridView某一行时改变该行的背景色方法一鼠标移到GridView某一行时改变该行…

访问模型参数,初始化模型参数,共享模型参数方法

一. 访问模型参数 对于使用Sequential类构造的神经网络&#xff0c;我们可以通过方括号[]来访问网络的任一层。回忆一下上一节中提到的Sequential类与Block类的继承关系。 对于Sequential实例中含模型参数的层&#xff0c;我们可以通过Block类的params属性来访问该层包含的所有…

QZEZ第一届“饭吉圆”杯程序设计竞赛

终于到了饭吉圆杯的开赛&#xff0c;这是EZ我参与的历史上第一场ACM赛制的题目然而没有罚时 不过题目很好&#xff0c;举办地也很成功&#xff0c;为法老点赞&#xff01;&#xff01;&#xff01; 这次和翰爷&#xff0c;吴骏达 dalao&#xff0c;陈乐扬dalao组的队&#xff0…

谈谈数据分析 caoz_让我们谈谈开放数据…

谈谈数据分析 caozAccording to the International Open Data Charter(1), it defines open data as those digital data that are made available with the technical and legal characteristics necessary so that they can be freely used, reused and redistributed by any…

数据创造价值_展示数据并创造价值

数据创造价值To create the maximum value, urgency, and leverage in a data partnership, you must present the data available for sale or partnership in a clear and comprehensive way. Partnerships are based upon the concept that you are offering value for valu…

Java入门系列-22-IO流

File类的使用 Java程序如何访问文件&#xff1f;通过 java.io.File 类 使用File类需要先创建文件对象 File filenew File(String pathname);&#xff0c;创建时在构造函数中指定物理文件或目录&#xff0c;然后通过文件对象的方法操作文件或目录的属性。 \ 是特殊字符&#xff…

缺了一部分

学Java好多年&#xff0c;也参与一次完整项目&#xff0c;觉得让自己写项目写不出来&#xff0c;总觉得缺了一部分。 在这方面愚笨&#xff0c;不知道缺在哪里。以前觉得是知识不够牢固&#xff0c;于是重复去学&#xff0c;发现就那些东西。如果没有业务来熟悉的话&#xff0c…

卷积神经网络——各种网络的简洁介绍和实现

各种网络模型&#xff1a;来源《动手学深度学习》 一&#xff0c;卷积神经网络&#xff08;LeNet&#xff09; LeNet分为卷积层块和全连接层块两个部分。下面我们分别介绍这两个模块。 卷积层块里的基本单位是卷积层后接最大池化层&#xff1a;卷积层用来识别图像里的空间模…

数据中台是下一代大数据_全栈数据科学:下一代数据科学家群体

数据中台是下一代大数据重点 (Top highlight)Data science has been an eye-catching field for many years now to young individuals having formal education with a bachelors, masters or Ph.D. in computer science, statistics, business analytics, engineering manage…

net如何判断浏览器的类别

回复&#xff1a;.net如何判断浏览器的类别?浏览器型号&#xff1a;Request.Browser.Type 浏览器名称&#xff1a;Request.Browser.browser 浏览器版本&#xff1a;Request.Browser.Version 浏览器Cookie&#xff1a;Request.Browser.Cookies 你的操作系统&#xff1a;Request…

AVS 端能力模块

Mark 转载于:https://www.cnblogs.com/clxye/p/9939333.html

pwn学习之四

本来以为应该能出一两道ctf的pwn了&#xff0c;结果又被sctf打击了一波。 bufoverflow_a 做这题时libc和堆地址都泄露完成了&#xff0c;卡在了unsorted bin attack上&#xff0c;由于delete会清0变量导致无法写&#xff0c;一直没构造出unsorted bin attack&#xff0c;后面根…

优化算法的简洁实现

动量法 思想&#xff1a; 动量法使用了指数加权移动平均的思想。它将过去时间步的梯度做了加权平均&#xff0c;且权重按时间步指数衰减。 代码&#xff1a; 在Gluon中&#xff0c;只需要在Trainer实例中通过momentum来指定动量超参数即可使用动量法。 d2l.train_gluon_ch7…

北方工业大学gpa计算_北方大学联盟仓库的探索性分析

北方工业大学gpa计算This is my firts publication here and i will start simple.这是我的第一篇出版物&#xff0c;这里我将简单介绍 。 I want to make an exploratory data analysis of UFRN’s warehouse and answer some questions about the data using Python and Pow…

泰坦尼克数据集预测分析_探索性数据分析-泰坦尼克号数据集案例研究(第二部分)

泰坦尼克数据集预测分析Data is simply useless until you don’t know what it’s trying to tell you.除非您不知道数据在试图告诉您什么&#xff0c;否则数据将毫无用处。 With this quote we’ll continue on our quest to find the hidden secrets of the Titanic. ‘The …