0
点赞
收藏
分享

微信扫一扫

java23种设计模式-解释器模式

东林梁 03-01 18:00 阅读 6

目录


System 类位于 java.lang 包下,是一个 final 类,意味着它不能被继承。并且其所有构造方法都是私有的,这使得我们无法创建 System 类的实例,只能通过类名来调用其静态方法和访问静态字段。以下是关于 System 类的全面知识点介绍:

静态字段

标准输入输出流相关

  1. System.in
    类型:InputStream
    作用:代表标准输入流,默认情况下与键盘输入关联。在控制台程序中,可用于接收用户输入的数据。
    示例:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class SystemInExample {
    public static void main(String[] args) {
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
            System.out.print("请输入你的姓名: ");
            String name = reader.readLine();
            System.out.println("你输入的姓名是: " + name);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
  1. System.out
    类型:PrintStream
    作用:代表标准输出流,默认情况下与控制台输出关联。用于向控制台打印各种信息。
    示例:
public class SystemOutExample {
    public static void main(String[] args) {
        System.out.println("这是使用 System.out 输出的信息。");
    }
}
  1. System.err
    类型:PrintStream
    作用:代表标准错误输出流,同样默认关联到控制台,但主要用于输出错误信息。与 System.out 不同,它通常会以不同的颜色或格式显示,方便用户区分正常输出和错误信息。
    示例:
public class SystemErrExample {
    public static void main(String[] args) {
        try {
            int result = 1 / 0;
        } catch (ArithmeticException e) {
            System.err.println("发生错误: " + e.getMessage());
        }
    }
}

常用静态方法

数组操作

arraycopy(Object src, int srcPos, Object dest, int destPos, int length)

作用:将源数组中从指定位置开始的一定数量的元素复制到目标数组的指定位置。这是一个本地方法,底层使用高效的内存复制操作,性能较高。

参数说明:

  • src:源数组。
  • srcPos:源数组中开始复制的起始位置。
  • dest:目标数组。
  • destPos:目标数组中开始粘贴的起始位置。
  • length:要复制的元素数量。

示例:

public class SystemArraycopyExample {
    public static void main(String[] args) {
        int[] source = {1, 2, 3, 4, 5};
        int[] destination = new int[5];
        System.arraycopy(source, 1, destination, 2, 3);
        for (int num : destination) {
            System.out.print(num + " ");
        }
    }
}

时间操作

  1. currentTimeMillis()
    • 作用:返回当前系统时间与 1970 年 1 月 1 日 00:00:00 UTC 之间的毫秒数,也称为时间戳。常用于性能测试、计时等场景。
    • 示例:
public class SystemCurrentTimeMillisExample {
    public static void main(String[] args) {
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < 1000000; i++) {
            // 模拟耗时操作
        }
        long endTime = System.currentTimeMillis();
        System.out.println("操作耗时: " + (endTime - startTime) + " 毫秒");
    }
}
  1. nanoTime()
    • 作用:返回当前系统的高精度时间,单位为纳秒。该方法返回的时间值是相对于某个固定但未指定的起点的,主要用于测量短时间间隔,精度比 currentTimeMillis() 更高。
    • 示例:
public class SystemNanoTimeExample {
    public static void main(String[] args) {
        long startTime = System.nanoTime();
        for (int i = 0; i < 1000; i++) {
            // 模拟更短的耗时操作
        }
        long endTime = System.nanoTime();
        System.out.println("操作耗时: " + (endTime - startTime) + " 纳秒");
    }
}

系统操作

  1. exit(int status)
    • 作用:终止当前正在运行的 Java 虚拟机。参数 status 为 0 表示正常终止,非零值表示异常终止。
    • 示例:
public class SystemExitExample {
    public static void main(String[] args) {
        System.out.println("程序开始执行");
        if (true) {
            System.exit(0);
        }
        System.out.println("这行代码不会被执行");
    }
}
  1. gc()
    • 作用:请求 Java 虚拟机运行垃圾回收器,尝试回收未使用的对象以释放内存。但这只是一个请求,Java 虚拟机不一定会立即执行垃圾回收操作。
    • 示例:
public class SystemGCExample {
    public static void main(String[] args) {
        for (int i = 0; i < 10000; i++) {
            new Object();
        }
        System.gc();
    }
}
  1. runFinalization()
    • 作用:请求 Java 虚拟机运行所有对象的 finalize() 方法。当一个对象被垃圾回收之前,Java 虚拟机会调用其 finalize() 方法进行一些资源清理操作。
    • 示例:
class MyClass {
    @Override
    protected void finalize() throws Throwable {
        System.out.println("对象被回收前执行 finalize 方法");
    }
}

public class SystemRunFinalizationExample {
    public static void main(String[] args) {
        MyClass obj = new MyClass();
        obj = null;
        System.gc();
        System.runFinalization();
    }
}

属性操作

  1. getProperty(String key)
    • 作用:获取指定键的系统属性值。系统属性包含了很多关于 Java 虚拟机和操作系统的信息,如 Java 版本、操作系统名称等。
    • 示例:
public class SystemGetPropertyExample {
    public static void main(String[] args) {
        String javaVersion = System.getProperty("java.version");
        System.out.println("Java 版本: " + javaVersion);
    }
}
  1. getProperty(String key, String def)
    • 作用:获取指定键的系统属性值,如果该属性不存在,则返回默认值 def。
    • 示例:
public class SystemGetPropertyWithDefaultExample {
    public static void main(String[] args) {
        String nonExistentProperty = System.getProperty("non.existent.property", "默认值");
        System.out.println("属性值: " + nonExistentProperty);
    }
}
  1. getProperties()
    • 作用:返回一个 Properties 对象,包含了所有的系统属性。可以通过遍历该对象来查看所有系统属性。
    • 示例:
import java.util.Properties;

public class SystemGetPropertiesExample {
    public static void main(String[] args) {
        Properties properties = System.getProperties();
        properties.forEach((key, value) -> System.out.println(key + " = " + value));
    }
}
  1. setProperty(String key, String value)
    • 作用:设置指定键的系统属性值。
    • 示例:
public class SystemSetPropertyExample {
    public static void main(String[] args) {
        System.setProperty("my.property", "自定义属性值");
        String value = System.getProperty("my.property");
        System.out.println("自定义属性值: " + value);
    }
}

安全管理

  1. getSecurityManager()
    • 作用:返回当前 Java 虚拟机的安全管理器,如果没有安装安全管理器,则返回 null。安全管理器用于控制 Java 程序对系统资源的访问权限。
    • 示例:
public class SystemGetSecurityManagerExample {
    public static void main(String[] args) {
        SecurityManager securityManager = System.getSecurityManager();
        if (securityManager != null) {
            System.out.println("当前安装了安全管理器");
        } else {
            System.out.println("当前未安装安全管理器");
        }
    }
}
  1. setSecurityManager(SecurityManager s)
    • 作用:设置 Java 虚拟机的安全管理器。如果参数 s 为 null,则移除当前的安全管理器。
    • 示例:
import java.lang.SecurityManager;

public class SystemSetSecurityManagerExample {
    public static void main(String[] args) {
        SecurityManager securityManager = new SecurityManager();
        System.setSecurityManager(securityManager);
        System.out.println("已设置安全管理器");
    }
}

其他方法

identityHashCode(Object x)

作用:返回指定对象的哈希码,该哈希码是基于对象的内存地址计算的,与对象的 hashCode() 方法可能不同。即使对象重写了 hashCode() 方法,identityHashCode() 仍然返回基于内存地址的哈希码。

示例:

public class SystemIdentityHashCodeExample {
    public static void main(String[] args) {
        Object obj = new Object();
        int identityHashCode = System.identityHashCode(obj);
        System.out.println("对象的基于内存地址的哈希码: " + identityHashCode);
    }
}
举报

相关推荐

0 条评论