第三章:常用类01

第三章:常用类

包装类

什么是包装类??

  • java中为8中基本数据类型又对应准备了8中包装类型。8中包装类型有属于引用数据类型,父类Object
  • 为什么要再提供8中包装类呢??
    java并不是纯面向对象的语言,java语言是一个面向对象的语言,但是java中的基本数据类型却不是面向对象的,但是我们在实际使用中经常将基本数据类型转换成对象,便于操作,比如,集合的操作中,这时,我们就需要将基本类型数据转化成对象!
1. 包装类过程
  • Java中的基本数据类型没有方法和属性,而包装类就是为了让这些拥有方法和属性,实现对象化交互。

在这里插入图片描述

  • 包装类的继承关系
    在这里插入图片描述
  • 包装类的对应
    在这里插入图片描述
  • 包装类的基本操作
    在这里插入图片描述
2.装箱与拆箱
  • 装箱:基本数据类型转换为包装类;
  • 拆箱:包装类转换为基本数据类型。
//手动拆装箱
public class IntegerTest02 {
    public static void main(String[] args) {
        //基本数据类型->>>引用数据类型(装箱)
        Integer i = new Integer(123);
        //引用数据类型->>基本数据类型(拆箱)
        float f = i.floatValue();
        System.out.println(f);

        //引用数据类型->>基本数据类型(拆箱)
        int intValue = i.intValue();
        System.out.println(intValue);
    }
}
//自动拆装箱
public class Test {
    public static void main(String[] args) {
        //JDK5新特性
        Integer i = 30;  //装箱
        int c = i;         //拆箱
        System.out.println("---------------------");
    }
}
2.int,Integer,String相互转换

通过包装类Integer.toString()将整型转换为字符串;

通过Integer.parseInt()将字符串转换为int类型;

通过valueOf()方法把字符串转换为包装类然后通过自动拆箱。

/*
 String  int  Integer相互转换
 */

public class InterTest03 {
    public static void main(String[] args) {
        //String---->int
        int i1 = Integer.parseInt("100");
        System.out.println(i1 + 1);
        //int -- >String
        String s2 = String.valueOf(i1);
        System.out.println(s2 + 1);

        //int -- 》Integer
        Integer x = 100;
        //Integer-->int
        int y = x;

        //String --> Integer
        Integer k = Integer.valueOf("100");
        //Integer-->String
        String e = String.valueOf(k);
    }
}

String类

1. 注意事项
  • String表示字符类型,属于引用数据类型,不属于基本数据类型
  • 在java中随便使用双引号括起来的都是String对象。例如“abc”,“def”
  • java中规定,双引号括起来的字符串,是不可变的,也就是说"abc"自出生到死亡都是不可改变的
  • 在JDK当中双引号括起来的字符串,如"abc”,"def"都是直接存储在方法区中的“字符串常池”当中的
2. String s = “abc”,与String s = new String(“abc”)的内存图解。

在这里插入图片描述

3. String的构造方法。
  • String s = new String("")
  • String s = “”
  • String s = new String(char数组)
  • String s = new String(char数组,起始下标,长度)
  • String s = new String(byte数组)
  • String s = new String(byte数组,起始下标,长度)
public class StringTest02 {
    public static void main(String[] args) {
        String s1 = new String("abc");
        System.out.println(s1);

        String s2 = "abc";
        System.out.println(s2);

        char[] chars = {'a','b','c'};
        String s3 = new String(chars);
        System.out.println(s3);
        String s4 = new String(chars,1,2);
        System.out.println(s4);

        byte[] by = {11,12,13,14};
        String s5 = new String(by);
        System.out.println(s5);
        String s6 = new String(by,1,3);
        System.out.println(s6);

    }
}
4. String的常用方法。
public class StringTest03 {
    public static void main(String[] args) {
        /*
         * (掌握) public char charAt(int index)
         *           返回指定索引处的 char 值。
         */
        char c = "中国人".charAt(1);
        System.out.println(c);
        /**
         * (了解)public int compareTo(String anotherString)
         *              按字典顺序比较两个字符串。
         */
        System.out.println("abc".compareTo("abcd"));
        /**
         *  boolean contains(CharSequence s)
         *           当且仅当此字符串包含指定的 char 值序列时,返回 true。
         */
        System.out.println("helloworld".contains("world"));
        /**
         *  String concat(String str)
         *           将指定字符串连接到此字符串的结尾。
         */
        System.out.println("hello".concat("world"));
        /**
         * boolean endsWith(String suffix)
         *           测试此字符串是否以指定的后缀结束。
         */
        System.out.println("test.txt".endsWith(".txt"));
        System.out.println("test.txt".endsWith(".java"));
        /**
         * boolean equals(Object anObject)
         *           将此字符串与指定的对象比较。
         */
        System.out.println("hello".equals("hello"));
        /**
         * boolean equalsIgnoreCase(String anotherString)
         *           将此 String 与另一个 String 比较,不考虑大小写。
         */
        System.out.println("ABC".equalsIgnoreCase("abc"));
        /**
         * byte[] getBytes()
         *           使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
         */
        byte [] bytes = "abcdef".getBytes();
        for (int i =0;i<bytes.length;i++){
            System.out.println(bytes[i]);
        }
        /**
         * int indexOf(String str)
         *           返回指定子字符串在此字符串中第一次出现处的索引。
         */
        System.out.println("hoooojava".indexOf("java"));
        /**
         * boolean isEmpty()
         *           当且仅当 length() 为 0 时返回 true。
         */
        System.out.println("".isEmpty());
        /**
         *  int lastIndexOf(int ch)
         *           返回指定字符在此字符串中最后一次出现处的索引
         */
        System.out.println("javajavajava".lastIndexOf("a"));
        /**
         * String replace(CharSequence target, CharSequence replacement)
         *           使用指定的字面值替换序列替换此字符串所有匹配字面值目标序列的子字符串。
         */
        System.out.println("helloworld".replace("hello","nihao"));
        /**
         *  String[] split(String regex)
         *           根据给定正则表达式的匹配拆分此字符串。
         */
        String [] s1 = "2020-10-31".split("-");
        for (int i = 0;i<s1.length;i++){
            System.out.print(s1[i]);
        }
        /**
         *  boolean startsWith(String prefix)
         *           测试此字符串是否以指定的前缀开始。
         */
        System.out.println("http://www.".startsWith("http"));
        /**
         *  String substring(int beginIndex)
         *           返回一个新的字符串,它是此字符串的一个子字符串。
         */
        System.out.println("http://www".substring(1));
        /*
            String substring(int beginIndex, int endIndex)
              返回一个新字符串,它是此字符串的一个子字符串。
         */
        System.out.println("http://www.baidu.com".substring(7,10));
        /**
         *  char[] toCharArray()
         *           将此字符串转换为一个新的字符数组。
         */
        char [] chars = "我是中国人".toCharArray();
        for (int i = 0;i<chars.length;i++){
            System.out.println(chars[i]);
        }
        /**
         * String toLowerCase()
         *           使用默认语言环境的规则将此 String 中的所有字符都转换为小写。
         *
         *  String toUpperCase()
         *           使用默认语言环境的规则将此 String 中的所有字符都转换为大写。
         */
        System.out.println("ABCDE".toLowerCase());
        System.out.println("abdc".toUpperCase());
        /**
         * String trim()
         *           返回字符串的副本,忽略前导空白和尾部空白。
         */
        System.out.println("           hello        world       ".trim());
        /**
         * static String valueOf(boolean b)
         *           返回 boolean 参数的字符串表示形式。
         * static String valueOf(char c)
         *           返回 char 参数的字符串表示形式。
         * static String valueOf(char[] data)
         *           返回 char 数组参数的字符串表示形式。
         * static String valueOf(char[] data, int offset, int count)
         *           返回 char 数组参数的特定子数组的字符串表示形式。
         * static String valueOf(double d)
         *           返回 double 参数的字符串表示形式。
         * static String valueOf(float f)
         *           返回 float 参数的字符串表示形式。
         * static String valueOf(int i)
         *           返回 int 参数的字符串表示形式。
         * static String valueOf(long l)
         *           返回 long 参数的字符串表示形式。
         * static String valueOf(Object obj)
         *           返回 Object 参数的字符串表示形式。
         */
        System.out.println(String.valueOf(111));
        System.out.println(String.valueOf('a'));
        System.out.println(String.valueOf(3.14));

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值