包装类
包装类(Wrapper Class)是Java中提供的一组类,用于将基本数据类型(如int
、double
、boolean
等)包装为对象。每个基本数据类型都有对应的包装类,如Integer
、Double
、Boolean
等。包装类提供了一些常用的方法和属性,使得可以在对象上进行操作和处理。
以下是Java中常见的包装类及其对应的基本数据类型:
Boolean
:对应boolean
Byte
:对应byte
Short
:对应short
Integer
:对应int
Long
:对应long
Float
:对应float
Double
:对应double
Character
:对应char
包装类的主要作用是在需要使用对象而不能使用基本数据类型的场景中,提供了一种封装基本数据类型的方式。它们可以用于集合类、泛型、反射、方法重载等需要使用对象的情况。
以下是一些包装类的使用示例:
// 使用包装类创建对象
Integer number = new Integer(10);
Boolean flag = new Boolean(true);
Character ch = new Character('a');
// 使用包装类的静态方法创建对象
Integer num1 = Integer.valueOf(20);
Double num2 = Double.valueOf(3.14);
Boolean result = Boolean.valueOf(false);
// 自动装箱和拆箱
Integer num3 = 30; // 自动装箱
int value = num3; // 自动拆箱
// 使用包装类的方法进行操作
Integer sum = Integer.sum(10, 20);
Double sqrt = Math.sqrt(9.0);
// 包装类和基本类型的比较
Boolean isEqual = Objects.equals(true, false); // 比较两个Boolean对象的值是否相等
// 包装类转换为基本类型
int intValue = number.intValue();
double doubleValue = num1.doubleValue();
// 字符串转换为包装类对象
Integer parsedInt = Integer.parseInt("123");
Double parsedDouble = Double.parseDouble("3.14");
// 包装类对象转换为字符串
String str = num1.toString();
// 包装类常量
int maxInt = Integer.MAX_VALUE;
double minDouble = Double.MIN_VALUE;
char spaceChar = Character.SPACE_SEPARATOR;
需要注意的是,包装类是不可变的,即一旦创建就不能修改其值。如果需要对包装类对象进行修改操作,可以创建新的对象来保存修改后的值。此外,由于自动装箱和拆箱的特性,包装类的使用可能会涉及到自动装箱和拆箱的性能开销,因此在性能敏感的场景中,需要谨慎使用包装类。
LocalDateTime
LocalDateTime
类是Java中的日期时间类,用于表示不带时区的日期和时间。它提供了处理日期和时间的常用方法,如获取年、月、日、时、分等信息,以及进行日期时间的计算和比较。
常用方法:
static LocalDateTime now()
: 返回当前日期和时间。static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute)
: 创建指定日期和时间的LocalDateTime
对象。int getYear()
: 获取年份。Month getMonth()
: 获取月份,返回一个Month
枚举值。int getDayOfMonth()
: 获取月份中的日期。int getHour()
: 获取小时。int getMinute()
: 获取分钟。LocalDateTime plusDays(long daysToAdd)
: 返回增加指定天数后的LocalDateTime
对象。LocalDateTime minusHours(long hoursToSubtract)
: 返回减去指定小时数后的LocalDateTime
对象。boolean isBefore(LocalDateTime other)
: 检查当前日期时间是否在另一个日期时间之前。boolean isAfter(LocalDateTime other)
: 检查当前日期时间是否在另一个日期时间之后。
示例:
LocalDateTime currentDateTime = LocalDateTime.now();
int year = currentDateTime.getYear();
Month month = currentDateTime.getMonth();
int dayOfMonth = currentDateTime.getDayOfMonth();
int hour = currentDateTime.getHour();
int minute = currentDateTime.getMinute();
LocalDateTime futureDateTime = currentDateTime.plusDays(5);
LocalDateTime pastDateTime = currentDateTime.minusHours(2);
boolean isBefore = currentDateTime.isBefore(futureDateTime);
boolean isAfter = currentDateTime.isAfter(pastDateTime);
LocalDate
LocalDate
类是Java中的日期类,用于表示不带时区的日期。
常用方法:
static LocalDate now()
: 返回当前日期。static LocalDate of(int year, int month, int dayOfMonth)
: 创建指定日期的LocalDate
对象。int getYear()
: 获取年份。Month getMonth()
: 获取月份,返回一个Month
枚举值。int getDayOfMonth()
: 获取月份中的日期。DayOfWeek getDayOfWeek()
: 获取星期几,返回一个DayOfWeek
枚举值。LocalDate plusDays(long daysToAdd)
: 返回增加指定天数后的LocalDate
对象。LocalDate minusMonths(long monthsToSubtract)
: 返回减去指定月份数后的LocalDate
对象。boolean isBefore(LocalDate other)
: 检查当前日期是否在另一个日期之前。boolean isAfter(LocalDate other)
: 检查当前日期是否在另一个日期之后。
LocalTime
LocalTime
类是Java中的时间类,用于表示不带时区的时间。
常用方法:
static LocalTime now()
: 返回当前时间。static LocalTime of(int hour, int minute)
: 创建指定时间的LocalTime
对象。int getHour()
: 获取小时。int getMinute()
: 获取分钟。int getSecond()
: 获取秒数。LocalTime plusHours(long hoursToAdd)
: 返回增加指定小时数后的LocalTime
对象。LocalTime minusMinutes(long minutesToSubtract)
: 返回减去指定分钟数后的LocalTime
对象。boolean isBefore(LocalTime other)
: 检查当前时间是否在另一个时间之前。boolean isAfter(LocalTime other)
: 检查当前时间是否在另一个时间之后。
示例:
LocalDate currentDate = LocalDate.now();
int year = currentDate.getYear();
Month month = currentDate.getMonth();
int dayOfMonth = currentDate.getDayOfMonth();
DayOfWeek dayOfWeek = currentDate.getDayOfWeek();
LocalDate futureDate = currentDate.plusDays(5);
LocalDate pastDate = currentDate.minusMonths(2);
boolean isBefore = currentDate.isBefore(futureDate);
boolean isAfter = currentDate.isAfter(pastDate);
LocalTime currentTime = LocalTime.now();
int hour = currentTime.getHour();
int minute = currentTime.getMinute();
int second = currentTime.getSecond();
LocalTime futureTime = currentTime.plusHours(3);
LocalTime pastTime = currentTime.minusMinutes(30);
boolean isBeforeTime = currentTime.isBefore(futureTime);
boolean isAfterTime = currentTime.isAfter(pastTime);
Integer:
Integer
类是Java中用于表示整数的包装类。它提供了各种处理整数的方法,如解析字符串为整数、整数与字符串之间的转换、比较整数大小等。
常用方法:
static int parseInt(String s)
: 将字符串解析为整数。static String toString(int i)
: 将整数转换为字符串。static int compare(int x, int y)
: 比较两个整数的大小,返回值为负数、零或正数。int intValue()
: 将Integer
对象转换为基本类型int
。static Integer valueOf(int i)
: 返回表示指定整数值的Integer
对象。boolean equals(Object obj)
: 比较两个Integer
对象是否相等。int compareTo(Integer anotherInteger)
: 比较当前Integer
对象与另一个Integer
对象的大小。static Integer max(int a, int b)
: 返回两个整数中较大的值。static Integer min(int a, int b)
: 返回两个整数中较小的值。
示例:
int number = Integer.parseInt("123");
String str = Integer.toString(456);
int comparison = Integer.compare(10, 5);
Integer integer = Integer.valueOf(100);
int value = integer.intValue();
boolean isEqual = Integer.valueOf(5).equals(Integer.valueOf(5));
int result = Integer.valueOf(10).compareTo(Integer.valueOf(5));
int max = Integer.max(7, 3);
int min = Integer.min(7, 3);
注意事项:
-
自动装箱和拆箱:
Integer
是一个包装类,用于将基本类型int
包装为对象。在需要使用Integer
对象的地方,可以直接使用int
类型,Java会自动进行装箱和拆箱的转换。例如:Integer num1 = 10; // 自动装箱 int num2 = num1; // 自动拆箱
尽管自动装箱和拆箱方便,但频繁的装箱和拆箱操作可能会导致性能下降。在对性能要求较高的场景中,应尽量避免不必要的装箱和拆箱操作。
-
对象相等性比较:在比较两个
Integer
对象的相等性时,应使用equals()
方法而不是==
运算符。因为==
运算符用于比较引用类型的对象时,比较的是对象的引用地址,而不是对象的值。例如:Integer num1 = 10; Integer num2 = 10; System.out.println(num1.equals(num2)); // 输出true System.out.println(num1 == num2); // 输出false
在上述示例中,
num1
和num2
虽然包含相同的值,但它们是不同的对象。使用equals()
方法可以比较两个Integer
对象的值是否相等。 -
整数缓存:Java中的整数缓存范围是-128到127。在这个范围内的整数会被缓存为对象,多次使用相同的值创建的
Integer
对象会引用相同的对象实例。例如:Integer num1 = 100; Integer num2 = 100; System.out.println(num1 == num2); // 输出true Integer num3 = 200; Integer num4 = 200; System.out.println(num3 == num4); // 输出false
在上述示例中,由于100在缓存范围内,
num1
和num2
引用了同一个对象实例,而200超出了缓存范围,num3
和num4
则引用了不同的对象实例。 -
数值转换:
Integer
类提供了将整数转换为其他基本类型的方法,如intValue()
、longValue()
、floatValue()
和doubleValue()
等。在进行数值转换时,应注意可能发生的溢出或精度丢失问题。例如:int intValue = Integer.MAX_VALUE; long longValue = Integer.MAX_VALUE; float floatValue = Integer.MAX_VALUE; double doubleValue = Integer.MAX_VALUE; System.out.println(intValue); // 输出2147483647 System.out.println(longValue); // 输出2147483647 System.out.println(floatValue); // 输出2.14748365E9 System.out.println(doubleValue); // 输出2.147483647E9
在上述示例中,由于
Integer.MAX_VALUE
的值为2147483647,超过了float
和double
的表示范围,导致转换后的结果出现了精度丢失。