search
HomeJavajavaTutorialHow to use java System class and Arrays class

1. Introduction

System is a system class. In the java.lang package of JDK, it can be seen that it is also a core language feature of java. The constructor of the System class is decorated with private and is not allowed to be instantiated. Therefore, the methods in the class are also static methods modified by static.

The Arrays class in JAVA is a tool class that implements array operations. It includes a variety of static methods that can implement array sorting and search, array comparison, and adding elements to the array. Functions such as copying and converting arrays to strings. These methods have overloaded methods for all basic types.

2. Detailed explanation of knowledge points

1. Concept

In the API, the introduction of the System class is relatively simple. We give the definition. System represents the system where the program is located and provides Corresponding system attribute information and system operations.

2. Commonly used methods

  • (1) public static void gc(): Used to run the garbage collector in the JVM and complete the memory Cleaning up garbage

  • (2) public static void exit(int status): Used to end the running Java program. Just pass in a number as a parameter. Usually 0 is passed in as normal status, and others are abnormal status

  • (3) public static long currentTimeMillis(): Get the current system time and January 1970 The millisecond difference between 00:00 on 01st

  • (4) public static Properties getProperties(): is used to obtain the specified key (string name) System property information recorded in

Code demonstration:

package com.Test;
import Test2.MyDate;
import java.awt.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Properties;

public class Main {

private final static String name = "磊哥的java历险记-@51博客";

/*
*public static void gc() //回收垃圾
*public static void exit(int status) //退出程序,0为正常状态,其他为异常状态
*public static long currentTimeMillis() //获取当前时间毫秒值
*public static Properties getProperties() //获取某个属性信息
*/
public static void main(String[] args) {
//构造方法被私有 不能创建对象
//System sy = new System();
//public static void exit(int status) //退出程序,0为正常状态,其他为异常状态
// System.out.println("我要退出了!");
// System.exit(0);
// System.out.println("我已经退出了!");
//public static long currentTimeMillis() //获取当前时间毫秒值
long timeMillis = System.currentTimeMillis();
long time = new Date().getTime();
long timeInMillis = Calendar.getInstance().getTimeInMillis();
System.out.println(timeMillis);
for(int i = 0; i < 5; i++) {
System.out.println("i love java");
}
long timeMillis2 = System.currentTimeMillis();
System.out.println(timeMillis2-timeMillis);
//publicstatic Properties getProperties() //获取某个属性信息
Properties properties = System.getProperties();
System.out.println(properties);
System.out.println("============="+name+"=============");
}
}

How to use java System class and Arrays class

3. Precautions

The System class cannot create objects manually because the construction method is privately modified, preventing the outside world from creating objects. All methods in the System class are static and can be accessed by class name. In the JDK, there are many such classes.

4. Arrays class

The Arrays class is a tool class provided by jdk specifically for operating arrays, located in the java.util package.

4.1. Commonly used methods of the Arrays class
  • (1) toString() method of Arrays - returns the string representation of the contents of a specified array..​

  • (2) Arrays copyOf () //Copy the specified array, intercept or fill with null (if necessary), so that the copy has the specified length.​

  • (3) Arrays sort() // Sort the specified type array in ascending numerical order.

  • (4) Arrays binarySearch () //Use binary search method to search the specified type array to obtain the specified value //Must be ordered

  • (5)Arrays fill() //Assign the specified type value to each element in the specified range of the specified type array

  • If two arrays of a given type mutually If equal, return true.

Code Demo:

package com.Test;
import java.util.Arrays;
/* Arrays toString () //返回指定数组内容的字符串表示形式。
Arrays copyOf () //复制指定的数组,截取或用 null 填充(如有必要),以使副本具有指定的长度。
Arrays sort() //对指定的类型数组按数字升序进行排序。
Arrays binarySearch () //使用二分搜索法来搜索制定类型数组,以获得指定的值 //必须有序
Arrays fill() //将指定的类型值分配给指定 类 型数组指定范围中的每个元素
Arrays equals() //如果两个指定的类型数组彼此相等,则返回 true。*/
public class Test{
private final static String name = "磊哥的java历险记-@51博客";
public static void main(String args[]){

//定义数组
int[] score={1,2,3};
int[] scores={1,2,3};
//数组之间比较,长度,值相等,则认为两个数组相等,返回布尔值
System.out.println("比较值和长度:"+Arrays.equals(score,scores));
//判断地址
if(score==scores){
System.out.println("score和scores比较,相等");
}else{
System.out.println("score和scores比较,不相等");
}
//定义二维数组
int[][] sc={{222,333,1,2,0},{1,2,3,2,0}};
//排序
Arrays.sort(sc[1]);
System.out.println("排序:"+Arrays.toString(sc[1]));
System.out.println("按照下标取值:"+sc[0][1]+" ");

//定义数据se
int[] se={1,2,3,4,5};
//填充数组
Arrays.fill(se,0);
System.out.println("填充:"+Arrays.toString(se));
//复制值到sx,增加指定长度
int[] sx=Arrays.copyOf(se,2);
//输出sx的填充后的值
System.out.println("复制2:"+Arrays.toString(sx));
int[] xb={14,20,67,34,33,23,10};
//排序xb
Arrays.sort(xb);
System.out.println(Arrays.toString(xb));
//在排序后,通过二分查找,找到34的元素,并返回下标
int index1=Arrays.binarySearch(xb,34);
System.out.println("二分法取值:"+index1);
System.out.println("============="+name+"=============");
}
}

How to use java System class and Arrays class

How to use java System class and Arrays class

##4.2. Refinement Exercise
In using the Arrays class, we will use some basic algorithms such as sorting, etc.

Title:

  • (1) Create an int type array A, the value of A is {1,2,3,4,5}

  • (2) Copy the value of A into B with a length of 6

  • (3) Compare whether A and B are the same

Experimental steps:

  • (1) Declare a class Test and create two arrays

  • (2) Use Arrays related methods to complete the operation

Code demonstration:

package com.Test;

import java.util.Arrays;
/*声明一个类Test,并且创建两个数组*/
/* Arrays toString () //返回指定数组内容的字符串表示形式。
Arrays copyOf () //复制指定的数组,截取或用 null 填充(如有必要),以使副本具有指定的长度。
Arrays sort() //对指定的类型数组按数字升序进行排序。
Arrays binarySearch () //使用二分搜索法来搜索制定类型数组,以获得指定的值 //必须有序
Arrays fill() //将指定的类型值分配给指定 类 型数组指定范围中的每个元素
Arrays equals() //如果两个指定的类型数组彼此相等,则返回 true。*/
public class Main {
private final static String name = "磊哥的java历险记-@51博客";
public static void main(String[] args){
//创建int类型数组A,A的值为{1,2,3,4,5}
int[]A = new int[]{1,2,3,4,5};
//将A的值拷贝进长度为6的B中
int[]B = Arrays.copyOf(A, 6);
//比较A和B是否相同
System.out.println("两个数组是否相等:"+Arrays.equals(A, B));
System.out.println("============="+name+"=============");
}
}

How to use java System class and Arrays class

The above is the detailed content of How to use java System class and Arrays class. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
JVM performance vs other languagesJVM performance vs other languagesMay 14, 2025 am 12:16 AM

JVM'sperformanceiscompetitivewithotherruntimes,offeringabalanceofspeed,safety,andproductivity.1)JVMusesJITcompilationfordynamicoptimizations.2)C offersnativeperformancebutlacksJVM'ssafetyfeatures.3)Pythonisslowerbuteasiertouse.4)JavaScript'sJITisles

Java Platform Independence: Examples of useJava Platform Independence: Examples of useMay 14, 2025 am 12:14 AM

JavaachievesplatformindependencethroughtheJavaVirtualMachine(JVM),allowingcodetorunonanyplatformwithaJVM.1)Codeiscompiledintobytecode,notmachine-specificcode.2)BytecodeisinterpretedbytheJVM,enablingcross-platformexecution.3)Developersshouldtestacross

JVM Architecture: A Deep Dive into the Java Virtual MachineJVM Architecture: A Deep Dive into the Java Virtual MachineMay 14, 2025 am 12:12 AM

TheJVMisanabstractcomputingmachinecrucialforrunningJavaprogramsduetoitsplatform-independentarchitecture.Itincludes:1)ClassLoaderforloadingclasses,2)RuntimeDataAreafordatastorage,3)ExecutionEnginewithInterpreter,JITCompiler,andGarbageCollectorforbytec

JVM: Is JVM related to the OS?JVM: Is JVM related to the OS?May 14, 2025 am 12:11 AM

JVMhasacloserelationshipwiththeOSasittranslatesJavabytecodeintomachine-specificinstructions,managesmemory,andhandlesgarbagecollection.ThisrelationshipallowsJavatorunonvariousOSenvironments,butitalsopresentschallengeslikedifferentJVMbehaviorsandOS-spe

Java: Write Once, Run Anywhere (WORA) - A Deep Dive into Platform IndependenceJava: Write Once, Run Anywhere (WORA) - A Deep Dive into Platform IndependenceMay 14, 2025 am 12:05 AM

Java implementation "write once, run everywhere" is compiled into bytecode and run on a Java virtual machine (JVM). 1) Write Java code and compile it into bytecode. 2) Bytecode runs on any platform with JVM installed. 3) Use Java native interface (JNI) to handle platform-specific functions. Despite challenges such as JVM consistency and the use of platform-specific libraries, WORA greatly improves development efficiency and deployment flexibility.

Java Platform Independence: Compatibility with different OSJava Platform Independence: Compatibility with different OSMay 13, 2025 am 12:11 AM

JavaachievesplatformindependencethroughtheJavaVirtualMachine(JVM),allowingcodetorunondifferentoperatingsystemswithoutmodification.TheJVMcompilesJavacodeintoplatform-independentbytecode,whichittheninterpretsandexecutesonthespecificOS,abstractingawayOS

What features make java still powerfulWhat features make java still powerfulMay 13, 2025 am 12:05 AM

Javaispowerfulduetoitsplatformindependence,object-orientednature,richstandardlibrary,performancecapabilities,andstrongsecurityfeatures.1)PlatformindependenceallowsapplicationstorunonanydevicesupportingJava.2)Object-orientedprogrammingpromotesmodulara

Top Java Features: A Comprehensive Guide for DevelopersTop Java Features: A Comprehensive Guide for DevelopersMay 13, 2025 am 12:04 AM

The top Java functions include: 1) object-oriented programming, supporting polymorphism, improving code flexibility and maintainability; 2) exception handling mechanism, improving code robustness through try-catch-finally blocks; 3) garbage collection, simplifying memory management; 4) generics, enhancing type safety; 5) ambda expressions and functional programming to make the code more concise and expressive; 6) rich standard libraries, providing optimized data structures and algorithms.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools