ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

常用类

2022-07-07 21:00:34  阅读:108  来源: 互联网

标签:常用 String int 包装 NumberFormatException 字符串 Integer


javaEE-2206-何川 20:52:52

包装类

Integer

Integer num1 = 100;
Integer num2 = 100; //true
num1num2;
Integer num1 = 1000;
Integer num2 = 1000; //false
num1
num2;
Integer num1 = Integer.valueOf(100) ;
Integer.paeseInt();
String
不可变性,串池不重复
StringBuffer 一个对象,改变值
trim() 去前后空格 eg.取rgb颜色自动加的空格
day14-常见类1

2、包装类

Integer:

反编译:

Integer num = Integer.valueOf(100);
Integer num1 = Integer.valueOf(100);
System.out.println(num == num1);
追踪到valueOf():

public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];//new Integer()
        return new Integer(i);
}
追踪到IntegerCache:
 
private static class IntegerCache {
        static final int low = -128;
        static final int high;
        static final Integer cache[];
        static {
            // high value may be configured by property
            int 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);//取i和127的最大值赋给i
                    // Maximum array size is Integer.MAX_VALUE
                    h = 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() {}
 }

如何设置上限:

image

int num = Integer.parseInt(str);
"123"---['1','2','3']--[1,2,3]---123

public static int parseInt(String s) throws NumberFormatException {
        return parseInt(s,10);
}

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.
         */
    //判断字符串是否为空--null
        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;//positive
        int i = 0, len = s.length();//字符串的长度
        int limit = -Integer.MAX_VALUE;
        int multmin;//算结果
        int digit;//位

        if (len > 0) {//判断""
            char firstChar = s.charAt(0);//第一个字符'1'
            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) {//i可能从0或1开始  +999   8888
                // Accumulating negatively avoids surprises near MAX_VALUE
                digit = 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;
}

3、String

// abcdef bcdefa cdefab defabc efabcd fabcde

// abcdefabcdef

补充方法:

toCharArray():转为字符数组

split():打断字符串----字符串数组

常用类-上
一、基本数据类型包装类
Java是一种纯面向对象语言,但是java中有8种基本数据类型,破坏了java为纯面向对象的特征。为了承诺在java中一切皆对象,java又给每种基本数据类型分别匹配了一个类,这个类我们称之为包装类/封装类。
注意:每个基本数据类型都有一个与之匹配的包装类。
1.1八大基本数据类型的包装类

注意:int的包装类的写法为Integer、char的包装类的写法是:Character
其余基本数据类型的包装类均是基本类型的首字母大写。
1.2包装类的层次结构

1.3包装类中的常用方法
装箱:把基本数据类型包装为对应的包装类对象 10
Integer i1 = new Integer(10); // 利用构造方法
Integer i2 = Integer.valueOf(10); //利用包装类中的静态方法
拆箱:把包装类对象转换为对应的基本数据类型。
int i3= i1.intValue(); //返回包装类对象i1对应的基本数据

1.4自动装箱和自动拆箱
前面的装箱和拆箱操作,相对较麻烦。自jdk1.5开始,java增加的对基本数据类型的自动装箱和自动拆箱操作。
java编译器在编译时期会根据源代码的语法来决定是否进行装箱或拆箱。
1、自动装箱:可以直接把一个基本数据类型赋值给包装类
例如: Integer i1 = 10; //自动装箱操作
2、自动拆箱:可以直接把一个包装类对象,赋值给基本类型
例如:int a = new Integer(3); //自动拆箱。
自动装箱和自动拆箱,简化了对包装类的操作。
注意:需要理解自动装箱和拆箱的本质
1.5必须get的技能
将字符串转换为整数
练习:将一个字符串如”1,2,3,4,5,6”转换成一个整型数组信息列表

二、字符串相关类
1.String类
1.1 什么是String
String是不可变类, 即一旦一个String对象被创建, 包含在这个对象中的字符序列是不可改变的, 直至该对象被销毁。
String类是final类,不能有子类。
1.2 创建字符串对象
常量池概念:
Java运行时会维护一个常量池, 常量池用来存放运行时中产生的各种字符串,并且池中的字符串的内容不重复。

1.3 String类型常用方法
1.3.1 获得新字符串的常用方法(拼接+替换)
1)String concat(String str) 在原有字符串的尾部添加参数字符串,返回一个新的字符串(总是堆内存中的对象),如果str的长度为0,则返回原字符串。str不能为空。
2)String subString(int beginIndex) 获得从beginIndex开始到结束的子字符串。( 包括beginIndex位置的字符)
3)public String toLowerCase() 把字符串中的英文字符全部转换为小写字符,返回值为装换后的新的字符串。
4)public String toUpperCase() 把字符串中的英文字符全部转换为大写字符,返回值为装换后的新的字符串。
5)public String trim() 把字符串中的首尾空白字符去掉
6)public String replace(CharSequence target, CharSequence replacement) 使用指定的字面值替换序列替换此字符串所有匹配字面值目标序列的子字符串
7)public String replace(char oldChar, char newChar) 返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。
8)public String replaceAll(String regex, String replacement) 使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。
9)public replaceFirst(String regex

标签:常用,String,int,包装,NumberFormatException,字符串,Integer
来源: https://www.cnblogs.com/liang5683/p/16456101.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有