当前位置: 首页 > news >正文

【Java学习笔记】System类

System 类

常用方法

方法描述
System.exit()退出当前程序
System.arraycopy(源数组,源数组起始索引,目标数组,目标数组起始索引,拷贝长度)复制数组元素,比较适合底层调用
System.currentTimeMillis()返回当前时间距离 1970-1-1 的毫秒数
System.gc()执行垃圾回收机制

一、exit()

传入参数 0:表示正常退出

代码示例

public class main {public static void main(String[] args) {System.out.println("hello world");System.exit(0);System.out.println("程序继续执行...");}
}// 输出结果
hello world

二、System.arraycopy()

复制数组元素,比较适合底层调用

使用方法:System.arraycopy(源数组,源数组起始索引,目标数组,目标数组起始索引,拷贝长度)

代码示例

import java.util.Arrays;public class main {public static void main(String[] args) {int[] arr = {1,2,3,4};int[] arr_new = new int[arr.length]; // 此时 arr_new 为 {0, 0, 0, 0}System.arraycopy(arr,0,arr_new,1,3);System.out.println(Arrays.toString(arr_new));}
}// 输出结果
[0, 1, 2, 3]

代码说明


三、System.currentTimeMillis()

返回当前时间距离 1970-1-1 的毫秒数

代码示例

public class main {public static void main(String[] args) {System.out.println(System.currentTimeMillis());}
}// 输出结果
1749372542217

四、System.gc()

主动触发垃圾回收机制,底层会默认调用finalize()方法执行垃圾回收机制

代码示例

public class finalize {public static void main(String[] args) {finals finals = new finals(18);finals = null;System.gc();}
}
class finals{int age;public finals(){}public finals(int age) {this.age = age;}@Overrideprotected void finalize() throws Throwable {System.out.println("调用finalize回收对象");}
}// 输出
调用finalize回收对象
http://www.dtcms.com/a/239232.html

相关文章:

  • Linux下的进程调度机制
  • 深入理解 Java 的反射、注解与动态代理
  • 2025新高考二卷选择题第一题题解
  • LangChain4j 1.x 核心源码剖析-基础篇
  • 【项目实训项目博客】用户使用手册
  • Secs/Gem第十二讲(基于secs4net项目的ChatGpt介绍)
  • 【第四十八周】HippoRAG 2 复现与分析(二):索引阶段代码分析
  • 嵌入式学习笔记 - freeRTOS为什么中断中不能使用互斥量
  • 使用自定义模板的方式批量切割图片。
  • 从零开始编写Mcp Server,发布上线,超简单极速入门
  • Amazon RDS on AWS Outposts:解锁本地化云数据库的混合云新体验
  • 循环神经网络(RNN):从理论到翻译
  • 标准代码项目开发流程学习指南
  • leetcode Top100 238. 除自身以外数组的乘积|数组系列
  • 【Ragflow】27.RagflowPlus(v0.4.1):小版本迭代,问题修复与功能优化
  • 高保真组件库:单选复选
  • 为什么需要Redis分布式锁?在哪些微服务场景下会用到?
  • 【C/C++】namespace + macro混用场景
  • 解决SQL Server SQL语句性能问题(9)——SQL语句改写(2)
  • gitee....
  • split方法
  • 如果在main中抛出异常,该如何处理
  • 2.1.3_1 编码和调制(上)
  • 联邦学习在各领域的落地应用
  • 【GPT模型训练】第一课:安装PyTorch环境
  • Python-Flask
  • Learning Smooth Humanoid Locomotion through Lipschitz-Constrained Policies
  • Visio粘贴Word公式技巧
  • 动态工作流:目标结构来自外部数据集
  • MySQL 故障排查:从 `SHOW PROCESSLIST` 到死锁检测的完整流程