四.System类
java.lang.System类中提供了大量的静态方法,可以获取与系统相关的信息或系统级操作,在System类的API文档中,常用的方法有:
- public static long currentTimeMillis():返回以毫秒为单位的当前时间。用来验证程序的效率
- public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length):将数组中指定的数据拷贝到另一个数组中。
currentTimeMillis方法
currentTimeMillis方法就是 获取当前系统时间与1970年01月01日00:00点之间的毫秒差值
import java.util.Date;
public class SystemDemo {
public static void main(String[] args) {
//获取当前时间毫秒值
System.out.println(System.currentTimeMillis()); // 1516090531144
}
}
练习
验证for循环打印数字1-9999所需要使用的时间(毫秒)
private static void demo01() {
//程序执行前,获取一次毫秒值
long s = System.currentTimeMillis();
//执行for循环
for (int i = 1; i <=9999 ; i++) {
System.out.println(i);
}
//程序执行后,获取一次毫秒值
long e = System.currentTimeMillis();
System.out.println("程序共耗时:"+(e-s)+"毫秒");//程序共耗时:106毫秒
}
}
arraycopy方法
- public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length):将数组中指定的数据拷贝到另一个数组中。
参数:
参数名称 参数类型 参数含义
1 src Object 源数组
2 srcPos int 源数组索引起始位置
3 dest Object 目标数组
4 destPos int 目标数组索引起始位置
5 length int 复制元素个数
练习
将src数组中前3个元素,复制到dest数组的前3个位置上复制元素前:src数组元素[1,2,3,4,5],dest数组元素[6,7,8,9,10]复制元素后:src数组元素[1,2,3,4,5],dest数组元素[1,2,3,9,10]
package com.itheima.demo05.System;
import java.util.Arrays;
public class Demo01System {
public static void main(String[] args) {
demo02();
StringBuilder sb = new StringBuilder();
}
private static void demo02() {
//定义源数组
int[] src = {1,2,3,4,5};
//定义目标数组
int[] dest = {6,7,8,9,10};
System.out.println("复制前:"+ Arrays.toString(dest));
//使用System类中的arraycopy把源数组的前3个元素复制到目标数组的前3个位置上
System.arraycopy(src,0,dest,0,3);
System.out.println("复制后:"+ Arrays.toString(dest));
}
五.StringBuilder类
字符串拼接问题
由于String类的对象内容不可改变,所以每当进行字符串拼接时,总是会在内存中创建一个新的对象。例如:
public class StringDemo {
public static void main(String[] args) {
String s = "Hello";
s += "World";
System.out.println(s);
}
}
StringBuilder概述
java.lang.StringBuilder类:字符串缓冲区,可以提高字符串的效率
构造方法:
- StringBuilder() 构造一个不带任何字符的字符串生成器,其初始容量为 16 个字符。
- StringBuilder(String str) 构造一个字符串生成器,并初始化为指定的字符串内容。
package com.itheima.demo06StringBuilder;
public class Demo01StringBuilder {
public static void main(String[] args) {
//空参数构造方法
StringBuilder bu1 = new StringBuilder();
System.out.println("bu1:"+bu1); //bu1:""
//带字符串的构造方法
StringBuilder bu2 = new StringBuilder("abc");
System.out.println("bu2:"+bu2); //bu2:abc
}
}
常用方法:
StringBuilder常用的方法有2个:
- public StringBuilder append(…):添加任意类型数据的字符串形式,并返回当前对象自身。
- public String toString():将当前StringBuilder对象转换为String对象。
append方法
append方法具有多种重载形式,可以接收任意类型的参数。任何数据作为参数都会将对应的字符串内容添加到StringBuilder中。例如:
public class Demo02StringBuilder {
public static void main(String[] args) {
//创建对象
StringBuilder builder = new StringBuilder();
//public StringBuilder append(任意类型)
StringBuilder builder2 = builder.append("hello");
//对比一下
System.out.println("builder:"+builder);
System.out.println("builder2:"+builder2);
System.out.println(builder == builder2); //true
// 可以添加 任何类型
builder.append("hello");
builder.append("world");
builder.append(true);
builder.append(100);
// 在我们开发中,会遇到调用一个方法后,返回一个对象的情况。然后使用返回的对象继续调用方法。
// 这种时候,我们就可以把代码现在一起,如append方法一样,代码如下
//链式编程
builder.append("hello").append("world").append(true).append(100);
System.out.println("builder:"+builder);
}
}
备注:StringBuilder已经覆盖重写了Object当中的toString方法。
toString方法
通过toString方法,StringBuilder对象将会转换为不可变的String对象。如:
public class Demo16StringBuilder {
public static void main(String[] args) {
// 链式创建
StringBuilder sb = new StringBuilder("Hello").append("World").append("Java");
// 调用方法
String str = sb.toString();
System.out.println(str); // HelloWorldJava
}
}
六.包装类
- 概述
Java提供了两个类型系统,基本类型与引用类型,使用基本类型在于效率,然而很多情况,会创建对象使用,因为对象可以做更多的功能,如果想要我们的基本类型像对象一样操作,就可以使用基本类型对应的包装类,如下:
基本类型 | 对应的包装类(位于java.lang包中) |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
装箱与拆箱
基本类型与对应的包装类对象之间,来回转换的过程称为”装箱“与”拆箱“:
-
装箱:从基本类型转换为对应的包装类对象。
-
拆箱:从包装类对象转换为对应的基本类型。
用Integer与 int为例:(看懂代码即可)
基本数值---->包装对象
Integer i = new Integer(4);//使用构造函数函数
Integer iii = Integer.valueOf(4);//使用包装类中的valueOf方法
包装对象---->基本数值
int num = i.intValue();
自动装箱与自动拆箱
从Java 5(JDK 1.5)开始,基本类型与包装类的装箱、拆箱动作可以自动完成。例如:
Integer i = 4;//自动装箱。相当于Integer i = Integer.valueOf(4);
i = i + 5;//等号右边:将i对象转成基本数值(自动拆箱) i.intValue() + 5;
//加法运算完成后,再次装箱,把基本数值转成对象。
基本类型与字符串之间的转换
- 基本类型转换为String
基本类型直接与””相连接即可;如:34+""
String转换成对应的基本类型
除了Character类之外,其他所有包装类都具有parseXxx静态方法可以将字符串参数转换为对应的基本类型:
- public static byte parseByte(String s):将字符串参数转换为对应的byte基本类型。
- public static short parseShort(String s):将字符串参数转换为对应的short基本类型。
- public static int parseInt(String s):将字符串参数转换为对应的int基本类型。
- public static long parseLong(String s):将字符串参数转换为对应的long基本类型。
- public static float parseFloat(String s):将字符串参数转换为对应的float基本类型。
- public static double parseDouble(String s):将字符串参数转换为对应的double基本类型。
- public static boolean parseBoolean(String s):将字符串参数转换为对应的boolean基本类型。
public class Demo18WrapperParse {
public static void main(String[] args) {
int num = Integer.parseInt("100");
}
}
注意:如果字符串参数的内容无法正确转换为对应的基本类型,则会显示java.lang.NumberFormatException异常。