java System类学习笔记


java 中 System 类

最常见到 System.out.println();

System类 定义为 public final class System extends Object{} 包含几个有用的类字段和方法,用了关键字 final 修饰,表示此类不能被其他类继承。

其构造方法为 private System{} (构造方法私有化,不能被外部实例化)。 

System 中有三个属性:in,out,err;

1.private final static InputStream in=null;

2.private final static PrintStream out=null;  

           -->  out 为类PrintStream 类型 println()就是PrintStream中的方法。out被 final 和static 修饰的常量,故可以用System调用out属性,再调用println()方法。

3.private final static PrintStream err=null;

System 中的几个方法:

System 中的方法全部用 static 修饰,可以用类名称直接调用,例如 System.getProperties();

1,public static Properties getProperties(){}      System.getProperties().list(System.out);  -->确定当前系统属性。

2,public static String getProperties(String key){}     System.getProperty(String key);  --> 获取当前键指定的系统属性。

3,public static long currentTimeMillis(){}       System.currentTimeMillis();  --> 返回当前时间(以毫秒为单位)。

public class S{
    public static void main(String[] args){ long startTime=System.currentTimeMillis(); int a=0; for(int i=0;i<1000000000;i++){ a+=i; } long endTime=System.currentTimeMillis(); System.out.println("执行此程序用了"+(endTime-startTime)+"毫秒。"); } }

------------>      输出为:  执行此程序用了380毫秒。

4,public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length){}    将指定源数组中的数组从指定位置复制到目标数组的指定位置。

public class S{
    public static void main(String[] args){ int[] a=new int[]{1,2,3,10,20,30,4,5,6}; int[] b=new int[3]; System.arraycopy(a,3,b,0,3); for(int i=0;i<b.length;i++){ System.out.print(b[i]+" "); } }

 ------------->      输出为:10  20  30        ------>  a表示数组a, 3表示 a 数组坐标(10),b表示数组b,0 表示 b 数组的坐标 0,3表示拷贝的长度。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM