欢迎访问宙启技术站
智能推送

Java标准库常用函数介绍及示例

发布时间:2023-06-25 09:31:47

Java标准库是Java语言自带的一组类和接口库,其中包含了大量常用函数,用于完成各种日常的编程工作。这些函数大多数都是从操作系统API或其他语言的标准库中移植过来的,因此具有很高的可靠性和兼容性。在本篇文章中,我们将介绍Java标准库中常用的几个函数及其使用方法。

一、输入输出函数

1. print() 和 println()

这两个函数用于向输出流中打印文本。它们的不同之处在于:print() 输出后不会自动换行,而 println() 输出后会自动换行。

例如:

System.out.print("Hello ");

System.out.println("world!");

输出结果为:

Hello world!

2. printf()

printf() 函数可以使用指定的格式字符串进行格式化输出。它的语法与 C 语言中的 printf() 函数类似。

格式化字符串的语法如下:

%[flags][width][.precision]conversion

其中,flags、width、precision 和 conversion 都是可选的参数。下面以几个例子来说明这个函数的用法:

int age = 25;

double height = 1.75;

System.out.printf("My age is %d and my height is %.2f meters.

", age, height);

输出结果为:

My age is 25 and my height is 1.75 meters.

二、字符串函数

1. length()

length() 函数用于获取字符串的长度。

例如:

String str = "Hello world!";

int len = str.length();

System.out.printf("The length of \"%s\" is %d.

", str, len);

输出结果为:

The length of "Hello world!" is 12.

2. indexOf()

indexOf() 函数用于查找某个子串在字符串中出现的位置。如果找到了,则返回它的下标;否则返回 -1。

例如:

String str = "Hello world!";

int index = str.indexOf("world");

System.out.printf("The index of \"world\" in \"%s\" is %d.

", str, index);

输出结果为:

The index of "world" in "Hello world!" is 6.

3. substring()

substring() 函数用于截取字符串的一部分。

它有两个重载的版本:

substring(int beginIndex):从 beginIndex 开始截取到末尾。

substring(int beginIndex, int endIndex):从 beginIndex 开始截取到 endIndex-1。

例如:

String str = "Hello world!";

String sub1 = str.substring(6);

String sub2 = str.substring(0, 5);

System.out.printf("sub1: \"%s\", sub2: \"%s\".

", sub1, sub2);

输出结果为:

sub1: "world!", sub2: "Hello".

三、日期时间函数

1. Date 类

Date 类是 Java 标准库中用于表示日期时间的类。它有构造函数和各种方法,用于获取、设置时间等操作。

例如:

import java.util.Date;

Date now = new Date();

System.out.println(now);

输出结果为:

Wed Sep 22 10:00:00 CST 2021

2. SimpleDateFormat 类

SimpleDateFormat 类用于将日期时间格式化为字符串,或将字符串解析为日期对象。

例如:

import java.text.SimpleDateFormat;

import java.util.Date;

Date now = new Date();

SimpleDateFormat fmt1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

SimpleDateFormat fmt2 = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");

String str1 = fmt1.format(now);

String str2 = fmt2.format(now);

System.out.printf("Pattern 1: \"%s\"

", str1);

System.out.printf("Pattern 2: \"%s\"

", str2);

输出结果为:

Pattern 1: "2021-09-22 10:00:00"

Pattern 2: "2021年09月22日 10时00分00秒"

四、数组函数

1. Arrays 类

Arrays 类提供了许多静态方法,用于操作数组。其中最常用的有以下几个:

sort(int[] arr):对整型数组 arr 进行排序。

binarySearch(int[] arr, int key):在整型数组 arr 中查找关键字 key 的位置,如果找到返回其下标;否则返回 -1。

copyOf(int[] original, int newLength):将整型数组 original 复制到一个新数组中,并返回长度为 newLength 的子数组,不足部分用 0 补齐(如果 newLength 小于 original 的长度)。

例如:

import java.util.Arrays;

int[] arr = {3, 1, 4, 1, 5, 9, 2, 6, 5};

Arrays.sort(arr);

System.out.println(Arrays.toString(arr));

int index = Arrays.binarySearch(arr, 6);

System.out.printf("The index of 6 is %d.

", index);

int[] sub = Arrays.copyOf(arr, 5);

System.out.println(Arrays.toString(sub));

输出结果为:

[1, 1, 2, 3, 4, 5, 5, 6, 9]

The index of 6 is 7.

[1, 1, 2, 3, 4]

总结

以上是 Java 标准库中常用的几个函数及其使用方法。Java标准库中的函数数量庞大,本文只是介绍了其中的一部分。在实际编程中,需要根据具体的需求选择合适的函数进行调用。掌握这些常用函数的使用方法,可以大大提高编写 Java 程序的效率。