Java常用类(一)

目录

1.基本数据类型的包装类

        1.1.八大基本数据类型的包装类

        1.2.包装类的层次结构

        1.3.自动装箱和自动拆箱

2.字符串相关类

        2.1.字符串的实例化

        2.2.字符串的运算

        2.3.字符串的比较

                2.3.1.用equals方法比较

                2.3.2.用==操作符比较

                2.3.3.使用compareTo()方法按字母顺序比较

                                 2.3.4.使用compareToIgnoreCase()方法比较

        2.4.字符串的常用方法


1.基本数据类型的包装类

        1.1.八大基本数据类型的包装类

        

        1.2.包装类的层次结构

在 Java 中,基本数据类型都有对应的包装类,它们形成了一个层次结构,以下是常见的包装类的层次结构:

1. Byte - 对应基本数据类型 byte。

2. Short - 对应基本数据类型 short。

3. Integer - 对应基本数据类型 int。

4. Long - 对应基本数据类型 long。

5. Float - 对应基本数据类型 float。

6. Double - 对应基本数据类型 double。

7. Boolean - 对应基本数据类型 boolean。

8. Character - 对应基本数据类型 char。

这些包装类提供了一些方法,使得基本数据类型能够像对象一样进行操作,例如转换、比较、计算等。同时,包装类还在一些情况下提供了更好的类型安全和面向对象的编程特性。包装类在 Java 的编程中被广泛使用,特别是在需要对基本数据类型进行更复杂的操作和处理时。
 

        1.3.自动装箱和自动拆箱

装箱:把基本数据类型包装为对应的包装类对象

1、 Integer i1 = new Integer(10); // 利用构造方法

2、 Integer i2 = Integer.valueOf(10); //利用包装类中的静态方法

拆箱:把包装类对象转换为对应的基本数据类型。

1、int i3= i1.intValue(); //返回包装类对象i1对应的基本数据

public class BoxingAndUnboxingExample {
    public static void main(String[] args) {
        // 自动装箱
        Integer integer1 = 10; 
        // 自动拆箱
        int intValue = integer1; 
    }
}

在上述代码中,Integer integer1 = 10; 就是自动装箱的示例,将基本类型 int 的值 10 自动包装到 Integer 包装类对象 integer1 中。

而 int intValue = integer1; 则是自动拆箱的示例,将 Integer 包装类对象 integer1 自动转换为基本类型 int 并赋值给 intValue 变量。

2.字符串相关类

        2.1.字符串的实例化

在 Java 中,可以使用以下几种常见的方式实例化字符串:

1. 直接赋值:
String str = "Hello, World!";
在上述示例中,使用双引号括住字符串内容,然后将其赋值给一个 String 类型的变量 str。

2. 通过构造函数实例化:
String str = new String("Hello, World!");
这种方式不太常用,因为通常使用直接赋值的方式更简洁和高效。

        2.2.字符串的运算

String s3 = “abc” + “d”;

String s4 = s3 + 5; //abcd5

注意:字符串类型的数据与其他数据类型做运算时,结果一定是字符串类型

        2.3.字符串的比较

                2.3.1.用equals方法比较

使用equals()方法比较字符串的内容:该方法比较两个字符串的内容是否相等,无论它们在逻辑上是否相等。如果参数字符串不为null且包含与指定字符串相同的字符,则返回true。

public class CompareUsingEquals {
    public static void main(String[] args) {
        String firstString = "coderolls";
        String secondString = "javablog";
        String thirdString = "coderolls";
        String fourthString = "CodeRolls";

        System.out.println("comparing strings using equals() and equalsIgnoreCase() method\n");

        System.out.println(firstString.equals(secondString));
        System.out.println(firstString.equals(thirdString));
        System.out.println(firstString.equalsIgnoreCase(fourthString));
    }
}
                2.3.2.用==操作符比较

使用==操作符比较字符串:在String中,==操作符用于比较给定字符串的引用,这取决于它们是否引用相同的对象。当使用==操作符比较两个字符串时,如果字符串变量指向相同的Java对象,则返回true,否则返回false。

public class CompareUsingEquals {
    public static void main(String[] args) {
        String firstString = "coderolls";
        String secondString = "javablog";
        String thirdString = "coderolls";
        String fourthString = "CodeRolls";

        System.out.println("comparing strings using equals() and equalsIgnoreCase() method\n");

        System.out.println(firstString.equals(secondString));
        System.out.println(firstString.equals(thirdString));
        System.out.println(firstString.equalsIgnoreCase(fourthString));
    }
}
                2.3.3.使用compareTo()方法按字母顺序比较

使用compareTo()方法按字母顺序比较字符串:该方法比较两个字符串,并返回字符串中第一个字母ASCII的差值。

public class StringCompareEmp {
    public static void main(String[] args) {
        String str = "Hello World";
        String anotherString = "hello world";
        Object objStr = str;

        System.out.println(str.compareTo(anotherString));
        System.out.println(str.compareToIgnoreCase(anotherString));
        System.out.println(str.compareTo(objStr.toString()));
    }
}

                           2.3.4.使用compareToIgnoreCase()方法比较

使用compareToIgnoreCase()方法比较字符串(忽略大小写):该方法与compareTo()方法类似,但它在比较字符串时不考虑大小写。

    2.4.字符串的常用方法

                1. length() 方法:获取字符串的长度。

public class StringMethodsExample {
    public static void main(String[] args) {
        String str = "Hello, World!";
        int length = str.length();
        System.out.println("字符串 'Hello, World!' 的长度为:" + length);
    }
}

            2. isEmpty() 方法:判断字符串是否为空(长度为 0)。

public class StringMethodsExample {
    public static void main(String[] args) {
        String str = "";
        boolean isEmpty = str.isEmpty();
        System.out.println("字符串是否为空:" + isEmpty);

        str = "Hello, World!";
        isEmpty = str.isEmpty();
        System.out.println("字符串是否为空:" + isEmpty);
    }
}

              3. indexOf() 方法:查找字符串中指定字符或子字符串首次出现的索引位置。    

public class StringMethodsExample {
    public static void main(String[] args) {
        String str = "Hello, World! How are you?");
        int index = str.indexOf("World");
        System.out.println("子字符串 'World' 在字符串中的索引为:" + index);

        index = str.indexOf('o');
        System.out.println("字符 'o' 在字符串中的索引为:" + index);
    }
}

                  4. substring() 方法:截取字符串的一部分。

public class StringMethodsExample {
    public static void main(String[] args) {
        String str = "Hello, World!";
        String subStr = str.substring(7);
        System.out.println("截取后的字符串:" + subStr);
    }
}

                5. toLowerCase() 方法:将字符串全部转换为小写。
 

public class StringMethodsExample {
    public static void main(String[] args) {
        String str = "HELLO, WORLD!";
        String lowerCaseStr = str.toLowerCase();
        System.out.println("转换为小写后的字符串:" + lowerCaseStr);
    }
}

                6. toUpperCase() 方法:将字符串全部转换为大写。

public class StringMethodsExample {
    public static void main(String[] args) {
        String str = "hello, world!";
        String upperCaseStr = str.toUpperCase();
        System.out.println("转换为大写后的字符串:" + upperCaseStr);
    }
}

  • 10
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值