前言
有关System类的所有函数的使用编程,使得更熟悉函数的使用。
时间
//返回当前时间,毫秒为单位,返回值long
System.out.println("------时间------");
System.out.println(System.currentTimeMillis());
//纳秒
System.out.println(System.nanoTime());
for(int i=0;i<10;i++){
System.out.print(System.currentTimeMillis()+" "+System.nanoTime());
System.out.println();
}
------时间------
1583041477818
147943121309200
1583041477818 147943122023300
1583041477818 147943123026500
1583041477818 147943123656100
1583041477818 147943124248400
1583041477818 147943124867000
1583041477818 147943125454600
1583041477818 147943126044400
1583041477818 147943126631500
1583041477818 147943127218500
1583041477818 147943127811400
输入
//System类的学习
import java.util.Scanner;
//import java.util.String;
public class SystemTest{
public static void main(String[] args){
//返回当前时间,毫秒为单位,返回值long
System.out.println(System.currentTimeMillis());
Scanner ss= new Scanner(System.in);
System.out.println("请输入n");
int n = ss.nextInt();
System.out.println("请输入n个数");
String[] sarray = new String[n];
for(int i=0;i<n;i++){
sarray[i]=ss.next();
}
System.out.println("输出");
for(int i=0;i<n;i++){
System.out.println(sarray[i]);
}
System.out.println("输入任意长度字符");
while(ss.hasNext()){
String s1 = ss.next();
System.out.println(s1);
if(s1.endsWith("<end>"))//终止
break;
}
}
}
1583034036234
请输入n
3
请输入n个数
ghu
h
h
输出
ghu
h
h
输入任意长度字符
weui
weui
jvkl
jvkl
12mvk
12mvk
dffj<end>
dffj<end>
系统属性、环境
System.out.println("------getenv当前系统环境的字符串映射图------");
System.out.println(System.getenv());
System.out.println("------getenv获取环境变量值------");
System.out.println(System.getenv("JAVA_HOME"));
//获取当前系统属性
//System.out.println(System.getProperties());
System.out.println("------getPropertity指定的系统属性------");
System.out.println(System.getProperty("user.name"));
其他
//只是提醒虚拟机:希望进行一次垃圾回收
System.gc();
String s1= new String("huuu");
String s2=new String("huuu");
//字符串对象不同,identityHashCode不同
System.out.println("------对象不同,identityHashCode------");
System.out.println(System.identityHashCode(s1));
System.out.println(System.identityHashCode(s2));
//String重写HashCode,序列相同就相同
System.out.println("------hashCode------");
System.out.println(s1.hashCode());
System.out.println(s2.hashCode());
String s3 = "huu";
String s4 = "huu";
System.out.println("------对象相同,identityHashCode------");
System.out.println(System.identityHashCode(s3));
System.out.println(System.identityHashCode(s4));
System.exit(0);
//不会打印
System.out.print("kkk");
------对象不同,identityHashCode------
1829164700
2018699554
------hashCode------
3214445
3214445
------对象相同,identityHashCode------
1311053135
1311053135