目 录CONTENT

文章目录

java常用类-包装类,string类

Rain
2020-05-29 / 0 评论 / 0 点赞 / 386 阅读 / 3,508 字 / 正在检测是否收录...

java常用类

包装类

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

自动装箱和拆箱

自动装箱:基本类型数据处于需要对象的环境中时,会自动转为“对象”

自动拆箱:当需要一个值时,对象会自动转为基本数据类型

// 测试自动装箱 自动拆箱
public class Demo01 {
    public static void main(String[] args) {
        // 自动装箱
        Integer a = 231;
        // 编译器自动按照该语法编译:Integer a = Integer.valueOf(231);

        // 自动拆箱
        // b -- int, a -- object
        int b = a;
        // int b = a.intValue();

        Integer c = null;
        int d = c; // 隐藏了调用c的intValue()的方法
        // java.lang.NullPointerException
        // 空指针,调用空对象的方法
    }
}

缓存问题

缓存[-128,127]之间的数字
系统初始的时候创建了[-128,127]之间的一个缓存数组
当我们调用valueOf的时候,首先检查是否在[-128,127]之间,如果在这个范围,则直接从缓存中拿出已经建好的对象,如果不在范围内,则创建新的integer对象

public class Demo02 {
    public static void main(String[] args) {
        Integer in3 = Integer.valueOf(-128);
        Integer in4 = -128;
        System.out.println(in3 == in4); // true
        // .equals 比较的是值
        System.out.println(in3.equals(in4)); // true
        int a = 1;
        int b = 1;
        System.out.println(a == b); // true
    }
}

String 类

声明

public class Demo00 {
    //  String 类声明
    String str1 = "hello";
    String str2 = new String(new char[]{'1','2'});
    String str3 = new String("hello");
}

常用方法

length()

// length()
        System.out.println(str.length());
        for (int i = 0; i < str.length(); i++) {
            System.out.println(str.charAt(i));
        }

concat()

// concat() 拼接字符串 st1.concat(str2);
        // + 当字符串参与运算时(有一边为字符串),为拼接作用
        // + 两边都不是字符串时,做加法运算
        String str1 = " nice to meet you";
        String str2 = str.concat(str1);
        String str3 = str + str1;
        System.out.println(str2); // my name is zzp nice to meet you
        System.out.println(str3); //my name is zzp nice to meet you
        String str4 = 1+2+"123";
        System.out.println(str4); // 3123
        String str5 = "123" + 1 + 2;
        System.out.println(str5); // 12312

格式化字符串

注意是printf

String str = String.format(“hello %s,my name is %s”," world"," zzp")

charAt()

返回索引位置的字符

for (int i = 0; i < str.length(); i++) {
            System.out.println(str.charAt(i));
        }

compareTo(String str)

调用这个方法的对象与参数值进行比较(ascii)

返回值是int类型

当调用这个方法的对象大于参数值,返回值大于0

当调用这个方法的对象等于参数值,返回值等于0

当调用这个方法的对象小于参数值,返回值小于0

public class Demo02 {
    public static void main(String[] args) {
        int result = "abc".compareTo("bac");
        System.out.println(result); // -1
    }
}

copyValueOf(char[] data)

静态方法,把一个字符数组转换为String类型并进行返回

// copyValueOf(char[] data)
        String str = "";
        str = str.copyValueOf(new char[]{'a', 'b', 'c'});
        System.out.println(str); //abc

endsWith()

比较调用这个方法的对象的内容是否和参数对象的内容一样,比较的是对象,如果两个对象是用不同的new产生的,则不同

string类型重写了该方法,只要值一样,则返回true

      // equals()
        String s1 = "new";
        String s2 = "new";
        System.out.println(s1.equals(s2)); // true
        System.out.println(s1==s2); //true

getChar()

语法

public void getChars(int srcBegin, int srcEnd, char[] dst,  int dstBegin)

参数

srcBegin – 字符串中要复制的第一个字符的索引。
srcEnd – 字符串中要复制的最后一个字符之后的索引。
dst – 目标数组。
dstBegin – 目标数组中的起始偏移量。

  // getChar()
        String str1 = new String("my name is zzp");
        char[] str2 = new char[6];
        str1.getChars(4,10,str2,0);
        System.out.println(str2); // ame is
0

评论区