Java常用类与方法

Chtholly 发布于 2022-08-24 1001 次阅读


包装类

注:引用类型(例如Integer)中的所有方法均为静态方法(static),不需要new对象即可使用

  • 装箱与拆箱
public static void main(String[] args) {
        int age = 30;

        //装箱
        Integer integer = new Integer(age);
        //Integer integer = Integer.valueOf(age);
        
        //拆箱
        int num = integer.intValue();
        
        //JDK1.5之后提供自动装箱拆箱
        int age2 = 30;

        //自动装箱
        Integer integer2 = age2;

        //自动拆箱
        int num2 = integer2;
    }
  • 基本类型和字符串转换
   public static void main(String[] args) {
        
        //基本类型转字符串
        int n1=100;
        
        String s1 = n1+"";
        
        String s2 = Integer.toString(n1);
        
        //字符串转基本类型
        String s3 = "100";
        
        int n2 = Integer.getInteger(s3);
        
    }
  • Integer缓冲区

没看懂

String常用方法

注:String具有不可变性

  • public int length():返回字符串的长度
  • public char charAt(int index):根据下标获取字符
public static void main(String[] args) {
        String string = "Hello World";
        System.out.println(string.charAt(0));
        //0
    }
  • public boolean contains(String str):判断当前字符串是否包含str
 public static void main(String[] args) {
        String string = "Hello World";
        System.out.println(string.contains("Hello"));
        //true

    }
  • public char[] toCharArray():将字符串转换成字符数组
public static void main(String[] args) {
        String string = "Hello World";
        char[] array = string.toCharArray();
        for (int i=0;i<string.length();i++){
            System.out.print(array[i]+" ");
        }
        //H e l l o   W o r l d 
    }
  • public int indexOf(String str):查找str首次出现的下标,存在返回下标,不存在返回-1
  • public int lastIndexOf(String str):查找str最后一次出现的下标
  • public String trim():去掉字符串前后的空格
public static void main(String[] args) {
        String string = " Hello World ";
        System.out.println(string.trim());
        //Hello World
    }
  • public String toUpperCase():将小写转成大写
  • public String toLowerCase():将大写转成小写
  • public boolean endWith(String str):判断字符串是否以str结尾
  • public String replace (char oldChar , char newChar):将旧字符替换成新字符
public static void main(String[] args) {
        String string = "Java一定是世界上最好的语言,你可以有不同的意见,但你不能改变我内心的想法。";
        System.out.println(string.replace("Java","Python"));
        //Python一定是世界上最好的语言,你可以有不同的意见,但你不能改变我内心的想法。
    }
  • public String[] split(String str):根据str做拆分
public static void main(String[] args) {
        String string = "Java一定是世界上最好的语言,你可以有不同的意见,但你不能改变我内心的想法。";
        System.out.println(string.replace("Java","Python"));
        //Python一定是世界上最好的语言,你可以有不同的意见,但你不能改变我内心的想法。

        String[] strings = string.split("[,]");
        for(String str : strings){
            System.out.println(str);
        }
        //Java一定是世界上最好的语言
        //你可以有不同的意见
        //但你不能改变我内心的想法。

        String[] strings1 = string.split("[,。]");
        for(String str : strings1){
            System.out.println(str);
        }
        //Java一定是世界上最好的语言
        //你可以有不同的意见
        //但你不能改变我内心的想法
    }

可变字符串

  • StringBuffer:可变长字符串,运行效率慢(比string快)、线程安全。
  • StringBuilder:可变成字符串,运行效率快、线程不安全。
  • 两者用法相同
public static void main(String[] args) {
        String string = "Java是世界上最好的语言";
        StringBuffer stringBuffer = new StringBuffer(string);
        //1、append(); 追加
        stringBuffer.append(" Python也不错");
        System.out.println(stringBuffer); //Java是世界上最好的语言 Python也不错
        //2、insert(); 添加
        stringBuffer.insert(0,"鲁迅说:");
        System.out.println(stringBuffer); //鲁迅说:Java是世界上最好的语言 Python也不错
        //3、replace(); 替换
        stringBuffer.replace(0,4,"我听说");
        System.out.println(stringBuffer); //我听说Java是世界上最好的语言 Python也不错
        //4、delete(); 删除
        stringBuffer.delete(0,3);
        System.out.println(stringBuffer); //Java是世界上最好的语言 Python也不错
    }

BigDecimal(精确运算)

public static void main(String[] args) {
        BigDecimal bigDecimal1 = new BigDecimal("1.0");
        BigDecimal bigDecimal2 = new BigDecimal("0.9");
        System.out.println(bigDecimal1);//1.0
        //减法
        BigDecimal bigDecimal3 = bigDecimal1.subtract(bigDecimal2);
        System.out.println(bigDecimal3);//0.1
        //加法
        BigDecimal bigDecimal4 = bigDecimal1.add(bigDecimal2);
        System.out.println(bigDecimal4);//1.9
        //乘法
        BigDecimal bigDecimal5 = bigDecimal1.multiply(bigDecimal2);
        System.out.println(bigDecimal5);//0.90
        //除法
        BigDecimal bigDecimal6 = new BigDecimal("1.4")
                .subtract(new BigDecimal("0.5"))
                .divide(new BigDecimal("0.9"));
        System.out.println(bigDecimal6);//1
    }
此作者没有提供个人介绍。
最后更新于 2022-08-24