一、一些基本使用

1.1 toString

这个函数可以将一个数字转化为String类型,同时这个函数还有一个很重要的作用,将某个数字转化为特定进制下的表示

import java.util.*;public class test{public static void main(String[] args) throws InterruptedException {String baseChange=Integer.toString(7,2);System.out.println(baseChange);}}

输出:

111

一、属性

1.1 IntegerCache

/*** Cache to support the object identity semantics of autoboxing for values between* -128 and 127 (inclusive) as required by JLS.** The cache is initialized on first usage.  The size of the cache* may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option.* During VM initialization, java.lang.Integer.IntegerCache.high property* may be set and saved in the private system properties in the* sun.misc.VM class.*/private static class IntegerCache {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;}private IntegerCache() {}}

二、方法

2.1 parseInt

/*** Parses the string argument as a signed decimal integer. The* characters in the string must all be decimal digits, except* that the first character may be an ASCII minus sign {@code '-'}* ({@code '\u005Cu002D'}) to indicate a negative value or an* ASCII plus sign {@code '+'} ({@code '\u005Cu002B'}) to* indicate a positive value. The resulting integer value is* returned, exactly as if the argument and the radix 10 were* given as arguments to the {@link #parseInt(java.lang.String,* int)} method.** @param s    a {@code String} containing the {@code int}*             representation to be parsed* @return     the integer value represented by the argument in decimal.* @exception  NumberFormatException  if the string does not contain a*               parsable integer.*/public static int parseInt(String s) throws NumberFormatException {return parseInt(s,10);}
/*** Parses the string argument as a signed integer in the radix* specified by the second argument. The characters in the string* must all be digits of the specified radix (as determined by* whether {@link java.lang.Character#digit(char, int)} returns a* nonnegative value), except that the first character may be an* ASCII minus sign {@code '-'} ({@code '\u005Cu002D'}) to* indicate a negative value or an ASCII plus sign {@code '+'}* ({@code '\u005Cu002B'}) to indicate a positive value. The* resulting integer value is returned.** <p>An exception of type {@code NumberFormatException} is* thrown if any of the following situations occurs:* <ul>* <li>The first argument is {@code null} or is a string of* length zero.** <li>The radix is either smaller than* {@link java.lang.Character#MIN_RADIX} or* larger than {@link java.lang.Character#MAX_RADIX}.** <li>Any character of the string is not a digit of the specified* radix, except that the first character may be a minus sign* {@code '-'} ({@code '\u005Cu002D'}) or plus sign* {@code '+'} ({@code '\u005Cu002B'}) provided that the* string is longer than length 1.** <li>The value represented by the string is not a value of type* {@code int}.* </ul>** <p>Examples:* <blockquote><pre>* parseInt("0", 10) returns 0* parseInt("473", 10) returns 473* parseInt("+42", 10) returns 42* parseInt("-0", 10) returns 0* parseInt("-FF", 16) returns -255* parseInt("1100110", 2) returns 102* parseInt("2147483647", 10) returns 2147483647* parseInt("-2147483648", 10) returns -2147483648* parseInt("2147483648", 10) throws a NumberFormatException* parseInt("99", 8) throws a NumberFormatException* parseInt("Kona", 10) throws a NumberFormatException* parseInt("Kona", 27) returns 411787* </pre></blockquote>** @param      s   the {@code String} containing the integer*                  representation to be parsed* @param      radix   the radix to be used while parsing {@code s}.* @return     the integer represented by the string argument in the*             specified radix.* @exception  NumberFormatException if the {@code String}*             does not contain a parsable {@code int}.*/public static int parseInt(String s, int radix)throws NumberFormatException{/** WARNING: This method may be invoked early during VM initialization* before IntegerCache is initialized. Care must be taken to not use* the valueOf method.*/if (s == null) {throw new NumberFormatException("null");}if (radix < Character.MIN_RADIX) {throw new NumberFormatException("radix " + radix +" less than Character.MIN_RADIX");}if (radix > Character.MAX_RADIX) {throw new NumberFormatException("radix " + radix +" greater than Character.MAX_RADIX");}int result = 0;boolean negative = false;int i = 0, len = s.length();int limit = -Integer.MAX_VALUE;int multmin;int digit;if (len > 0) {char firstChar = s.charAt(0);if (firstChar < '0') { // Possible leading "+" or "-"if (firstChar == '-') {negative = true;limit = Integer.MIN_VALUE;} else if (firstChar != '+')throw NumberFormatException.forInputString(s);if (len == 1) // Cannot have lone "+" or "-"throw NumberFormatException.forInputString(s);i++;}multmin = limit / radix;while (i < len) {// Accumulating negatively avoids surprises near MAX_VALUEdigit = Character.digit(s.charAt(i++),radix);if (digit < 0) {throw NumberFormatException.forInputString(s);}if (result < multmin) {throw NumberFormatException.forInputString(s);}result *= radix;if (result < limit + digit) {throw NumberFormatException.forInputString(s);}result -= digit;}} else {throw NumberFormatException.forInputString(s);}return negative ? result : -result;}

2.2 valueOf

/*** Returns an {@code Integer} object holding the* value of the specified {@code String}. The argument is* interpreted as representing a signed decimal integer, exactly* as if the argument were given to the {@link* #parseInt(java.lang.String)} method. The result is an* {@code Integer} object that represents the integer value* specified by the string.** <p>In other words, this method returns an {@code Integer}* object equal to the value of:** <blockquote>*  {@code new Integer(Integer.parseInt(s))}* </blockquote>** @param      s   the string to be parsed.* @return     an {@code Integer} object holding the value*             represented by the string argument.* @exception  NumberFormatException  if the string cannot be parsed*             as an integer.*/public static Integer valueOf(String s) throws NumberFormatException {return Integer.valueOf(parseInt(s, 10));}
/*** Returns an {@code Integer} object holding the value* extracted from the specified {@code String} when parsed* with the radix given by the second argument. The first argument* is interpreted as representing a signed integer in the radix* specified by the second argument, exactly as if the arguments* were given to the {@link #parseInt(java.lang.String, int)}* method. The result is an {@code Integer} object that* represents the integer value specified by the string.** <p>In other words, this method returns an {@code Integer}* object equal to the value of:** <blockquote>*  {@code new Integer(Integer.parseInt(s, radix))}* </blockquote>** @param      s   the string to be parsed.* @param      radix the radix to be used in interpreting {@code s}* @return     an {@code Integer} object holding the value*             represented by the string argument in the specified*             radix.* @exception NumberFormatException if the {@code String}*            does not contain a parsable {@code int}.*/public static Integer valueOf(String s, int radix) throws NumberFormatException {return Integer.valueOf(parseInt(s,radix));}
/*** Returns an {@code Integer} instance representing the specified* {@code int} value.  If a new {@code Integer} instance is not* required, this method should generally be used in preference to* the constructor {@link #Integer(int)}, as this method is likely* to yield significantly better space and time performance by* caching frequently requested values.** This method will always cache values in the range -128 to 127,* inclusive, and may cache other values outside of this range.** @param  i an {@code int} value.* @return an {@code Integer} instance representing {@code i}.* @since  1.5*/public static Integer valueOf(int i) {if (i >= IntegerCache.low && i <= IntegerCache.high)return IntegerCache.cache[i + (-IntegerCache.low)];return new Integer(i);}

jdk Integer 具体实现相关推荐

  1. 图解Java设计模式学习笔记——结构型模式(适配器模式、桥接模式、装饰者模式、组合模式、外观模式、享元模式、代理模式)

    一.适配器模式(类适配器.对象适配器.接口适配器) 1.现实生活中的例子 泰国插座用的是两孔的(欧标),可以买个多功能转换插头(适配器),这样就可以使用了国内的电器了. 2.基本介绍 适配器模式(Ad ...

  2. Java后端架构师的成长之路(二)——Java设计模式(2)

    Java设计模式 23种设计模式 结构型模式 适配器模式 基本介绍 适配器模式工作原理 类适配器模式 对象适配器模式 接口适配器模式 适配器模式在SpringMVC框架应用的源码分析 适配器模式的注意 ...

  3. java int类源码,一起学JDK源码 -- Integer类

    Integer类为java基本类型int的包装类,除了前面提到的Byte类,Short类中的大部分方法,Integer类中还提供了很多处理int类型的方法,接下来就让我们一起看看吧. 基础知识: 1. ...

  4. JDK源码学习笔记——Integer

    一.类定义 public final class Integer extends Number implements Comparable<Integer> 二.属性 private fi ...

  5. java jdk缓存-128~127的Long与Integer

    先推断下以下代码的输出结果 Qa:---------------------------------------------- Long a = Long.valueOf(127);          ...

  6. JDK源码解析 Integer类使用了享元模式

    JDK源码解析 Integer类使用了享元模式. 我们先看下面的例子: public class Demo {public static void main(String[] args) {Integ ...

  7. integer为null_走进 JDK 之 Integer

    文中相关源码: Integer.java 开发的越久,越能体会到基础知识的重要性.抽空捋一下 JDK 源码,权当查漏补缺.读完之后,你会发现 JDK 源码真的会给你很多惊喜. Integer 是基本类 ...

  8. JDK 源码 Integer解读之一(toString)

    1:首先我们可以通过一个main函数,通过断点调试,正式开启Integer.toString()方法之旅. public static void main ( String[] arg ) { Sys ...

  9. JDK源码分析-Integer

    Integer是平时开发中最常用的类之一,但是如果没有研究过源码很多特性和坑可能就不知道,下面深入源码来分析一下Integer的设计和实现. Integer: 继承结构: -java.lang.Obj ...

  10. JDK源码解析之 java.lang.Integer

    teger 基本数据类型int 的包装类 Integer 类型的对象包含一个 int 类型的字段 一.类定义 public final class Integer extends Number imp ...

最新文章

  1. 自监督媲美全监督,港中文、商汤场景去遮挡方法入选 CVPR 2020 Oral
  2. MPEG简介 + 如何计算CBR 和VBR的MP3的播放时间
  3. Xamarin.iOS编译出错
  4. HDU4292-Food-网络流
  5. mysql -u 报错_MySQL报错解决!
  6. 前端工作面试问题(下)
  7. 【BZOJ4262】Sum 单调栈+线段树
  8. html5-样式表的使用-初步
  9. Transact-SQL管理与开发实例精粹
  10. ScreenFlow for mac 录制视频的工具
  11. 请熟悉ECO开发的朋友解答我的一些小问题!
  12. [环境搭建]Windows下安装Ruby和Jekyll
  13. HDU 4630 No Pain No Game (线段树+离线)
  14. 2021年5月软考网络工程师上午真题(带答案解析)上
  15. mysql数据对比_MySQL--如何快速对比数据
  16. vb用计算机解决鸡兔同笼,vb解决鸡兔同笼问题
  17. 安卓真机如何连接本地服务器_Android 真机连接本地PC服务器
  18. 海贼王 动漫 全集目录 分章节 精彩打斗剧集
  19. unity 裙子摆动_随风摆动的草丛——Unity shader graph 2D初探
  20. 为什么不想做产品经理

热门文章

  1. Android O后台服务限制总结
  2. jersery集成jackson实现restful api,由于jdk版本不一致导致的坑
  3. python覆盖数据库重复数据_如何在数据库中插入唯一数据/处理重复数据
  4. 几个优质的技术公号,值得关注
  5. 百度App网络深度优化系列《一》DNS优化
  6. (已拿offer)2017腾讯暑期实习生从笔试到面试总结(附带华为、阿里面试经历)...
  7. cryptapi双向认证_2019 08 28 netty案例,netty4.1中级拓展篇十三《Netty基于SSL实现信息传输过程中双向加密验证》...
  8. 基于fpga的dds函数信号发生器的设计_低频信号发生器
  9. json中保存数据与解析数据与python中json模块做对应关系
  10. delphi和python和halcon_【《zw版·Halcon与delphi系列原创教程》Halcon图层与常用绘图函数...