Java常用类
在Java编程中,有许多常用的类提供了丰富的功能和方法,帮助开发者更高效地完成各种任务。本文将介绍包装类、字符串相关类、日期时间类、Math类和枚举类,并提供相应的代码示例。
1. 包装类(Wrapper Classes)
Java中的包装类提供了将基本数据类型转换为对象的功能,使得基本数据类型具有对象的特性。常用的包装类包括Integer、Double、Boolean等。
代码示例:
public class WrapperClassExample {
public static void main(String[] args) {
// 使用包装类创建对象
Integer num1 = new Integer(10);
Double num2 = new Double(3.14);
// 自动装箱
Integer num3 = 20; Double num4 = 6.28;
// 自动拆箱
int value1 = num1.intValue();
double value2 = num2.doubleValue();
// 字符串转换为包装类对象
Integer num5 = Integer.valueOf("30");
Double num6 = Double.valueOf("9.8");
// 包装类转换为字符串
String str1 = num1.toString();
String str2 = num2.toString();
System.out.println("num1: " + num1 + ", num2: " + num2);
System.out.println("num3: " + num3 + ", num4: " + num4);
System.out.println("value1: " + value1 + ", value2: " + value2);
System.out.println("num5: " + num5 + ", num6: " + num6);
System.out.println("str1: " + str1 + ", str2: " + str2); } }
2. 字符串相关类(String and StringBuilder)
Java中的字符串相关类提供了丰富的方法来操作字符串,包括字符串的拼接、查找、替换等操作。String类是不可变的,而StringBuilder则是可变的,适用于频繁的字符串操作。
代码示例:
public class StringExample {
public static void main(String[] args) {
// String类示例
String str1 = "Hello";
String str2 = "World";
// 字符串拼接
String result1 = str1 + " " + str2;
System.out.println("result1: " + result1);
// 字符串长度
int length = result1.length();
System.out.println("Length: " + length);
// 字符串查找
boolean contains = result1.contains("World");
System.out.println("Contains 'World': " + contains);
// 字符串替换
String replaced = result1.replace("World", "Java");
System.out.println("Replaced: " + replaced);
// StringBuilder类示例
StringBuilder builder = new StringBuilder();
builder.append("Java");
builder.append(" is");
builder.append(" awesome");
String result2 = builder.toString();
System.out.println("result2: " + result2); } }
3. 日期时间类(Date and Time)
Java中的日期时间类用于处理日期和时间信息,包括日期格式化、日期计算、时区设置等功能。常用的类包括Date、Calendar和DateFormat等。
代码示例:
import java.util.Date;
import java.text.SimpleDateFormat;
public class DateTimeExample {
public static void main(String[] args) {
// 获取当前日期时间
Date now = new Date();
System.out.println("Current time: " + now);
// 日期格式化
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = sdf.format(now);
System.out.println("Formatted time: " + formattedDate); } }
4. Math类
Java中的Math类提供了常用的数学运算方法,如求绝对值、取整、幂运算等。
代码示例:
public class MathExample { public static void main(String[] args) {
// 求绝对值
double absValue = Math.abs(-10.5);
System.out.println("Absolute value: " + absValue);
// 向上取整
double ceilValue = Math.ceil(3.14);
System.out.println("Ceil value: " + ceilValue);
// 向下取整
double floorValue = Math.floor(3.14);
System.out.println("Floor value: " + floorValue);
// 平方根
double sqrtValue = Math.sqrt(25);
System.out.println("Square root value: " + sqrtValue);
// 幂运算
double powValue = Math.pow(2, 3);
System.out.println("Power value: " + powValue);
// 随机数生成
double randomValue = Math.random();
System.out.println("Random value: " + randomValue); } }
5. 枚举(Enum)
枚举是一种特殊的类,用于定义一组有限的常量,通常用于表示一些固定的值,如星期、月份等。
代码示例:
public class EnumExample { enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY }
public static void main(String[] args) {
Day today = Day.MONDAY; System.out.println("Today is " + today); } }