ITPub博客

首页 > IT职业 > IT图书 > Scanner类,System类,Runtime类

Scanner类,System类,Runtime类

IT图书 作者:piny 时间:2021-09-09 09:05:26 0 删除 编辑

Scanner类
步骤:
1:导包,import java.util.Scanner;
2:创建对象 Scanner sc = new Scanner(System.in);
3:获取 用户输入 之前 做一个判断 sc.hasNextXXX()来确保后面的获取值的方法不会抛异常
4:调用相应的 sc.nextXXX();sc.nextInt(); abc
5:关闭 输入流的资源,避免内存泄漏 sc.close
System类

// in “标准”输入流
InputStream is = System.in;
// out “标准”输出流
PrintStream ps = System.out;
// err “标准”错误输出流
System.err.println("中国");
// getEnv() 返回一个不能修改的当前系统环境的字符串映射视图
Map map = System.getenv();
System.out.println(map);
// getProperties() 确定当前的系统属性
Properties props = System.getProperties();
// getProperty() 获取指定键指示的系统属性
String usrdir = props.getProperty("user.dir");
System.out.println("用户的项目目录:" + usrdir);
// identityHashCode(); 另外一种方式获取 一个对象的唯一标识 hashcode

Object obj1 = new Object();
System.out.println(obj1.hashCode());

System.out.println(System.identityHashCode(obj1));
// gc(); 运行垃圾回收器
System.gc();
// exit() 终止当前正在运行的 Java 虚拟机
System.exit(0);
System.out.println("hello world");
// currentTimeMillis();返回当前时间的毫秒值
System.out.println("当前时间:" + System.currentTimeMillis());
// public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length) 从指定源数组中复制一个数组,复制从指定的位置开始,到目标数组的指定位置结束
int[] array1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int[] array2 = new int[10];
System.arraycopy(array1, 2, array2, 5, 3);
System.out.println(Arrays.toString(array2));

Runtime类

// availableProcessors()
Runtime rt = Runtime.getRuntime();
System.out.println("可用的处理器个数:" + rt.availableProcessors());
// freeMemory()
System.out.println("虚拟机可用内存:" + rt.freeMemory())
// totalMemory()
System.out.println("虚拟机内存总数:" + rt.totalMemory())
// runFinalization()
rt.runFinalization();// 做出一些努力调用 即将被gc回收的对象的 finalize()方法

bit:位 byte:字节 8bit = 1byte
1kb = 1 kilobyte = 1024 byte
1kb = 1024 byte
1mb = 1mega byte = 1024 kb
1GB = 1024 mb

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/3034/viewspace-2807989/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2020-05-13

  • 博文量
    137
  • 访问量
    8806