Java学习日志(二): System类,StringBuilder类,包装类

JavaEE学习日志持续更新----> 必看!JavaEE学习路线(文章总汇)
java


今天学习了System类,StringBuilder类和包装类

System类

  • 和Math同样,不能建立对象
  • java.lang.System:系统类
  • System类包含一些和系统相关的方法,是一个工具类,里面的方法都是静态的,可经过类名直接调用.

currentTimeMillis()

以毫秒为单位返回当前时间。web

  • 此方法至关于Date类的getTime方法
  • 可用于测试程序的效率
public class Demo01System {
    public static void main(String[] args) {
        /* * static long currentTimeMillis() 以毫秒为单位返回当前时间。 * 此方法至关于Date类的getTime方法 * */
        long s = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {
            System.out.println(i);
        }
        long e = System.currentTimeMillis();
        //测试程序效率
        System.out.println(e-s);
    }
}

arraycopy​方法

static void arraycopy​(Object src, int srcPos, Object dest, int destPos, int length)编程

  • 将指定源数组中的数组从指定位置开始复制到目标数组的指定位置。

参数数组

  • src - 源数组。
  • srcPos - 源数组中的起始位置。(源数组的起始索引
  • dest - 目标数组。
  • destPos - 目标数据中的起始位置。
  • length - 要复制的数组元素的数量。
public class Demo02System {
    public static void main(String[] args) {
        int[] src = {1,2,3,4,5};
        int[] dest = new int[src.length];
        System.out.println(Arrays.toString(dest));
        //使用arraycopy方法,复制src前三个元素到dest中
        System.arraycopy(src,0,dest,0,3);
        System.out.println(Arrays.toString(dest));
    }
}

StringBuilder类

java.lang.StringBuilder多线程

  • 多线程缓冲区,StringBuffer为单线程缓冲区
  • 字符串缓冲区支持可变的字符串
  • 底层源码:
  • byte[] value = new byte[capacity]; capacity初始容量为默认16
  • 没有被final修饰,值能够被改变

StringBuilder在内存中操做的始终是一个数组,占用内存少,效率高app

StringBuilder()

构造方法:svg

  • StringBuilder() 构造一个字符串构建器,其中不包含任何字符,初始容量为16个字符。
  • StringBuilder​(String str) 构造一个初始化为指定字符串内容的字符串构建器。
public class Demo01 {
    public static void main(String[] args) {
        StringBuilder bu1 = new StringBuilder();
        System.out.println("bu1:"+bu1);//底层数组中没有内容
        StringBuilder bu2 = new StringBuilder("abc");
        System.out.println("bu2:"+bu2);//存储了abc

        /* * 成员方法:int length() 返回长度(字符数)。 * */
        System.out.println(bu1.length());
        System.out.println(bu2.length());
        /* * 成员方法:int capacity() 返回当前容量。 * */
        System.out.println(bu1.capacity());
        System.out.println(bu2.capacity());
    }
}

append(任意类型)

成员方法:工具

  • public StringBuilder append(任意类型):添加任意类型数据的字符串形式,并返回当前对象自身

底层源码:学习

public StringBuilder append(String str) {
        super.append(str);
        return this;
}

返回的是this,返回对象自己!测试

public class Demo02 {
    public static void main(String[] args) {
        StringBuilder bu1 = new StringBuilder();
        //使用append方法
// StringBuilder bu2 = bu1.append("aaa");
// System.out.println(bu1);//aaa
// System.out.println(bu2);//aaa
// System.out.println(bu1 == bu2);//true 比较的是地址值
        bu1.append("abc");
        bu1.append(1);
        bu1.append(true);
        bu1.append('&');
        bu1.append(3.3);
        System.out.println(bu1);

        /*链式编程: * 根据方法的返回值,继续调用方法 * System.out.println("abc".toUpperCase().toLowerCase().toUpperCase().toLowerCase()); * */
        bu1.append("abc").append(100).append(true).append(2.2).append('^');
        System.out.println(bu1);
        /* * 字符串拼接"a"+"b"+"c" * */
        bu1.append("a").append("b").append("c");

    }
}

String和StringBuilder相互转换

在这里插入图片描述
String—>StringBuilder:

  • 使用带参构造:StringBuilder​(String str) 构造一个初始化为指定字符串内容的字符串构建器。

StringBuilder—>String:

  • 使用成员方法:toString();
public class Demo03 {
    public static void main(String[] args) {
        String s = "helloWorld";
        //String--->StringBuilder:
        StringBuilder bu = new StringBuilder(s);
        bu.reverse();//把bu中内容反转
        System.out.println(bu);
        //StringBuilder--->String:
        s = bu.toString();
        System.out.println(s);
    }
}

包装类

java.lang
在这里插入图片描述
基本数据类型:

  • 四类八种:

    byte    short   int     long    float   double  boolean     char
      Byte    Short   Integer Long    Float   Double  Boolean     Character

装箱与拆箱

装箱:把基本类型数据,转化为包装类

  • 1.包装类的构造方法
    Integer​(int value)
    Integer​(String s)
  • 2.包装类的静态方法
    static Integer valueOf​(int i) 返回表示指定的 int值的 Integer实例。
    static Integer valueOf​(String s) 返回一个 Integer物体保持在指定的值 String 。
  • 注意:
    参数:String s 传递字符串的基本类型,传递其余类型会抛出异常
    好比:"1"正确 "a"异常

拆箱:在包装类中,取出基本类型数据

  • int intValue() 返回此值 Integer为 int 。
public class Demo01 {
    public static void main(String[] args) {
        //装箱:把基本类型数据,转化为包装类
        Integer integer1 = new Integer(1);
        System.out.println(integer1);//重写了toString方法
        Integer integer2 = new Integer("1");
        //Integer integer3 = new Integer("a");//异常
        //System.out.println(integer3);
        System.out.println(integer2);

        Integer integer3 = Integer.valueOf(1);
        System.out.println(integer3);
        Integer integer4 = Integer.valueOf("1");
        System.out.println(integer4);
// 拆箱:在包装类中,取出基本类型数据
        int i = integer1.intValue();
        System.out.println(i);
    }
}

自动装箱与自动拆箱

在JDK1.5之后,装箱和拆箱可自动进行,基本数据类型与包装类之间自动转换

public class Demo02 {
    public static void main(String[] args) {
        /* * 自动装箱 * 至关于Integer in = new Integer(1); * */
        Integer in = 1;
        /* * 自动拆箱 * in是包装类,没法直接参与计算,必须先把包装类转化为基本类型再和1相加 * in.intValue()+1=2 * 自动装箱 * in = 2 * */
        in = in + 1;
        ArrayList<Integer> list = new ArrayList<>();
        list.add(1);//自动装箱 list.add(new Integer(1));
        int i = list.get(0);//自动拆箱 int i = list.get(0).intValue();
    }
}

基本数据类型与字符串类型之间转换

基本数据类型–>String类型
1.基本数据类型+"":工做中经常使用
2.包装类中toString方法:

  • static String toString​(int i) 返回表示指定整数的 String对象。

3.String类中的valueOf

  • static String valueOf​(int i) 返回int整数的字符串表示形式。

String类型–>基本数据类型
1.包装类构造方法(不多用)
2.包装类的parseXXX

  • Integer:int parseInt(“字符串的整数”);
  • Double:double parseDouble(“字符串的小数”);
public class Demo03 {
    public static void main(String[] args) {
// 基本数据类型-->String类型
        int a = 10;
        String s1 = a+"";
        System.out.println(s1+100);//10100

        String s2 = Integer.toString(10);
        System.out.println(s2+100);//10100

        String s3 = String.valueOf(a);
        System.out.println(s3+100);//10100

// String类型-->基本数据类型
        int i = Integer.parseInt("10");
        System.out.println(i+100);//110
        boolean aTrue = Boolean.parseBoolean("true");
        System.out.println(aTrue);
    }
}