常用Java函数一览表及示例演示
Java是一种高级编程语言,广泛应用于各类应用程序和开发领域。Java具有跨平台、面向对象、简洁、安全等优点,而常用的Java函数则是开发Java应用程序中不可或缺的一部分。本文将介绍一些常用的Java函数,并提供简单的示例演示。
1. String类函数
String类是Java中用于处理字符串的类,常用的函数包括:
(1) length() : 获取字符串的长度
示例:
String str = "Hello World!";
int len = str.length();
System.out.println("字符串 \"" + str + "\" 的长度为:" + len);
输出结果:
字符串 "Hello World!" 的长度为:12
(2) substring() : 获取子字符串
示例:
String str = "Hello World!";
String subStr = str.substring(6);
System.out.println("截取子字符串为:" + subStr);
输出结果:
截取子字符串为:World!
(3) toUpperCase() : 将字符串转换为大写字母
示例:
String str = "Hello World!";
String upperStr = str.toUpperCase();
System.out.println("转换为大写后的字符串为:" + upperStr);
输出结果:
转换为大写后的字符串为:HELLO WORLD!
2. Math类函数
Math类是Java中用于进行数学计算的类,常用的函数包括:
(1) abs() : 获取数字的绝对值
示例:
int num = -10;
int absNum = Math.abs(num);
System.out.println("数字 " + num + " 的绝对值为:" + absNum);
输出结果:
数字 -10 的绝对值为:10
(2) pow() : 计算一个数的幂
示例:
double num = 2.0;
double powNum = Math.pow(num, 3);
System.out.println("数字 " + num + " 的3次幂为:" + powNum);
输出结果:
数字 2.0 的3次幂为:8.0
(3) random() : 生成随机数
示例:
double randomNum = Math.random();
System.out.println("生成的随机数为:" + randomNum);
输出示例:
生成的随机数为:0.9075761718337105
3. Arrays类函数
Arrays类是Java中用于处理数组的类,常用的函数包括:
(1) sort() : 对数组进行排序
示例:
int[] array = {4, 2, 6, 1, 3, 5};
Arrays.sort(array);
System.out.println("排序后的数组为:" + Arrays.toString(array));
输出结果:
排序后的数组为:[1, 2, 3, 4, 5, 6]
(2) binarySearch() : 在有序数组中查找指定元素的位置
示例:
int[] array = {1, 2, 3, 4, 5, 6};
int index = Arrays.binarySearch(array, 4);
System.out.println("数字 4 在数组中的索引为:" + index);
输出结果:
数字 4 在数组中的索引为:3
(3) fill() : 将数组中的元素全部修改为指定值
示例:
int[] array = new int[5];
Arrays.fill(array, 10);
System.out.println("修改后的数组为:" + Arrays.toString(array));
输出结果:
修改后的数组为:[10, 10, 10, 10, 10]
4. Date类函数
Date类是Java中用于处理日期和时间的类,常用的函数包括:
(1) getTime() : 获取当前时间戳
示例:
Date date = new Date();
long timestamp = date.getTime();
System.out.println("当前时间戳为:" + timestamp);
输出结果:
当前时间戳为:1639348353441
(2) toString() : 将日期转换为字符串
示例:
Date date = new Date();
String str = date.toString();
System.out.println("日期转换为字符串后为:" + str);
输出结果:
日期转换为字符串后为:Sat Dec 11 09:12:33 CST 2021
(3) compareTo() : 比较两个日期的大小
示例:
Date date1 = new Date();
Thread.sleep(1000); // 等待1秒
Date date2 = new Date();
int result = date1.compareTo(date2);
if (result < 0) {
System.out.println("日期1早于日期2");
} else if (result == 0) {
System.out.println("日期1等于日期2");
} else {
System.out.println("日期1晚于日期2");
}
输出结果:
日期1早于日期2
5. File类函数
File类是Java中用于处理文件和目录的类,常用的函数包括:
(1) exists() : 判断文件或目录是否存在
示例:
File file = new File("D:/test.txt");
boolean exists = file.exists();
if (exists) {
System.out.println("文件存在");
} else {
System.out.println("文件不存在");
}
输出结果:
文件不存在
(2) isFile() : 判断是否为文件
示例:
File file = new File("D:/test.txt");
if (file.isFile()) {
System.out.println("是文件");
} else {
System.out.println("不是文件");
}
输出结果:
是文件
(3) isDirectory() : 判断是否为目录
示例:
File file = new File("D:/test");
if (file.isDirectory()) {
System.out.println("是目录");
} else {
System.out.println("不是目录");
}
输出结果:
是目录
总结:
以上介绍了一些常用的Java函数及其示例演示,这些函数在日常开发中经常会用到,掌握它们能够提高开发效率,并使代码更简洁、优雅。当然,这些函数只是Java中的冰山一角,建议大家在开发过程中多了解、多使用、多学习。
