包装类及其抽象基类

包装类及其抽象基类

Number

Number 类是 Java 中所有数值类的抽象基类。它为整数和浮点数提供了一些基础的数值转换方法。具体的数值类型包括 Byte, Short, Integer, Long, Float, 和 Double,这些类都继承自 Number 类。Number 类中的方法主要用于将数值类型对象转换为不同的基本数据类型。
虽然 Number 是一个抽象类,但它提供了一些具体的转换方法,这些方法在其子类中得到了实现。在下面的讲解中,我们会详细介绍 Number 类中的所有方法,包括每个方法的参数和作用,并给出相应的示例代码。

一、基本数值转换方法

1. byte byteValue()

功能: 返回该 Number 对象的值并将其转换为 byte 类型。
参数: 无
返回值: byte - 该 Number 对象的 byte 值。
示例:

public class NumberExample {
    public static void main(String[] args) {
        Integer num = 123;
        byte result = num.byteValue();
        System.out.println("Byte value: " + result); // 输出: 123
    }
}
2. short shortValue()

功能: 返回该 Number 对象的值并将其转换为 short 类型。
参数: 无
返回值: short - 该 Number 对象的 short 值。
示例:

public class NumberExample {
    public static void main(String[] args) {
        Integer num = 12345;
        short result = num.shortValue();
        System.out.println("Short value: " + result); // 输出: 12345
    }
}
3. int intValue()

功能: 返回该 Number 对象的值并将其转换为 int 类型。
参数: 无
返回值: int - 该 Number 对象的 int 值。
示例:

public class NumberExample {
    public static void main(String[] args) {
        Double num = 123.45;
        int result = num.intValue();
        System.out.println("Int value: " + result); // 输出: 123
    }
}
4. long longValue()

功能: 返回该 Number 对象的值并将其转换为 long 类型。
参数: 无
返回值: long - 该 Number 对象的 long 值。
示例:

public class NumberExample {
    public static void main(String[] args) {
        Float num = 123.45f;
        long result = num.longValue();
        System.out.println("Long value: " + result); // 输出: 123
    }
}
5. float floatValue()

功能: 返回该 Number 对象的值并将其转换为 float 类型。
参数: 无
返回值: float - 该 Number 对象的 float 值。
示例:

public class NumberExample {
    public static void main(String[] args) {
        Long num = 12345L;
        float result = num.floatValue();
        System.out.println("Float value: " + result); // 输出: 12345.0
    }
}
6. double doubleValue()

功能: 返回该 Number 对象的值并将其转换为 double 类型。
参数: 无
返回值: double - 该 Number 对象的 double 值。
示例:

public class NumberExample {
    public static void main(String[] args) {
        Integer num = 12345;
        double result = num.doubleValue();
        System.out.println("Double value: " + result); // 输出: 12345.0
    }
}

二、具体数值类型的类及其特有方法

除了 Number 类本身的方法,Java 中的具体数值类型(如 Integer, Double 等)还提供了许多特有的方法。下面我们分别介绍这些具体数值类型及其方法。

1. Integer

Integer 类是 int 类型的包装类,除了继承自 Number 类的方法外,还提供了许多特有的方法。

  • static int parseInt(String s): 将字符串解析为 int 类型。
  • static Integer valueOf(String s): 返回一个保存指定值的 Integer 对象。
  • static String toString(int i): 返回指定 int 的字符串表示形式。
  • int compareTo(Integer anotherInteger): 比较两个 Integer 对象。

示例:

public class IntegerExample {
    public static void main(String[] args) {
        // 使用 parseInt 方法
        int num = Integer.parseInt("123");
        System.out.println("Parsed int: " + num); // 输出: 123

        // 使用 valueOf 方法
        Integer numObj = Integer.valueOf("123");
        System.out.println("Integer object: " + numObj); // 输出: 123

        // 使用 toString 方法
        String numStr = Integer.toString(123);
        System.out.println("String representation: " + numStr); // 输出: 123

        // 使用 compareTo 方法
        Integer num1 = 123;
        Integer num2 = 456;
        int comparison = num1.compareTo(num2);
        System.out.println("Comparison result: " + comparison); // 输出: -1
    }
}
2. Double

Double 类是 double 类型的包装类,提供了许多特有的方法。

  • static double parseDouble(String s): 将字符串解析为 double 类型。
  • static Double valueOf(String s): 返回一个保存指定值的 Double 对象。
  • static String toString(double d): 返回指定 double 的字符串表示形式。
  • int compareTo(Double anotherDouble): 比较两个 Double 对象。

示例:

public class DoubleExample {
    public static void main(String[] args) {
        // 使用 parseDouble 方法
        double num = Double.parseDouble("123.45");
        System.out.println("Parsed double: " + num); // 输出: 123.45

        // 使用 valueOf 方法
        Double numObj = Double.valueOf("123.45");
        System.out.println("Double object: " + numObj); // 输出: 123.45

        // 使用 toString 方法
        String numStr = Double.toString(123.45);
        System.out.println("String representation: " + numStr); // 输出: 123.45

        // 使用 compareTo 方法
        Double num1 = 123.45;
        Double num2 = 456.78;
        int comparison = num1.compareTo(num2);
        System.out.println("Comparison result: " + comparison); // 输出: -1
    }
}
3. Float

Float 类是 float 类型的包装类,提供了许多特有的方法。

  • static float parseFloat(String s): 将字符串解析为 float 类型。
  • static Float valueOf(String s): 返回一个保存指定值的 Float 对象。
  • static String toString(float f): 返回指定 float 的字符串表示形式。
  • int compareTo(Float anotherFloat): 比较两个 Float 对象。

示例:

public class FloatExample {
    public static void main(String[] args) {
        // 使用 parseFloat 方法
        float num = Float.parseFloat("123.45");
        System.out.println("Parsed float: " + num); // 输出: 123.45

        // 使用 valueOf 方法
        Float numObj = Float.valueOf("123.45");
        System.out.println("Float object: " + numObj); // 输出: 123.45

        // 使用 toString 方法
        String numStr = Float.toString(123.45f);
        System.out.println("String representation: " + numStr); // 输出: 123.45

        // 使用 compareTo 方法
        Float num1 = 123.45f;
        Float num2 = 456.78f;
        int comparison = num1.compareTo(num2);
        System.out.println("Comparison result: " + comparison); // 输出: -1
    }
}
4. Long

Long 类是 long 类型的包装类,提供了许多特有的方法。

  • static long parseLong(String s): 将字符串解析为 long 类型。
  • static Long valueOf(String s): 返回一个保存指定值的 Long 对象。
  • static String toString(long l): 返回指定 long 的字符串表示形式。
  • int compareTo(Long anotherLong): 比较两个 Long 对象。

示例:

public class LongExample {
    public static void main(String[] args) {
        // 使用 parseLong 方法
        long num = Long.parseLong("12345");
        System.out.println("Parsed long: " + num); // 输出: 12345

        // 使用 valueOf 方法
        Long numObj = Long.valueOf("12345");
        System.out.println("Long object: " + numObj); // 输出: 12345

        // 使用 toString 方法
        String numStr = Long.toString(12345L);
        System.out.println("String representation: " + numStr); // 输出: 12345

        // 使用 compareTo 方法
        Long num1 = 12345L;
        Long num2 = 45678L;
        int comparison = num1.compareTo(num2);
        System.out.println("Comparison result: " + comparison); // 输出: -1
    }
}
5. Short

Short 类是 short 类型的包装类,提供了许多特有的方法。

  • static short parseShort(String s): 将字符串解析为 short 类型。
  • static Short valueOf(String s): 返回一个保存指定值的 Short 对象。
  • static String toString(short s): 返回指定 short 的字符串表示形式。
  • int compareTo(Short anotherShort): 比较两个 Short 对象。

示例:

public class ShortExample {
    public static void main(String[] args) {
        // 使用 parseShort 方法
        short num = Short.parseShort("12345");
        System.out.println("Parsed short: " + num); // 输出: 12345

        // 使用 valueOf 方法
        Short numObj = Short.valueOf("12345");
        System.out.println("Short object: " + numObj); // 输出: 12345

        // 使用 toString 方法
        String numStr = Short.toString((short)12345);
        System.out.println("String representation: " + numStr); // 输出: 12345

        // 使用 compareTo 方法
        Short num1 = 12345;
        Short num2 = 4567;
        int comparison = num1.compareTo(num2);
        System.out.println("Comparison result: " + comparison); // 输出: 1
    }
}
6. Byte

Byte 类是 byte 类型的包装类,提供了许多特有的方法。

  • static byte parseByte(String s): 将字符串解析为 byte 类型。
  • static Byte valueOf(String s): 返回一个保存指定值的 Byte 对象。
  • static String toString(byte b): 返回指定 byte 的字符串表示形式。
  • int compareTo(Byte anotherByte): 比较两个 Byte 对象。

示例:

public class ByteExample {
    public static void main(String[] args) {
        // 使用 parseByte 方法
        byte num = Byte.parseByte("123");
        System.out.println("Parsed byte: " + num); // 输出: 123

        // 使用 valueOf 方法
        Byte numObj = Byte.valueOf("123");
        System.out.println("Byte object: " + numObj); // 输出: 123

        // 使用 toString 方法
        String numStr = Byte.toString((byte)123);
        System.out.println("String representation: " + numStr); // 输出: 123

        // 使用 compareTo 方法
        Byte num1 = 123;
        Byte num2 = 45;
        int comparison = num1.compareTo(num2);
        System.out.println("Comparison result: " + comparison); // 输出: 1
    }
}

总结

Number 类及其子类(Byte, Short, Integer, Long, Float, Double)提供了一系列用于数值转换和操作的方法。这些方法在处理不同数值类型之间的转换以及执行基本的数值操作时非常有用。通过详细了解和掌握这些方法,您可以在 Java 编程中更加高效地处理各种数值类型。

Byte

Java中的Byte类是基本数据类型byte的包装类,提供了一些方法来操作byte类型的数据。以下是Byte类中的所有方法,按照用途和功能进行分类,并附带测试用例。

1. 构造方法

  • Byte(byte value):创建一个表示指定byte值的Byte对象。
  • Byte(String s):创建一个表示指定字符串所表示的byte值的Byte对象。
Byte byte1 = new Byte((byte) 10);
Byte byte2 = new Byte("10");
System.out.println(byte1); // 输出: 10
System.out.println(byte2); // 输出: 10

2. 常量

  • static byte MIN_VALUE:表示byte类型的最小值,即-128。
  • static byte MAX_VALUE:表示byte类型的最大值,即127。
System.out.println(Byte.MIN_VALUE); // 输出: -128
System.out.println(Byte.MAX_VALUE); // 输出: 127

3. 静态方法

3.1 类型转换
  • static Byte valueOf(byte b):返回一个表示指定byte值的Byte实例。
  • static Byte valueOf(String s):返回一个表示指定字符串所表示的byte值的Byte实例。
  • static Byte valueOf(String s, int radix):返回一个表示指定字符串所表示的byte值的Byte实例,使用指定的基数。
Byte byte3 = Byte.valueOf((byte) 10);
Byte byte4 = Byte.valueOf("10");
Byte byte5 = Byte.valueOf("1010", 2); // 二进制的1010表示10
System.out.println(byte3); // 输出: 10
System.out.println(byte4); // 输出: 10
System.out.println(byte5); // 输出: 10
3.2 字符串转换
  • static String toString(byte b):返回一个表示指定byte值的字符串对象。
String str = Byte.toString((byte) 10);
System.out.println(str); // 输出: 10
3.3 解析字符串
  • static byte parseByte(String s):将字符串参数解析为有符号的byte值。
  • static byte parseByte(String s, int radix):将字符串参数解析为有符号的byte值,使用指定的基数。
byte b1 = Byte.parseByte("10");
byte b2 = Byte.parseByte("1010", 2); // 二进制的1010表示10
System.out.println(b1); // 输出: 10
System.out.println(b2); // 输出: 10

4. 实例方法

4.1 获取值
  • byte byteValue():返回此Byte对象的byte值。
Byte byte6 = new Byte((byte) 10);
byte b3 = byte6.byteValue();
System.out.println(b3); // 输出: 10
4.2 转换为其他基本类型
  • short shortValue():返回此Byte对象的short值。
  • int intValue():返回此Byte对象的int值。
  • long longValue():返回此Byte对象的long值。
  • float floatValue():返回此Byte对象的float值。
  • double doubleValue():返回此Byte对象的double值。
Byte byte7 = new Byte((byte) 10);
System.out.println(byte7.shortValue()); // 输出: 10
System.out.println(byte7.intValue());   // 输出: 10
System.out.println(byte7.longValue());  // 输出: 10
System.out.println(byte7.floatValue()); // 输出: 10.0
System.out.println(byte7.doubleValue()); // 输出: 10.0
4.3 比较
  • int compareTo(Byte anotherByte):比较两个Byte对象的数值。
Byte byte8 = new Byte((byte) 10);
Byte byte9 = new Byte((byte) 20);
System.out.println(byte8.compareTo(byte9)); // 输出: -10
4.4 相等性
  • boolean equals(Object obj):判断此Byte对象是否与指定的对象相等。
Byte byte10 = new Byte((byte) 10);
Byte byte11 = new Byte((byte) 10);
System.out.println(byte10.equals(byte11)); // 输出: true
4.5 哈希码
  • int hashCode():返回此Byte对象的哈希码。
Byte byte12 = new Byte((byte) 10);
System.out.println(byte12.hashCode()); // 输出: 10
4.6 字符串表示
  • String toString():返回一个表示此Byte值的字符串对象。
Byte byte13 = new Byte((byte) 10);
System.out.println(byte13.toString()); // 输出: 10

Short

Java中的Short类是基本数据类型short的包装类,它提供了许多方法来操作和处理short类型的数据。以下是Short类中的所有方法,按照用途和功能进行分类整理,并附带测试用例。

1. 构造方法

  • Short(short value):创建一个表示指定short值的Short对象。
  • Short(String s):创建一个表示指定字符串所表示的short值的Short对象。
Short short1 = new Short((short) 10);
Short short2 = new Short("10");
System.out.println(short1); // 输出: 10
System.out.println(short2); // 输出: 10

2. 常量

  • static short MIN_VALUE:表示short类型的最小值,即-32768。
  • static short MAX_VALUE:表示short类型的最大值,即32767。
System.out.println(Short.MIN_VALUE); // 输出: -32768
System.out.println(Short.MAX_VALUE); // 输出: 32767

3. 静态方法

3.1 类型转换
  • static Short valueOf(short s):返回一个表示指定short值的Short实例。
  • static Short valueOf(String s):返回一个表示指定字符串所表示的short值的Short实例。
  • static Short valueOf(String s, int radix):返回一个表示指定字符串所表示的short值的Short实例,使用指定的基数。
Short short3 = Short.valueOf((short) 10);
Short short4 = Short.valueOf("10");
Short short5 = Short.valueOf("1010", 2); // 二进制的1010表示10
System.out.println(short3); // 输出: 10
System.out.println(short4); // 输出: 10
System.out.println(short5); // 输出: 10
3.2 字符串转换
  • static String toString(short s):返回一个表示指定short值的字符串对象。
String str = Short.toString((short) 10);
System.out.println(str); // 输出: 10
3.3 解析字符串
  • static short parseShort(String s):将字符串参数解析为有符号的short值。
  • static short parseShort(String s, int radix):将字符串参数解析为有符号的short值,使用指定的基数。
short s1 = Short.parseShort("10");
short s2 = Short.parseShort("1010", 2); // 二进制的1010表示10
System.out.println(s1); // 输出: 10
System.out.println(s2); // 输出: 10
3.4 比较
  • static int compare(short x, short y):比较两个short值。
System.out.println(Short.compare((short) 10, (short) 20)); // 输出: -1
System.out.println(Short.compare((short) 20, (short) 10)); // 输出: 1
System.out.println(Short.compare((short) 10, (short) 10)); // 输出: 0
3.5 位操作
  • static int toUnsignedInt(short x):将short值转换为无符号的int表示。
  • static long toUnsignedLong(short x):将short值转换为无符号的long表示。
//补码:1000000000000001 → 原码:1111111111111111 → 输出: 65535(十进制)
System.out.println(Short.toUnsignedInt((short) -1)); 
System.out.println(Short.toUnsignedLong((short) -1)); // 输出: 65535

4. 实例方法

4.1 获取值
  • short shortValue():返回此Short对象的short值。
Short short6 = new Short((short) 10);
short s3 = short6.shortValue();
System.out.println(s3); // 输出: 10
4.2 转换为其他基本类型
  • byte byteValue():返回此Short对象的byte值。
  • int intValue():返回此Short对象的int值。
  • long longValue():返回此Short对象的long值。
  • float floatValue():返回此Short对象的float值。
  • double doubleValue():返回此Short对象的double值。
Short short7 = new Short((short) 10);
System.out.println(short7.byteValue());   // 输出: 10
System.out.println(short7.intValue());    // 输出: 10
System.out.println(short7.longValue());   // 输出: 10
System.out.println(short7.floatValue());  // 输出: 10.0
System.out.println(short7.doubleValue()); // 输出: 10.0
4.3 比较
  • int compareTo(Short anotherShort):比较两个Short对象的数值。
Short short8 = new Short((short) 10);
Short short9 = new Short((short) 20);
System.out.println(short8.compareTo(short9)); // 输出: -10
4.4 相等性
  • boolean equals(Object obj):判断此Short对象是否与指定的对象相等。
Short short10 = new Short((short) 10);
Short short11 = new Short((short) 10);
System.out.println(short10.equals(short11)); // 输出: true
4.5 哈希码
  • int hashCode():返回此Short对象的哈希码。
Short short12 = new Short((short) 10);
System.out.println(short12.hashCode()); // 输出: 10
4.6 字符串表示
  • String toString():返回一个表示此Short值的字符串对象。
Short short13 = new Short((short) 10);
System.out.println(short13.toString()); // 输出: 10

通过这些方法的分类和测试用例,你应该能够彻底理解并熟练运用Short类中的所有方法。

Integer

Java中的Integer类是基本数据类型int的包装类,提供了许多方法来操作和处理int类型的数据。以下是Integer类中的所有方法,按照用途和功能进行分类整理,并附带测试用例。

1. 构造方法

  • Integer(int value):创建一个表示指定int值的Integer对象。
  • Integer(String s):创建一个表示指定字符串所表示的int值的Integer对象。
Integer int1 = new Integer(10);
Integer int2 = new Integer("10");
System.out.println(int1); // 输出: 10
System.out.println(int2); // 输出: 10

2. 常量

  • static int MIN_VALUE:表示int类型的最小值,即-2^31。
  • static int MAX_VALUE:表示int类型的最大值,即2^31 - 1。
System.out.println(Integer.MIN_VALUE); // 输出: -2147483648
System.out.println(Integer.MAX_VALUE); // 输出: 2147483647

3. 静态方法

3.1 类型转换
  • static Integer valueOf(int i):返回一个表示指定int值的Integer实例。
  • static Integer valueOf(String s):返回一个表示指定字符串所表示的int值的Integer实例。
  • static Integer valueOf(String s, int radix):返回一个表示指定字符串所表示的int值的Integer实例,使用指定的基数。
Integer int3 = Integer.valueOf(10);
Integer int4 = Integer.valueOf("10");
Integer int5 = Integer.valueOf("1010", 2); // 二进制的1010表示10
System.out.println(int3); // 输出: 10
System.out.println(int4); // 输出: 10
System.out.println(int5); // 输出: 10
3.2 字符串转换
  • static String toString(int i):返回一个表示指定int值的字符串对象。
  • static String toHexString(int i):返回指定int值的十六进制字符串表示形式。
  • static String toOctalString(int i):返回指定int值的八进制字符串表示形式。
  • static String toBinaryString(int i):返回指定int值的二进制字符串表示形式。
  • static String toUnsignedString(int i):返回指定int值的无符号字符串表示形式,基数为10。
String str1 = Integer.toString(10);
String str2 = Integer.toHexString(10);
String str3 = Integer.toOctalString(10);
String str4 = Integer.toBinaryString(10);
String str5 = Integer.toUnsignedString(10);
System.out.println(str1); // 输出: 10
System.out.println(str2); // 输出: a
System.out.println(str3); // 输出: 12
System.out.println(str4); // 输出: 1010
System.out.println(str5); // 输出: 10
3.3 解析字符串
  • static int parseInt(String s):将字符串参数解析为有符号的int值。
  • static int parseInt(String s, int radix):将字符串参数解析为有符号的int值,使用指定的基数。
  • static int parseUnsignedInt(String s):将字符串参数解析为无符号的int值。
  • static int parseUnsignedInt(String s, int radix):将字符串参数解析为无符号的int值,使用指定的基数。
int i1 = Integer.parseInt("10");
int i2 = Integer.parseInt("1010", 2); // 二进制的1010表示10
int i3 = Integer.parseUnsignedInt("10");
int i4 = Integer.parseUnsignedInt("1010", 2); // 二进制的1010表示10
System.out.println(i1); // 输出: 10
System.out.println(i2); // 输出: 10
System.out.println(i3); // 输出: 10
System.out.println(i4); // 输出: 10
3.4 比较
  • static int compare(int x, int y):比较两个int值。
  • static int compareUnsigned(int x, int y):比较两个无符号的int值。
System.out.println(Integer.compare(10, 20)); // 输出: -1
System.out.println(Integer.compare(20, 10)); // 输出: 1
System.out.println(Integer.compare(10, 10)); // 输出: 0
System.out.println(Integer.compareUnsigned(10, 20)); // 输出: -1
System.out.println(Integer.compareUnsigned(20, 10)); // 输出: 1
System.out.println(Integer.compareUnsigned(10, 10)); // 输出: 0
3.5 位操作
  • static int highestOneBit(int i):返回指定int值的最高位的一个比特。
  • static int lowestOneBit(int i):返回指定int值的最低位的一个比特。
  • static int bitCount(int i):返回指定int值的二进制补码表示形式中,1的个数。
  • static int numberOfLeadingZeros(int i):返回指定int值的二进制补码表示形式中,前导零的个数。
  • static int numberOfTrailingZeros(int i):返回指定int值的二进制补码表示形式中,末尾零的个数。
  • static int rotateLeft(int i, int distance):返回通过将指定int值左旋转指定位数后的值。
  • static int rotateRight(int i, int distance):返回通过将指定int值右旋转指定位数后的值。
  • static int reverse(int i):返回指定int值的二进制补码表示形式的反转位排序后的值。
  • static int signum(int i):返回指定int值的符号函数。
  • static int reverseBytes(int i):返回通过将指定int值的字节顺序反转后的值。
System.out.println(Integer.highestOneBit(10)); // 输出: 8
System.out.println(Integer.lowestOneBit(10));  // 输出: 2
System.out.println(Integer.bitCount(10));      // 输出: 2
System.out.println(Integer.numberOfLeadingZeros(10)); // 输出: 28
System.out.println(Integer.numberOfTrailingZeros(10)); // 输出: 1
System.out.println(Integer.rotateLeft(10, 2)); // 输出: 40
System.out.println(Integer.rotateRight(10, 2)); // 输出: -1073741822
System.out.println(Integer.reverse(10));      // 输出: 1342177280
System.out.println(Integer.signum(10));       // 输出: 1
System.out.println(Integer.reverseBytes(10)); // 输出: 167772160

4. 实例方法

4.1 获取值
  • int intValue():返回此Integer对象的int值。
Integer int6 = new Integer(10);
int i5 = int6.intValue();
System.out.println(i5); // 输出: 10
4.2 转换为其他基本类型
  • byte byteValue():返回此Integer对象的byte值。
  • short shortValue():返回此Integer对象的short值。
  • long longValue():返回此Integer对象的long值。
  • float floatValue():返回此Integer对象的float值。
  • double doubleValue():返回此Integer对象的double值。
Integer int7 = new Integer(10);
System.out.println(int7.byteValue());   // 输出: 10
System.out.println(int7.shortValue());  // 输出: 10
System.out.println(int7.longValue());   // 输出: 10
System.out.println(int7.floatValue());  // 输出: 10.0
System.out.println(int7.doubleValue()); // 输出: 10.0
4.3 比较
  • int compareTo(Integer anotherInteger):比较两个Integer对象的数值。
Integer int8 = new Integer(10);
Integer int9 = new Integer(20);
System.out.println(int8.compareTo(int9)); // 输出: -10
4.4 相等性
  • boolean equals(Object obj):判断此Integer对象是否与指定的对象相等。
Integer int10 = new Integer(10);
Integer int11 = new Integer(10);
System.out.println(int10.equals(int11)); // 输出: true
4.5 哈希码
  • int hashCode():返回此Integer对象的哈希码。
Integer int12 = new Integer(10);
System.out.println(int12.hashCode()); // 输出: 10
4.6 字符串表示
  • String toString():返回一个表示此Integer值的字符串对象。
Integer int13 = new Integer(10);
System.out.println(int13.toString()); // 输出: 10

通过这些方法的分类和测试用例,你应该能够彻底理解并熟练运用Integer类中的所有方法。

Long

Java中的Long类是基本数据类型long的包装类,它提供了许多方法来操作和处理long类型的数据。以下是Long类中的所有方法,按照用途和功能进行分类整理,并附带测试用例。

1. 构造方法

  • Long(long value):创建一个表示指定long值的Long对象。
  • Long(String s):创建一个表示指定字符串所表示的long值的Long对象。
Long long1 = new Long(10L);
Long long2 = new Long("10");
System.out.println(long1); // 输出: 10
System.out.println(long2); // 输出: 10

2. 常量

  • static long MIN_VALUE:表示long类型的最小值,即-2^63。
  • static long MAX_VALUE:表示long类型的最大值,即2^63 - 1。
System.out.println(Long.MIN_VALUE); // 输出: -9223372036854775808
System.out.println(Long.MAX_VALUE); // 输出: 9223372036854775807

3. 静态方法

3.1 类型转换
  • static Long valueOf(long l):返回一个表示指定long值的Long实例。
  • static Long valueOf(String s):返回一个表示指定字符串所表示的long值的Long实例。
  • static Long valueOf(String s, int radix):返回一个表示指定字符串所表示的long值的Long实例,使用指定的基数。
Long long3 = Long.valueOf(10L);
Long long4 = Long.valueOf("10");
Long long5 = Long.valueOf("1010", 2); // 二进制的1010表示10
System.out.println(long3); // 输出: 10
System.out.println(long4); // 输出: 10
System.out.println(long5); // 输出: 10
3.2 字符串转换
  • static String toString(long l):返回一个表示指定long值的字符串对象。
  • static String toUnsignedString(long l):返回指定long值的无符号字符串表示形式,基数为10。
  • static String toHexString(long l):返回指定long值的十六进制字符串表示形式。
  • static String toOctalString(long l):返回指定long值的八进制字符串表示形式。
  • static String toBinaryString(long l):返回指定long值的二进制字符串表示形式。
String str1 = Long.toString(10L);
String str2 = Long.toHexString(10L);
String str3 = Long.toOctalString(10L);
String str4 = Long.toBinaryString(10L);
String str5 = Long.toUnsignedString(10L);
System.out.println(str1); // 输出: 10
System.out.println(str2); // 输出: a
System.out.println(str3); // 输出: 12
System.out.println(str4); // 输出: 1010
System.out.println(str5); // 输出: 10
3.3 解析字符串
  • static long parseLong(String s):将字符串参数解析为有符号的long值。
  • static long parseLong(String s, int radix):将字符串参数解析为有符号的long值,使用指定的基数。
  • static long parseUnsignedLong(String s):将字符串参数解析为无符号的long值。
  • static long parseUnsignedLong(String s, int radix):将字符串参数解析为无符号的long值,使用指定的基数。
long l1 = Long.parseLong("10");
long l2 = Long.parseLong("1010", 2); // 二进制的1010表示10
long l3 = Long.parseUnsignedLong("10");
long l4 = Long.parseUnsignedLong("1010", 2); // 二进制的1010表示10
System.out.println(l1); // 输出: 10
System.out.println(l2); // 输出: 10
System.out.println(l3); // 输出: 10
System.out.println(l4); // 输出: 10
3.4 比较
  • static int compare(long x, long y):比较两个long值。
  • static int compareUnsigned(long x, long y):比较两个无符号的long值。
System.out.println(Long.compare(10L, 20L)); // 输出: -1
System.out.println(Long.compare(20L, 10L)); // 输出: 1
System.out.println(Long.compare(10L, 10L)); // 输出: 0
System.out.println(Long.compareUnsigned(10L, 20L)); // 输出: -1
System.out.println(Long.compareUnsigned(20L, 10L)); // 输出: 1
System.out.println(Long.compareUnsigned(10L, 10L)); // 输出: 0
3.5 位操作
  • static long highestOneBit(long l):返回指定long值的最高位的一个比特。
  • static long lowestOneBit(long l):返回指定long值的最低位的一个比特。
  • static int bitCount(long l):返回指定long值的二进制补码表示形式中,1的个数。
  • static int numberOfLeadingZeros(long l):返回指定long值的二进制补码表示形式中,前导零的个数。
  • static int numberOfTrailingZeros(long l):返回指定long值的二进制补码表示形式中,末尾零的个数。
  • static long rotateLeft(long l, int distance):返回通过将指定long值左旋转指定位数后的值。
  • static long rotateRight(long l, int distance):返回通过将指定long值右旋转指定位数后的值。
  • static long reverse(long l):返回指定long值的二进制补码表示形式的反转位排序后的值。
  • static int signum(long l):返回指定long值的符号函数。
  • static long reverseBytes(long l):返回通过将指定long值的字节顺序反转后的值。
System.out.println(Long.highestOneBit(10L)); // 输出: 8
System.out.println(Long.lowestOneBit(10L));  // 输出: 2
System.out.println(Long.bitCount(10L));      // 输出: 2
System.out.println(Long.numberOfLeadingZeros(10L)); // 输出: 60
System.out.println(Long.numberOfTrailingZeros(10L)); // 输出: 1
System.out.println(Long.rotateLeft(10L, 2)); // 输出: 40
System.out.println(Long.rotateRight(10L, 2)); // 输出: 4611686018427387902
System.out.println(Long.reverse(10L));      // 输出: 576460752303423488
System.out.println(Long.signum(10L));       // 输出: 1
System.out.println(Long.reverseBytes(10L)); // 输出: 720575940379279360

4. 实例方法

4.1 获取值
  • long longValue():返回此Long对象的long值。
Long long6 = new Long(10L);
long l5 = long6.longValue();
System.out.println(l5); // 输出: 10
4.2 转换为其他基本类型
  • byte byteValue():返回此Long对象的byte值。
  • short shortValue():返回此Long对象的short值。
  • int intValue():返回此Long对象的int值。
  • float floatValue():返回此Long对象的float值。
  • double doubleValue():返回此Long对象的double值。
Long long7 = new Long(10L);
System.out.println(long7.byteValue());   // 输出: 10
System.out.println(long7.shortValue());  // 输出: 10
System.out.println(long7.intValue());    // 输出: 10
System.out.println(long7.floatValue());  // 输出: 10.0
System.out.println(long7.doubleValue()); // 输出: 10.0
4.3 比较
  • int compareTo(Long anotherLong):比较两个Long对象的数值。
Long long8 = new Long(10L);
Long long9 = new Long(20L);
System.out.println(long8.compareTo(long9)); // 输出: -10
4.4 相等性
  • boolean equals(Object obj):判断此Long对象是否与指定的对象相等。
Long long10 = new Long(10L);
Long long11 = new Long(10L);
System.out.println(long10.equals(long11)); // 输出: true
4.5 哈希码
  • int hashCode():返回此Long对象的哈希码。
Long long12 = new Long(10L);
System.out.println(long12.hashCode()); // 输出: 10
4.6 字符串表示
  • String toString():返回一个表示此Long值的字符串对象。
Long long13 = new Long(10L);
System.out.println(long13.toString()); // 输出: 10

通过这些方法的分类和测试用例,你应该能够彻底理解并熟练运用Long类中的所有方法。

Float

Java中的Float类是基本数据类型float的包装类,它提供了许多方法来操作和处理float类型的数据。以下是Float类中的所有方法,按照用途和功能进行分类整理,并附带测试用例。

1. 构造方法

  • Float(float value):创建一个表示指定float值的Float对象。
  • Float(double value):创建一个表示指定double值的Float对象。
  • Float(String s):创建一个表示指定字符串所表示的float值的Float对象。
Float float1 = new Float(10.5f);
Float float2 = new Float(10.5);
Float float3 = new Float("10.5");
System.out.println(float1); // 输出: 10.5
System.out.println(float2); // 输出: 10.5
System.out.println(float3); // 输出: 10.5

2. 常量

  • static float MIN_VALUE:表示float类型的最小正非零值,即1.4E-45
  • static float MAX_VALUE:表示float类型的最大值,即3.4028235E38
  • static float MIN_NORMAL:表示float类型的最小正标准值,即1.17549435E-38
  • static float NaN:表示非数字值(Not a Number)。
  • static float POSITIVE_INFINITY:表示正无穷大。
  • static float NEGATIVE_INFINITY:表示负无穷大。
System.out.println(Float.MIN_VALUE);          // 输出: 1.4E-45
System.out.println(Float.MAX_VALUE);          // 输出: 3.4028235E38
System.out.println(Float.MIN_NORMAL);         // 输出: 1.17549435E-38
System.out.println(Float.NaN);                // 输出: NaN
System.out.println(Float.POSITIVE_INFINITY);  // 输出: Infinity
System.out.println(Float.NEGATIVE_INFINITY);  // 输出: -Infinity

3. 静态方法

3.1 类型转换
  • static Float valueOf(float f):返回一个表示指定float值的Float实例。
  • static Float valueOf(String s):返回一个表示指定字符串所表示的float值的Float实例。
Float float4 = Float.valueOf(10.5f);
Float float5 = Float.valueOf("10.5");
System.out.println(float4); // 输出: 10.5
System.out.println(float5); // 输出: 10.5
3.2 字符串转换
  • static String toString(float f):返回一个表示指定float值的字符串对象。
  • static String toHexString(float f):返回一个表示指定float值的十六进制字符串表示形式。
String str1 = Float.toString(10.5f);
String str2 = Float.toHexString(10.5f);
System.out.println(str1); // 输出: 10.5
System.out.println(str2); // 输出: 0x1.500000p3
3.3 解析字符串
  • static float parseFloat(String s):将字符串参数解析为有符号的float值。
float f1 = Float.parseFloat("10.5");
System.out.println(f1); // 输出: 10.5
3.4 比较
  • static int compare(float f1, float f2):比较两个float值。
  • static boolean isNaN(float v):判断指定的float值是否是非数字值(NaN)。
  • static boolean isInfinite(float v):判断指定的float值是否是无穷大。
  • static boolean isFinite(float f):判断指定的float值是否是有限的。
System.out.println(Float.compare(10.5f, 20.5f)); // 输出: -1
System.out.println(Float.compare(20.5f, 10.5f)); // 输出: 1
System.out.println(Float.compare(10.5f, 10.5f)); // 输出: 0
System.out.println(Float.isNaN(Float.NaN)); // 输出: true
System.out.println(Float.isInfinite(Float.POSITIVE_INFINITY)); // 输出: true
System.out.println(Float.isFinite(10.5f)); // 输出: true
3.5 位操作
  • static int floatToIntBits(float value):将float值转换为与相同二进制表示的int值。
  • static int floatToRawIntBits(float value):将float值转换为原始二进制表示的int值。
  • static float intBitsToFloat(int bits):将int值转换为与相同二进制表示的float值。
int bits = Float.floatToIntBits(10.5f);
int rawBits = Float.floatToRawIntBits(10.5f);
float f2 = Float.intBitsToFloat(bits);
System.out.println(bits);       // 输出: 1095761920
System.out.println(rawBits);    // 输出: 1095761920
System.out.println(f2);         // 输出: 10.5

4. 实例方法

4.1 获取值
  • float floatValue():返回此Float对象的float值。
Float float6 = new Float(10.5f);
float f3 = float6.floatValue();
System.out.println(f3); // 输出: 10.5
4.2 转换为其他基本类型
  • byte byteValue():返回此Float对象的byte值。
  • short shortValue():返回此Float对象的short值。
  • int intValue():返回此Float对象的int值。
  • long longValue():返回此Float对象的long值。
  • double doubleValue():返回此Float对象的double值。
Float float7 = new Float(10.5f);
System.out.println(float7.byteValue());   // 输出: 10
System.out.println(float7.shortValue());  // 输出: 10
System.out.println(float7.intValue());    // 输出: 10
System.out.println(float7.longValue());   // 输出: 10
System.out.println(float7.doubleValue()); // 输出: 10.5
4.3 比较
  • int compareTo(Float anotherFloat):比较两个Float对象的数值。
Float float8 = new Float(10.5f);
Float float9 = new Float(20.5f);
System.out.println(float8.compareTo(float9)); // 输出: -1
4.4 相等性
  • boolean equals(Object obj):判断此Float对象是否与指定的对象相等。
Float float10 = new Float(10.5f);
Float float11 = new Float(10.5f);
System.out.println(float10.equals(float11)); // 输出: true
4.5 哈希码
  • int hashCode():返回此Float对象的哈希码。
Float float12 = new Float(10.5f);
System.out.println(float12.hashCode()); // 输出: 1095761920
4.6 字符串表示
  • String toString():返回一个表示此Float值的字符串对象。
Float float13 = new Float(10.5f);
System.out.println(float13.toString()); // 输出: 10.5

通过这些方法的分类和测试用例,你应该能够彻底理解并熟练运用Float类中的所有方法。

Double

好的,Java中的Double类是基本数据类型double的包装类,它提供了许多方法来操作和处理double类型的数据。以下是Double类中的所有方法,按照用途和功能进行分类整理,并附带测试用例。

1. 构造方法

  • Double(double value):创建一个表示指定double值的Double对象。
  • Double(String s):创建一个表示指定字符串所表示的double值的Double对象。
Double double1 = new Double(10.5);
Double double2 = new Double("10.5");
System.out.println(double1); // 输出: 10.5
System.out.println(double2); // 输出: 10.5

2. 常量

  • static double MIN_VALUE:表示double类型的最小正非零值,即4.9E-324
  • static double MAX_VALUE:表示double类型的最大值,即1.7976931348623157E308
  • static double MIN_NORMAL:表示double类型的最小正标准值,即2.2250738585072014E-308
  • static double NaN:表示非数字值(Not a Number)。
  • static double POSITIVE_INFINITY:表示正无穷大。
  • static double NEGATIVE_INFINITY:表示负无穷大。
System.out.println(Double.MIN_VALUE);          // 输出: 4.9E-324
System.out.println(Double.MAX_VALUE);          // 输出: 1.7976931348623157E308
System.out.println(Double.MIN_NORMAL);         // 输出: 2.2250738585072014E-308
System.out.println(Double.NaN);                // 输出: NaN
System.out.println(Double.POSITIVE_INFINITY);  // 输出: Infinity
System.out.println(Double.NEGATIVE_INFINITY);  // 输出: -Infinity

3. 静态方法

3.1 类型转换
  • static Double valueOf(double d):返回一个表示指定double值的Double实例。
  • static Double valueOf(String s):返回一个表示指定字符串所表示的double值的Double实例。
Double double3 = Double.valueOf(10.5);
Double double4 = Double.valueOf("10.5");
System.out.println(double3); // 输出: 10.5
System.out.println(double4); // 输出: 10.5
3.2 字符串转换
  • static String toString(double d):返回一个表示指定double值的字符串对象。
  • static String toHexString(double d):返回一个表示指定double值的十六进制字符串表示形式。
String str1 = Double.toString(10.5);
String str2 = Double.toHexString(10.5);
System.out.println(str1); // 输出: 10.5
System.out.println(str2); // 输出: 0x1.5000000000000p3
3.3 解析字符串
  • static double parseDouble(String s):将字符串参数解析为有符号的double值。
double d1 = Double.parseDouble("10.5");
System.out.println(d1); // 输出: 10.5
3.4 比较
  • static int compare(double d1, double d2):比较两个double值。
  • static boolean isNaN(double v):判断指定的double值是否是非数字值(NaN)。
  • static boolean isInfinite(double v):判断指定的double值是否是无穷大。
  • static boolean isFinite(double d):判断指定的double值是否是有限的。
System.out.println(Double.compare(10.5, 20.5));  // 输出: -1
System.out.println(Double.compare(20.5, 10.5));  // 输出: 1
System.out.println(Double.compare(10.5, 10.5));  // 输出: 0
System.out.println(Double.isNaN(Double.NaN));    // 输出: true
System.out.println(Double.isInfinite(Double.POSITIVE_INFINITY)); // 输出: true
System.out.println(Double.isFinite(10.5));       // 输出: true
3.5 位操作
  • static long doubleToLongBits(double value):将double值转换为与相同二进制表示的long值。
  • static long doubleToRawLongBits(double value):将double值转换为原始二进制表示的long值。
  • static double longBitsToDouble(long bits):将long值转换为与相同二进制表示的double值。
long bits = Double.doubleToLongBits(10.5);
long rawBits = Double.doubleToRawLongBits(10.5);
double d2 = Double.longBitsToDouble(bits);
System.out.println(bits);       // 输出: 4621819117588971520
System.out.println(rawBits);    // 输出: 4621819117588971520
System.out.println(d2);         // 输出: 10.5

4. 实例方法

4.1 获取值
  • double doubleValue():返回此Double对象的double值。
Double double5 = new Double(10.5);
double d3 = double5.doubleValue();
System.out.println(d3); // 输出: 10.5
4.2 转换为其他基本类型
  • byte byteValue():返回此Double对象的byte值。
  • short shortValue():返回此Double对象的short值。
  • int intValue():返回此Double对象的int值。
  • long longValue():返回此Double对象的long值。
  • float floatValue():返回此Double对象的float值。
Double double6 = new Double(10.5);
System.out.println(double6.byteValue());   // 输出: 10
System.out.println(double6.shortValue());  // 输出: 10
System.out.println(double6.intValue());    // 输出: 10
System.out.println(double6.longValue());   // 输出: 10
System.out.println(double6.floatValue());  // 输出: 10.5
4.3 比较
  • int compareTo(Double anotherDouble):比较两个Double对象的数值。
Double double7 = new Double(10.5);
Double double8 = new Double(20.5);
System.out.println(double7.compareTo(double8)); // 输出: -1
4.4 相等性
  • boolean equals(Object obj):判断此Double对象是否与指定的对象相等。
Double double9 = new Double(10.5);
Double double10 = new Double(10.5);
System.out.println(double9.equals(double10)); // 输出: true
4.5 哈希码
  • int hashCode():返回此Double对象的哈希码。
Double double11 = new Double(10.5);
System.out.println(double11.hashCode()); // 输出: 1076101120
4.6 字符串表示
  • String toString():返回一个表示此Double值的字符串对象。
Double double12 = new Double(10.5);
System.out.println(double12.toString()); // 输出: 10.5

通过这些方法的分类和测试用例,你应该能够彻底理解并熟练运用Double类中的所有方法。

Character

Java中的Character类是基本数据类型char的包装类,它提供了许多方法来操作和处理char类型的数据。以下是Character类中的所有方法,按照用途和功能进行分类整理,并附带测试用例。

1. 构造方法

  • Character(char value):创建一个表示指定char值的Character对象。
Character char1 = new Character('a');
System.out.println(char1); // 输出: a

2. 常量

  • static char MIN_VALUE:表示char类型的最小值,即\u0000
  • static char MAX_VALUE:表示char类型的最大值,即\uFFFF
  • static int MIN_RADIX:表示最小的基数,即2。
  • static int MAX_RADIX:表示最大的基数,即36。
  • static byte UNASSIGNED:指示该字符没有分配。
  • static byte DECIMAL_DIGIT_NUMBER:指示该字符是一个十进制数字。
  • static byte LETTER_NUMBER:指示该字符是一个字母数字。
  • static byte OTHER_NUMBER:指示该字符是其他类型的数字。
  • static byte LOWERCASE_LETTER:指示该字符是一个小写字母。
  • static byte UPPERCASE_LETTER:指示该字符是一个大写字母。
  • static byte TITLECASE_LETTER:指示该字符是一个标题字母。
  • static byte MODIFIER_LETTER:指示该字符是一个修饰字母。
  • static byte OTHER_LETTER:指示该字符是其他类型的字母。
  • static byte NON_SPACING_MARK:指示该字符是一个非间隔标记。
  • static byte ENCLOSING_MARK:指示该字符是一个封闭标记。
  • static byte COMBINING_SPACING_MARK:指示该字符是一个组合间隔标记。
  • static byte CONTROL:指示该字符是一个控制字符。
  • static byte FORMAT:指示该字符是一个格式字符。
  • static byte PRIVATE_USE:指示该字符是一个私用字符。
  • static byte SURROGATE:指示该字符是一个代理字符。
  • static byte DASH_PUNCTUATION:指示该字符是一个短划线标点。
  • static byte START_PUNCTUATION:指示该字符是一个起始标点。
  • static byte END_PUNCTUATION:指示该字符是一个结束标点。
  • static byte CONNECTOR_PUNCTUATION:指示该字符是一个连接符。
  • static byte OTHER_PUNCTUATION:指示该字符是其他类型的标点。
  • static byte MATH_SYMBOL:指示该字符是一个数学符号。
  • static byte CURRENCY_SYMBOL:指示该字符是一个货币符号。
  • static byte MODIFIER_SYMBOL:指示该字符是一个修饰符号。
  • static byte OTHER_SYMBOL:指示该字符是其他类型的符号。
  • static byte INITIAL_QUOTE_PUNCTUATION:指示该字符是一个初始引号标点。
  • static byte FINAL_QUOTE_PUNCTUATION:指示该字符是一个结束引号标点.
  • static byte DIRECTIONALITY_UNDEFINED:指示字符的双向类别未定义。
System.out.println(Character.MIN_VALUE); // 输出: 
System.out.println(Character.MAX_VALUE); // 输出: 
System.out.println(Character.MIN_RADIX); // 输出: 2
System.out.println(Character.MAX_RADIX); // 输出: 36

3. 静态方法

3.1 类型转换和字符相关
  • static Character valueOf(char c):返回一个表示指定char值的Character实例。
  • static char forDigit(int digit, int radix):根据指定的基数将数字转换为字符。
Character char2 = Character.valueOf('b');
System.out.println(char2); // 输出: b

char charForDigit = Character.forDigit(10, 16);
System.out.println(charForDigit); // 输出: a
3.2 判断字符类型
  • static boolean isDigit(char ch):判断指定的字符是否是一个数字。
  • static boolean isLetter(char ch):判断指定的字符是否是一个字母。
  • static boolean isLetterOrDigit(char ch):判断指定的字符是否是一个字母或数字。
  • static boolean isLowerCase(char ch):判断指定的字符是否是小写字母。
  • static boolean isUpperCase(char ch):判断指定的字符是否是大写字母。
  • static boolean isTitleCase(char ch):判断指定的字符是否是标题字母。
  • static boolean isWhitespace(char ch):判断指定的字符是否是空白字符。
  • static boolean isSpaceChar(char ch):判断指定的字符是否是一个空格字符。
  • static boolean isISOControl(char ch):判断指定的字符是否是ISO控制字符。
  • static boolean isJavaIdentifierStart(char ch):判断指定的字符是否可以作为Java标识符的首字符。
  • static boolean isJavaIdentifierPart(char ch):判断指定的字符是否可以作为Java标识符的一部分。
  • static boolean isUnicodeIdentifierStart(char ch):判断指定的字符是否可以作为Unicode标识符的首字符。
  • static boolean isUnicodeIdentifierPart(char ch):判断指定的字符是否可以作为Unicode标识符的一部分。
  • static boolean isIdentifierIgnorable(char ch):判断指定的字符是否是可忽略的标识符。
System.out.println(Character.isDigit('1')); // 输出: true
System.out.println(Character.isLetter('a')); // 输出: true
System.out.println(Character.isLetterOrDigit('1')); // 输出: true
System.out.println(Character.isLowerCase('a')); // 输出: true
System.out.println(Character.isUpperCase('A')); // 输出: true
System.out.println(Character.isTitleCase('A')); // 输出: false
System.out.println(Character.isWhitespace(' ')); // 输出: true
System.out.println(Character.isSpaceChar(' ')); // 输出: true
System.out.println(Character.isISOControl('\n')); // 输出: true
System.out.println(Character.isJavaIdentifierStart('_')); // 输出: true
System.out.println(Character.isJavaIdentifierPart('1')); // 输出: true
System.out.println(Character.isUnicodeIdentifierStart('a')); // 输出: true
System.out.println(Character.isUnicodeIdentifierPart('1')); // 输出: true
System.out.println(Character.isIdentifierIgnorable('\u0000')); // 输出: true
3.3 获取字符属性
  • static int getNumericValue(char ch):返回指定字符的数值。
  • static byte getDirectionality(char ch):返回指定字符的双向类别。
  • static int getType(char ch):返回指定字符的类型。
  • static char getDirectionality(char ch):返回指定字符的双向属性。
System.out.println(Character.getNumericValue('9')); // 输出: 9
System.out.println(Character.getDirectionality('a')); // 输出: 0
System.out.println(Character.getType('a')); // 输出: 2
3.4 转换字符
  • static char toLowerCase(char ch):将字符转换为小写。
  • static char toUpperCase(char ch):将字符转换为大写。
  • static char toTitleCase(char ch):将字符转换为标题字符。
  • static int toCodePoint(char high, char low):返回由代理字符对表示的代码点。
  • static int codePointAt(CharSequence seq, int index):返回指定索引处的Unicode代码点。
  • static int codePointBefore(CharSequence seq, int index):返回指定索引之前的Unicode代码点。
  • static int codePointCount(CharSequence seq, int beginIndex, int endIndex):返回指定文本范围中的Unicode代码点数。
  • static int offsetByCodePoints(CharSequence seq, int index, int codePointOffset):返回从指定索引开始偏移指定代码点的索引。
System.out.println(Character.toLowerCase('A')); // 输出: a
System.out.println(Character.toUpperCase('a')); // 输出: A
System.out.println(Character.toTitleCase('a')); // 输出: A

int codePoint = Character.toCodePoint('D', 'C');
System.out.println(codePoint); // 输出: 57475

String text = "hello";
System.out.println(Character.codePointAt(text, 2)); // 输出: 108
System.out.println(Character.codePointBefore(text, 2)); // 输出: 101
System.out.println(Character.codePointCount(text, 0, text.length())); // 输出: 5
System.out.println(Character.offsetByCodePoints(text, 1, 3)); // 输出: 4
3.5 代理字符相关
  • static boolean isHighSurrogate(char ch):判断指定的字符是否是高代理。
  • static boolean isLowSurrogate(char ch):判断指定的字符是否是低代理。
  • static boolean isSurrogatePair(char high, char low):判断两个字符是否形成代理对。
  • static boolean isSupplementaryCodePoint(int codePoint):判断指定的代码点是否是补充代码点。
System.out.println(Character.isHighSurrogate('\uD800')); // 输出: true
System.out.println(Character.isLowSurrogate('\uDC00')); // 输出: true
System.out.println(Character.isSurrogatePair('\uD800', '\uDC00')); // 输出: true
System.out.println(Character.isSupplementaryCodePoint(0x1D11E)); // 输出: true

4. 实例方法

4.1 获取值
  • char charValue():返回此Character对象的char值。
Character char3 = new Character('c');
char c1 = char3.charValue();
System.out.println(c1); // 输出: c
4.2 比较
  • int compareTo(Character anotherCharacter):比较两个Character对象的数值。
  • boolean equals(Object obj):判断此Character对象是否与指定的对象相等。
  • int hashCode():返回此Character对象的哈希码。
Character char4 = new Character('d');
Character char5 = new Character('e');
System.out.println(char4.compareTo(char5)); // 输出: -1
System.out.println(char4.equals(char5)); // 输出: false
System.out.println(char4.hashCode()); // 输出: 100
4.3 字符串表示
  • String toString():返回一个表示此Character值的字符串对象。
Character char6 = new Character('f');
System.out.println(char6.toString()); // 输出: f

通过这些方法的分类和测试用例,你应该能够彻底理解并熟练运用Character类中的所有方法。

Boolean

当然,Java中的Boolean类是基本数据类型boolean的包装类,它提供了丰富的方法来操作和处理boolean类型的数据。以下是Boolean类中的所有方法,按照用途和功能进行分类整理,并附带测试用例。

1. 构造方法

  • Boolean(boolean value):创建一个表示指定boolean值的Boolean对象。
  • Boolean(String s):创建一个表示指定字符串所表示的boolean值的Boolean对象。
Boolean bool1 = new Boolean(true);
Boolean bool2 = new Boolean("true");
Boolean bool3 = new Boolean("false"); 
Boolean bool4 = new Boolean("random");
System.out.println(bool1); // 输出: true
System.out.println(bool2); // 输出: true
System.out.println(bool3); // 输出: false
System.out.println(bool4); // 输出: false 对于"random"这样的字符串,Boolean的构造函数会将其视为false。

2. 常量

  • static Boolean TRUE:表示trueBoolean对象。
  • static Boolean FALSE:表示falseBoolean对象。
System.out.println(Boolean.TRUE); // 输出: true
System.out.println(Boolean.FALSE); // 输出: false

3. 静态方法

3.1 类型转换
  • static Boolean valueOf(boolean b):返回一个表示指定boolean值的Boolean实例。
  • static Boolean valueOf(String s):返回一个表示指定字符串所表示的boolean值的Boolean实例。
Boolean bool5 = Boolean.valueOf(true);
Boolean bool6 = Boolean.valueOf("true");
Boolean bool7 = Boolean.valueOf("false");
Boolean bool8 = Boolean.valueOf("random");
System.out.println(bool5); // 输出: true
System.out.println(bool6); // 输出: true
System.out.println(bool7); // 输出: false
System.out.println(bool8); // 输出: false 对于"random"这样的字符串,Boolean的valueOf会将其视为false。
3.2 布尔值解析
  • static boolean parseBoolean(String s):将字符串参数解析为有符号的boolean值。
boolean b1 = Boolean.parseBoolean("true");
boolean b2 = Boolean.parseBoolean("false");
boolean b3 = Boolean.parseBoolean("random");
System.out.println(b1); // 输出: true
System.out.println(b2); // 输出: false
System.out.println(b3); // 输出: false 对于"random"这样的字符串,parseBoolean会将其视为false。
3.3 比较
  • static int compare(boolean x, boolean y):比较两个boolean值。
  • boolean equals(Object obj):判断此Boolean对象是否与指定的对象相等。
System.out.println(Boolean.compare(true, false)); // 输出: 1
System.out.println(Boolean.compare(false, true)); // 输出: -1
System.out.println(Boolean.compare(true, true)); // 输出: 0

Boolean bool9 = new Boolean(true);
Boolean bool10 = new Boolean(false);
System.out.println(bool9.equals(bool10)); // 输出: false
System.out.println(bool9.equals(Boolean.TRUE)); // 输出: true

4. 实例方法

4.1 获取值
  • boolean booleanValue():返回此Boolean对象的boolean值。
Boolean bool11 = new Boolean(true);
boolean b4 = bool11.booleanValue();
System.out.println(b4); // 输出: true
4.2 哈希码
  • int hashCode():返回此Boolean对象的哈希码。
Boolean bool12 = new Boolean(true);
System.out.println(bool12.hashCode()); // 输出: 1231 (对于`true`)
Boolean bool13 = new Boolean(false);
System.out.println(bool13.hashCode()); // 输出: 1237 (对于`false`)
4.3 字符串表示
  • String toString():返回一个表示此Boolean值的字符串对象。
  • static String toString(boolean b):返回一个表示指定boolean值的字符串对象。
Boolean bool14 = new Boolean(true);
System.out.println(bool14.toString()); // 输出: true

System.out.println(Boolean.toString(false)); // 输出: false
4.4 逻辑运算
  • static boolean logicalAnd(boolean a, boolean b):返回两个布尔值的逻辑与。
  • static boolean logicalOr(boolean a, boolean b):返回两个布尔值的逻辑或。
  • static boolean logicalXor(boolean a, boolean b):返回两个布尔值的逻辑异或。
System.out.println(Boolean.logicalAnd(true, true));   // 输出: true
System.out.println(Boolean.logicalAnd(true, false));  // 输出: false
System.out.println(Boolean.logicalOr(true, false));   // 输出: true
System.out.println(Boolean.logicalOr(false, false));  // 输出: false
System.out.println(Boolean.logicalXor(true, false));  // 输出: true
System.out.println(Boolean.logicalXor(true, true));   // 输出: false

5. 总结与使用场景

  • 构造方法:用于创建Boolean对象,可以通过boolean值或String值进行构造。
  • 常量:提供了Boolean对象的常量表示(TRUEFALSE)。
  • 静态方法
    • 类型转换:通过valueOf方法可以将基本类型boolean或字符串转换为Boolean对象。
    • 布尔值解析parseBoolean方法用于解析字符串为布尔值。
    • 比较compare方法用于比较两个布尔值,equals方法用于判断Boolean对象是否相等。
  • 实例方法
    • 获取值booleanValue方法用于获取Boolean对象的boolean值。
    • 哈希码hashCode方法返回Boolean对象的哈希码。
    • 字符串表示toString方法将布尔值转换为字符串。
    • 逻辑运算:提供了逻辑与、或、异或运算的静态方法。

6. 使用场景

  • 构造Boolean对象:当需要将布尔值封装为对象进行传递或存储时,可以使用Boolean类。
  • 字符串解析:当需要从字符串中解析布尔值时,可以使用parseBooleanvalueOf
  • 布尔值比较:当需要比较两个布尔值时,可以使用compareequals方法。
  • 逻辑运算:当需要进行布尔值的逻辑运算时,可以使用logicalAndlogicalOrlogicalXor方法。

通过这些方法的分类和测试用例,你应该能够彻底理解并熟练运用Boolean类中的所有方法。

Void

Void 类是 Java 中一个特殊的类,用于表示 void 这个特殊的返回类型。Void 类是 java.lang 包的一部分,它是一个最终类(final class),这意味着它不能被继承。Void 类没有公共构造函数,因此不能实例化。作为一个特殊的类,Void 类主要用于反射和泛型中,表示方法没有返回值。
由于 Void 类实际上非常简单,并且没有方法(除了从 Object 类继承的方法),以下是对 Void 类及其相关内容的详细解释。

Void 类简介

Void 类是为了表示 void 关键字在反射和泛型中的作用。void 是 Java 中的一种返回类型,表示方法不返回任何值。而 Void 类是 void 类型的包装类,用于在需要使用类的地方表示 void 类型。

一、Void 类中的字段

1. TYPE

功能: TYPE 是一个静态字段,表示 void 的类对象。TYPE 字段是 Class<Void> 类型的对象。
示例:

public class VoidExample {
    public static void main(String[] args) {
        Class<Void> voidType = Void.TYPE;
        System.out.println("Void type: " + voidType);
    }
}

二、继承自 Object 类的方法

由于 Void 类没有定义任何方法,它只继承了 Object 类中的方法。以下是 Object 类中的方法,Void 类也继承了这些方法:

  • boolean equals(Object obj):比较两个对象是否相等。
  • int hashCode():返回对象的哈希码。
  • String toString():返回对象的字符串表示。
  • void notify():唤醒在此对象监视器上等待的单个线程。
  • void notifyAll():唤醒在此对象监视器上等待的所有线程。
  • void wait():让当前线程等待,直到其他线程调用此对象的 notify()notifyAll() 方法。
  • void wait(long timeout):让当前线程等待指定的时间,直到其他线程调用此对象的 notify()notifyAll() 方法,或者等待时间已到。
  • void wait(long timeout, int nanos):让当前线程等待指定的时间和纳秒,直到其他线程调用此对象的 notify()notifyAll() 方法,或者等待时间已到。
  • Object clone():创建并返回此对象的一个副本。
  • Class<?> getClass():返回此 Object 的运行时类。

三、使用场景

1. 泛型中的使用

在泛型中,有时需要表示 void 类型,例如在 Future<Void> 中表示一个不返回值的异步任务。
示例:

import java.util.concurrent.*;

public class VoidExample {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        Future<Void> future = executorService.submit(new Callable<Void>() {
            @Override
            public Void call() {
                // 执行一些任务,但不返回值
                System.out.println("Task executed");
                return null;
            }
        });

        try {
            future.get(); // 等待任务完成
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        } finally {
            executorService.shutdown();
        }
    }
}
2. 反射中的使用

在反射中,Void.TYPE 用于表示 void 返回类型。
示例:

import java.lang.reflect.Method;

public class VoidExample {
    public static void main(String[] args) {
        try {
            Method method = VoidExample.class.getMethod("exampleMethod");
            Class<?> returnType = method.getReturnType();
            if (returnType.equals(Void.TYPE)) {
                System.out.println("The return type of exampleMethod is void");
            }
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }

    public static void exampleMethod() {
        // 这是一个返回类型为 void 的方法
    }
}

总结

Void 类是一个特殊的类,用于表示 void 类型。尽管它没有定义任何方法,但它在泛型和反射中有特定的用途。以下是对 Void 类的简要总结:

  • 字段:
    • TYPE: 表示 void 的类对象 (Class<Void> 类型)。
  • 继承自 Object 类的方法:
    • boolean equals(Object obj)
    • int hashCode()
    • String toString()
    • void notify()
    • void notifyAll()
    • void wait()
    • void wait(long timeout)
    • void wait(long timeout, int nanos)
    • Object clone()
    • Class<?> getClass()
  • 使用场景:
    • 泛型: 在泛型中使用 Void 表示 void 类型,如 Future<Void>
    • 反射: 在反射中使用 Void.TYPE 表示 void 返回类型。
  • 14
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值