System类

java.lang.System类代表当前Java程序的运行平台。

|-可以做输入输出,垃圾回收;(此处不讲)

|-可以获取时间;

|-可以获取环境变量;

|-可以获取系统信息;

|-可以获取对象的原始HashCode。(比如String类就改写了hashCode方法,不能唯一地标识一个对象)

获取时间

public class System1时间 {
public static void main(String[] args) {
System.out.println("--currentTimeMillis():UTC(世界标准时间) 1970.1.1开始到现在的时间差");
System.out.println("毫秒:" + System.currentTimeMillis());
System.out.println("nanoTime()只能用于测量已过的时间");
System.out.println("纳秒:" + System.nanoTime());
}
}

System.currentTimeMillis()可以用户计算一段代码运行所消耗的时间。

System.nanoTime()精确度太高,硬件环境不一定能精确到纳秒,因此这个方法并不常用。


获取环境变量

import java.util.Map;
public class System2环境变量 {
public static void main(String[] args) {
String _环境变量名 = "JAVA_HOME";
System.out.println(_环境变量名 + " = " + System.getenv(_环境变量名));
System.out.println("-----环境变量(全部)-----");
Map<String, String> env = System.getenv();
for (String name : env.keySet()) {
System.out.println(name + " = " + env.get(name));
}
}
}

获取系统信息

import java.util.Properties;
public class System3getProperty {
public static void main(String[] args) {
System.out.println(System.getProperty("os.name"));
System.out.println(System.getProperty("user.dir"));// 用户工作路径
System.out.println("-----System Property(ALL)-----");
Properties props = System.getProperties();
for (Object k : props.keySet()) {
String v = props.getProperty(k.toString());
System.out.println(k);
System.out.println(k + " = " + v);
}
}
}

获取对象的原始HashCode

(比如String类就改写了hashCode方法,不能唯一地标识一个对象)

public class System4IdentityHashCode {
public static void main(String[] args) {
System.out.println("---identityHashCode():不同对象,此结果必不同---");
System.out.println("---因为hashCode()可能被重写(比如String类),无法准确确定对象---");
// s1和s2是两个不同对象
String s1 = new String("Hello");
String s2 = new String("Hello");
// String重写了hashCode()方法——改为“根据字符序列计算hashCode值”,
// 因为s1和s2的字符序列相同,所以它们的hashCode()相同
// (和equals无关,equals不是根据hashCode判断,但是也是根据字符序列比较,殊途同归)
System.out.println("new String 1.hashCode() = " + s1.hashCode());
System.out.println("new String 2.hashCode() = " + s2.hashCode());
// s1和s2是不同的字符串对象,所以它们的identityHashCode值不同
System.out.println("new String 1.identityHashCode() = " + System.identityHashCode(s1));
System.out.println("new String 2.identityHashCode() = " + System.identityHashCode(s2));
String s3 = "Java";
String s4 = "Java";
// s3和s4是相同的字符串对象,所以它们的identityHashCode值相同
System.out.println("String 3.identityHashCode() = " + System.identityHashCode(s3));
System.out.println("String 4.identityHashCode() = " + System.identityHashCode(s4));
}
}

运行结果(每次运行的结果可能不同)

---identityHashCode():不同对象,此结果必不同---
---因为hashCode()可能被重写(比如String类),无法准确确定对象---
new String 1.hashCode() = 69609650
new String 2.hashCode() = 69609650
new String 1.identityHashCode() = 366712642
new String 2.identityHashCode() = 1829164700
String 3.identityHashCode() = 2018699554
String 4.identityHashCode() = 2018699554

Java基础教程——System类的更多相关文章

  1. java基础21 System类和Runtime类

    一.System系统类 1.1.System系统类 主要用于获取系统信息 1.2.System类的常用方法 arraycopy(Object src, int srcPos, Object dest, ...

  2. java基础之System类

    System类概述System 类包含一些有用的类字段和方法.它不能被实例化. 成员方法 public static void gc()运行垃圾回收器 public static void exit( ...

  3. Java基础教程(10)--类

    一.声明类   你已经见过了以如下方式定义的类: class MyClass { // field, constructor, and method declarations }   上面是声明类的最 ...

  4. Java基础教程——File类、Paths类、Files类

    File类 File类在java.io包中.io代表input和output,输入和输出. 代表与平台无关的文件和目录. 可以新建.删除.重命名,但不能访问文件内容. File类里的常量: impor ...

  5. Java基础教程——BigDecimal类

    BigDecimal类 float.double类型的数字在计算的时候,容易发生精度丢失. 使用java.math.BigDecimal类可以解决此类问题. 前面讲过Math类,现在的BigDecim ...

  6. Java基础教程——Date类和Calendar类

    Date类和Calendar类都是关于日期的类,都在java.util包中,使用时需要import. Date java.util.Date类的对象用来表示时间和日期,用得最多的是获取系统当前日期和时 ...

  7. Java基础教程——Scanner类

    Scanner属于java.util包. java.util包是Java内置的一个工具包,其中包含一系列常用的工具类,如处理日期.日历.集合类: 如果要使用到该包中的类,必须显式引入包名:import ...

  8. Java基础教程——RunTime类

    RunTime类 java.lang.RunTime类代表Java程序的运行时环境. 可以进行垃圾回收(gc()),可以进行系统资源清理(runFinalization()): 可以加载文件(load ...

  9. Java基础教程——Object类

    Object类 Object类是Java所有类类型的父类(或者说祖先类更合适) <Thinking in Java(Java编程思想)>的第一章名字就叫"everything i ...

随机推荐

  1. viewpage和tablayout导航栏

    引入material库: implementation 'com.google.android.material:material:1.2.1' <?xml version="1.0& ...

  2. Express 配置HTML页面访问

    Express 配置HTML页面访问 1.配置模板引擎 Express默认的模板引擎是pug(jade),想要渲染html页面必须要导入对应的模板引擎ejs npm install ejs 安装完成在 ...

  3. JAVA类库之——Math类(持续更新)

    Math类 目录 Math类 1.Math 类中的常量方法 返回π(圆周率)值的方法:PI 返回E(自然对数低)值的方法:E 2.Math 类中的三角函数方法 计算正弦函数值的方法:Sin(radia ...

  4. Qt5打包后缺少dll,启动失败问题

    Qt5使用Qt自带的windeployqt打包程序教程很多,其过程也很简单,但是大部分人在打包过程中会出现提示缺少dll,或者错误0xXXXX等问题,网上各种说法,我测试过基本都不怎么正确,这里写一下 ...

  5. python重要第三方库pandas加载数据(详解)

    Pandas数据加载 关注公众号"轻松学编程"了解更多. pandas提供了一些用于将表格型数据读取为DataFrame对象的函数,其中read_csv和read_table这两个 ...

  6. [Luogu P1345] [USACO5.4]奶牛的电信Telecowmunication (最小割)

    题面 传送门:https://www.luogu.org/problemnew/show/P1345 ] Solution 这道题,需要一个小技巧了解决. 我相信很多像我这样接蒟蒻,看到这道题,不禁兴 ...

  7. C++ storage allocation + Dynamic memory allocation + setting limits + initializer list (1)

    1. 对象的空间在括号开始就已经分配,但是构造在定义对象的时候才会实现,若跳过(譬如goto),到括号结束析构会发生错误,编译会通不过. 2.初始化 1 struct X { int i ; floa ...

  8. odbc。INI配置

    [ODBC Data Sources] ST = OSCAR ODBC DRIVER [ST] Driver = /opt/ShenTong/odbc/lib/liboscarodbcw.so Ser ...

  9. haproxy 思考

    通过代理服务器在两个TCP接连之间转发数据是一个常见的需求,然后通常部署的时候涉及到(虚拟)服务器.真实服务器.防护设备.涉及到多个ip地址相关联,改动一个IP就需要修改配置. 比如反向服务器部署的时 ...

  10. nginx&http 第三章 ngx 事件http 初始化1

    在 http 配置块中,我们配置了 http 连接相关的信息,HTTP 框架也正是从这里启动的 在 nginx 初始化的过程中,执行了 ngx_init_cycle 函数,其中进行了配置文件解析,调用 ...