zoukankan      html  css  js  c++  java
  • Java基础(六)包装类

    一、包装类

      JAVA是一种面向对象语言,java中的类把方法与数据连接在一起,但在JAVA中不能定义基本类型对象为了能将基本类型视为对象进行处理,java为每个基本类型都提供了包装类

    对应关系如下:

    基本类型 包装类型
    byte Byte
    short Short
    int Integer
    long Long
    float Float
    double Double
    char Character
    boolean Boolean

     

    1、Number类

      BigDecimal、BigInteger、Byte、Double、Float、Integer、Long、Short 都是Number类的子类。

      CharacterBoolean 父类是Object,不属于Number下面的子类。

    查看API文档:

      Number类是抽象类,不能直接通过new关键字创建。 Number类有六个方法,并且这些方法有些是abstract修饰的方法(抽象方法),Number 的子类需要覆写这些抽象方法。

    2、Integer包装类

    查看API文档:

      理解:Integer类里面包含一个int value,可以理解内部有个变量盒子,存放int值,也就是说Integer类里面有个int值。

      由于父类Number已经提供6个方法,子类就可以调用父类提供6个方法,把Integer对象里面的int 值转换成其他的6个基本类型值,比如转换成byte,short,int,long,float,double 。

      同理:对于Number子类的其他类型,比如Double类也是一样,可以调用Number类里面提供的6个方法把Double里面存放的值转换成这6个值。

      查看 Integer 源代码,可以看到 Integer 类里面有 int 字段,有个构造方法。

      当通过Integer构造方法创建Integer对象的时候 (比如new Integer(123)),就把值,存放到 int 类型的字段里面。

    2.1 Integer 构造方法

     查看API文档:

    查看 Integer 源代码:

      可以看出通过 Integer(String s) 构造方法的源码看出最终还是把值赋值给变量 this.value,this.value  是指在Integer里面定义的一个int 变量,用于存放值。

    注意:

    1、当试图将字符串转换成一种数值类型时,该字符串里面包含不能转换的字符时,出现异常。例如含有字母(A、B、C)或运算符(+ 、-)等。

    2、不能超出 int 的范围时,比如12345678987654321 。 

    3、自动装箱拆箱

    自动装箱:从基本类型(int)转换成包装类型(Integer),不用手动进行转换操作,编译器会自动调用 Integer.valueof 方法把基本类型(int)转换成 (Integer)类型这种方式。

    方式一:通过构造方法new Integer(123);

    方式二:直接赋值给Integer ;

        Integer integer = 123;

    自动拆箱:从包装类型(Integer)转换成基本类型(Int),也不用在进行手动进行转换操作,编译器会自动调用 Integer.intValue 把包装类型(Integer)转换成基本类型 (int)。

    方式一:调用Integer里的方法 intValue方法;

    方式一:直接赋值给int;

    8 种基本类型与包装类型之间的转换都支持自动拆箱和装箱操作。

    4、short类、Long类和Double类

      除了这个Integer包装类以外,还有Short,Long,Double包装类,结构都和Integer类似。

    5、Boolean类 和 Character

      Character和Boolean 父类是Object。   

      Boolean类无最大,最小值。

      Character就表示一个字符,而String 字符串表示多个字符。

    6、String

    查看API文档:

    从API看出String类特点:

      1.索引是从0开始的;

      2.字符串对象(String对象)一旦创建其长度和内容就不能更改;

    创建String对象的方式:

      1、直接赋值,String name = "abcdefg";

      2、调用String的构造方法。

     1 public class Demo {
     2     /**
     3     * 测试创建字符串
     4     */
     5     @Test
     6     public void test1(){
     7         String s1 = new String();  //表示一个空字符串
     8         String s2 = "";
     9         String s3 = null;
    10 
    11         System.out.println(s1 == s2 );  //false
    12         System.out.println(s1.equals(s3));  //false
    13         //System.out.println(s3.equals(s1));  s3 放在equals前面会报空指针异常 java.lang.NullPointerException
    14     }
    15 }

    分析:

       String s1 = new String();  //s1将指向具体的String实例,默认值为“”,在内存中分配一个空间;

       String s2 = " ";  //s2对应一个空串,声明对象的引用 ,在内存中分配一个空间;

       String s3 = null;  //s3引用为空;

    注意:s1和s2被实例化,而s3没有实例化,但s1s2所指的地址空间不同,值一样,都为空。

     6.1 String 类常用方法

    1、char charAt(int index);

      取字符串中指定位置的字符,index 从0 开始计算。

     1 public class Demo {
     2     /**
     3      * 测试charAt
     4      */
     5     @Test
     6     public void testCharAt() {
     7         String str = "abcdefg";
     8         for (int i = 0; i < str.length(); i++) {
     9             char element = str.charAt(i);
    10             System.out.print("[" + element + "]");  //输出:[a][b][c][d][e][f][g]
    11         }
    12     }
    13 }

    2、int codePointAt(int index);

      返回指定索引处的字符(Unicode 代码点)。

     1 public class Demo {
     2     /**
     3     * 测试codePointAt
     4     */
     5     @Test
     6     public void testCodePointAt(){
     7         String str = "abcdefg";
     8         int codePointAt = str.codePointAt(2);
     9         System.out.println(codePointAt);  //输出:99 代表c 的 ASCII码
    10     }
    11 }

    3、String concat(String str);

      将指定(传入)字符串连接到此(当前)字符串的结尾。用于连接字符串

     1 public class Demo {
     2     /**
     3     * 测试concat
     4     */
     5     @Test
     6     public void testConcat(){
     7         String str1 = "字符串1111";
     8         String str2 = "字符串2222";
     9         String concatStr = str1.concat(str2);  //创建一个字符串
    10         System.out.println(concatStr);  //输出:字符串1111字符串2222
    11     }
    12 }

    4、boolean endsWith(String suffix);

      测试此字符串是否以指定的后缀结束。

     1 public class Demo {
     2     /**
     3      * 测试endsWith
     4      */
     5     @Test
     6     public void testEndsWith(){
     7         String str = "abcd";
     8         boolean result = str.endsWith("d");
     9         System.out.println(result);  //返回:true
    10     }
    11 }

    5、boolean isEmpty();

      当且仅当length() 时返回 true

     1 public class Demo {
     2     /**
     3      * 测试isEmpty
     4      */
     5     @Test
     6     public void testIsEmpty(){
     7         String str = "";  //不能为null
     8         boolean result = str.isEmpty();
     9         System.out.println(result);  //返回:true
    10     }
    11 }

    6、int length();

      返回当前字符串长度。

    7、String replace(char oldChar, char newChar);

      返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。

     1 public class Demo {
     2     /**
     3      * 测试replace
     4      */
     5     @Test
     6     public void testReplace(){
     7         String str = "字符串222";
     8         String newStr = str.replace("222", "替换");
     9         System.out.println(newStr);  //输出:字符串替换
    10     }
    11 }

    8、String substring(int beginIndex);

      返回一个新的字符串,从beginIndex 开始截取,它是此字符串的一个子字符串。

    String substring(int beginIndex. int endIndex);

      返回一个新字符串,它是此字符串的一个子字符串;注意:左闭右开,左边包括,右边不包括。

     1 public class Demo {
     2     /**
     3      * 测试substring
     4      */
     5     @Test
     6     public void testSubString(){
     7         String str1 = "abcdefg";
     8         String substring = str1.substring(1);
     9         System.out.println(substring);  //输出:bcdefg
    10 
    11         String str2 = "abcdefg";
    12         String substring1 = str2.substring(1, 3);
    13         System.out.println(substring1);  //输出:bc 注意:不包含index为3的字符
    14     }
    15 }

    9、String toLowerCase();

      将此String 中的所有字符都转换为小写。

    10、String toUpperCase();

      将此String 中的所有字符都转成大写。

    11、String trim();

      忽略字符串前导空白和尾部空白。

     1 public class Demo {
     2     /**
     3      * 测试trim
     4      */
     5     @Test
     6     public void testTrim(){
     7         String str = "   233333   ";
     8         String trim = str.trim();
     9         System.out.println(trim);  //输出:233333 没有空格了
    10     }
    11 }

    6.2 StringBuffer 类和 StringBuilder 类

    查看API文档:

    StringBuffer类常用方法

    1、StringBuffer append(Object o);

      将指定的任意类型对象追加到此StringBuffer 对象。

    2、StringBuffer delete(int start, int end);

      移除此序列的子字符串中的字符。

    3、StringBuffer insert(int offset, Object o);

      将任意类型参数的字符串表示形式插入此序列中。

    4、StringBuffer replace(int start, int end, String str);

      使用给定 String 中的字符替换此序列的子字符串中的字符。

     1 public class StringBufferTest {
     2     /**
     3     * 测试StringBuffer方法
     4     */
     5     @Test
     6     public void testStringBufferMethod(){
     7         //1.StringBuffer append(Object o); 添加
     8         StringBuffer str = new StringBuffer("abcdefgh");
     9         str.append("ijk");
    10         System.out.println(str);  //输出:abcdefghijk
    11 
    12         //2.StringBuffer delete(int start, int end); 删除
    13         str.delete(0, 3);  
    14         System.out.println(str);  //输出:defghijk  左闭右开[ )
    15 
    16         //3、StringBuffer insert(int offset, Object o); 插入
    17         str.insert(1, "333");
    18         System.out.println(str); //输出:d333efghijk
    19 
    20         //4、StringBuffer replace(int start, int end, String str); 替换
    21         str.replace(1, 4, "222");
    22         System.out.println(str);  //输出:d222efghijk
    23     }
    24 }

    比较 StringBuffer 和 String

     1 public class StringBufferTest {
     2     /**
     3      * 测试StringBuffer
     4      * 把0-9999 整数连接成一个字符串;
     5      * 类似效果123456789101112....9999;
     6      * 
     7      * 分析:1. 动态获取10000个值,使用for循环
     8      * 2. 定义一个空字符串str
     9      * 3. 循环拼接
    10      */
    11     @Test
    12     public void testStringBuffer() {
    13         long begin1 = System.currentTimeMillis();
    14         String str1 = "";
    15         for (int i = 0; i < 10000; i++) {
    16             str1 += i;
    17         }
    18         System.out.println(str1);
    19         long end1 = System.currentTimeMillis();
    20         System.out.println(end1 - begin1);  //输出: 974  花费时间多,因为要创建 10000 个对象。
    21 
    22         System.out.println("=========");
    23 
    24         //使用stringBuffer 只创建一个对象
    25         long begin2 = System.currentTimeMillis();
    26         StringBuffer str2 = new StringBuffer();
    27         for (int i = 0; i < 10000; i++) {
    28             str2.append(i);
    29         }
    30         System.out.println(str2);
    31         long end2 = System.currentTimeMillis();
    32         System.out.println(end2 - begin2); //输出: 11  时间花费非常少
    33     }
    34 }

    StringBuilder类

    小结:

    1、String:不可变得字符串;

    2、StringBuffer:可变的,是线程安全;

    3、StringBuilder:可变的,是线程不安全,比StringBuffer性能高点;

    4、String,StringBuffer,StringBuilder 都表示字符序列,接口都是CharSequence;

    5、StringBuffer,StringBuilder中有很多和String中一样的方法;

    特有的方法: 主要是可以改变对象值中的方法(添加,删除,插入,替换,反转)

  • 相关阅读:
    关于DRY原则
    类型之惑
    ThoughtWorks测试
    编程非易事
    瀑布与迭代的真实区别
    对TDD原则的理解
    自我练习
    C# CreateProcess的测试
    乱侃OOD
    复杂系统的五个属性
  • 原文地址:https://www.cnblogs.com/zt19994/p/8422661.html
Copyright © 2011-2022 走看看