Java:三个特殊的类(String类、Object类和包装类)

一:String

  1. String类的两种实例化方式:

    a、直接赋值:String str1 = “hello world”;

    b、传统方法:String str2 = new String(“hello world”);

  2. 字符串相等的比较:equals”和“==”:

    1)、“==”进行字符串的比较:

    String str1 = hello;

    String str2 = new String(hello);

    System.out.println(str1 == str2); // false

        这里的==”比较的是str1str2分别所指向堆内存的地址。也就是说这里str1str2的地址是不相同的。如下图所示:Str1利用直接赋值的方法,在常量池中直接产生了一个“hello”,而str2利用传统赋值的方法,先用new在堆空间里申请了一块内存用来存放String类的一个对象,内存当中存入“hello”,然后再将括号内的“hello”入池,这时因为str1在直接赋值时在常量池中已经产生了“hello”,所以str2括号内的“hello”无需再在常量池中申请空间进行存储,直接指向常量池原有的“hello”。

    (2)equals进行字符串的比较:

    String str1 = “hello”;

    String str2 = new String(“hello”);

    System.out.println(str1.equals( str2));  //true

        这里比较两个字符串用的是equals方法,比较的是str1str2中的内容,因为它俩的内容都是“hello”,所以返回“true”。

    (3)equals”与“==”的区别:

    a、“==”进行数值的比较,比较字符串时,比较的是字符串所在的地址数值。

    b、“equals”进行字符串内容的比较。

3、字符串常量不可变更。

    字符串一旦定义之后,就不能被更改。编程语言对字符串的实现都是字符数组,而数组一旦定义出来,长度是固定的。所以字符串常量不可变更。

String str = "hello";
Str = str + " world";
Str += "!!!";
System.out.println(str);//hello world!!!
这里之所以会打印“hello world!!!”,是因为Str所指向的内存地址发生了改变,如下图所示。
4、字符与字符串

    字符串就是一个字符数组,所以在String类里面支持有字符数组转换为字符串以及字符串变为字符的操作方法:

    a、public String(char value【】):将字符数组的所有内容变为字符串

    b、public String(char value【】,int offset,int count):将部分字符数组中的内容变为字符串

    c、public char charAt(int index):取得制定索引位置的字符,索引从0开始

    d、public char[] toCharArray():将字符串变为字符数组返回

5、字节与字符串

    a、public String(byte byte【】):将字节数组变为字符串

    b、public String(byte byte【】,int offset,int length):将部分字节数组中的内容变为字符串

    c、public byte【】 getBytes():将字符串以字节数组的形式返回

    d、public byte【】 getBytes(String charsetName)throws:编码转换处理

6、字符串比较

    a、public boolean equalsIgnoreCase(String anotherString):不区分大小写进行比较

    b、public int compareTo(String anotherString):比较两个字符串大小关系

    compareTo()方法在比较两个字符串时,基于字符串中各个字符的Unicode值进行比较。如果字符串相等返回0;小于返回内容小于0的数;大于返回内容大于0的数。

        System.out.println("A".compareTo("a"));//-32
        System.out.println("A".compareTo("A"));//0
        System.out.println("a".compareTo("A"));//32
        System.out.println("Aa".compareTo("AA"));//32

7、字符串查找

    a、public boolean contains(CharSequence s):判断子字符串是否存在

    b、public boolean startsWith(String prefix):判断是否以指定字符串开始

    c、public boolean endWith(String suffix):判断是否以指定字符串结尾

8、字符串拆分

    a、public String【】 split(String regex):将字符串全部拆分

    b、public String【】 split(String regex,int limit):将字符串部分拆分,数组长度为limit的极限

9、字符串替换

    a、public String replaceAll(String regex,String replacement):替换所有的制定内容

    b、public String replaceFirst(String regex,String replacement):替换首个内容

10、字符串截取

    a、public String substring(int beginIndex):从指定位置截取到结尾

    b、public String substring(int beginIndex,int endIndex):截取部分内容

二:Object类

1、Java中除了Object类外,其他所有的类都默认继承Object类,所以所有类的对象都可以使用Object进行接收

       比如下面这段程序,就用Object接受了Person类和Student类的对象。

class Person{

}
class Student{

}
public class Test7_6{
    public static void main(String[] args){
        fun(new Person());
        fun(new Student());
    }
    public static void fun(Object obj){
        System.out.println(obj);
    }
}

2、Object类的方法

    (1):取得对象信息的toString()方法

class Person{
    private String name;
    private int age;
    public Person(String name,int age){
        this.name = name;
        this.age = age;
    }
}
public class Test7_6{
    public static void main(String[] args){
        fun(new Person("xxx",18));//Person@15db9742
        fun("hello");             //hello
    }
    public static void fun(Object obj){
        System.out.println(obj.toString());
    }
}

    上面这段代码中,当传入一个对象时,打印出的是该对象的地址;如果传入一个字符串时,打印的是字符串的内容。

    当如果不想得到toString()默认给出的返回内容,可以对toString()方法进行覆写:

    比如我想得到对象的所有属性或者某个属性的信息:

class Person{
    private String name;
    private int age;
    public Person(String name,int age){
        this.name = name;
        this.age = age;
    }
    public String toString(){
        return "姓名:" + this.name + " 年龄:" + this.age;  //这里直接返回对象的name属性和age属性。
    }
}

public class Test7_6{
    public static void main(String[] args){
        fun(new Person("xxx",18));
    }
    public static void fun(Object obj){
        System.out.println(obj.toString());
    }
}

    (2)、对象比较

    前面我们在说String类的时候提到了equals()方法,实际上String类的equals()方法是对Object类中equals()方法的覆写。

class Person{
    private String name;
    private int age;
    public Person(String name,int age){
        this.name = name;
        this.age = age;
    }
    public String toString(){
        return "姓名:" + this.name + " 年龄:" + this.age;
    }
    public boolean equals(Object obj){
        if(obj == null){    //如果传入对象没有被实例化,直接返回false
            return false;
        }
        if(obj == this){    //如果传入对象是当前对象,直接返回true
            return true;
        }
        if(!(obj instanceof Person)){    //如果传入对象不是Person类的实例化对象直接返回false
            return false;
        }
        Person per = (Person)obj;//走到这里说明传入对象是Person类的对象,将obj向下转型
        return this.name.equals(per.name) && this.age == per.age;  //比较传入对象的各属性内容,如果全相同返回true,否则返回false
    }
}

class Student{}

public class Test7_6{
    public static void main(String[] args){
        Person per1 = new Person("xx",18);
        Person per2 = new Person("yy",18);
        Person per3 = new Person("xx",18);
        Student stu = new Student();
        Person per  = null;
        System.out.println(per1.equals(per2));//false per1与per2的姓名属性不同,返回false
        System.out.println(per1.equals(per3));//true  per1与per3的所有属性相同,返回true
        System.out.println(per1.equals(per1));//true  per1与自己相比,直接返回true
        System.out.println(per1.equals(stu));//false  stu不是Person类的实例化对象,返回false
        System.out.println(per1.equals(per));//false  per没有进行实例化,返回false
    }
}

3、Object接收引用数据类型

    Object类可以接收引用数据类型,包括类,数组,接口。

public static void main(String[] args){
        Object obj = new int[]{0, 1, 2, 3, 4};
        int[] data = (int[])obj;
        for(int i :data){
            System.out.println(i + ",");
        }
    }

    运行结果  :  

0,
1,
2,
3,
4,

    这里用Object类接收了一个整型数组。

三:包装类

    上面说到,Object类可以接收所有的引用数据类型,可是Java中数据类型分为基本数据类型和引用数据类型。所以,包装类的存在是为了基本数据类型而生的。

    1、将基本数据类型包装为一个类对象的本质就是使用Object类进行接收和处理,看下面代码:

class IntDemo{
    private int num;
    public IntDemo(int num){
        this.num = num;
    }
    public int intValue(){
        return this.num;
    }
}
public class Test7_6{
    public static void main(String[] args){
        Object obj = new IntDemo(10);       //向上转型       
        IntDemo num = (IntDemo)obj;         //向下转型
        System.out.println(num.intValue()); //10
    }
}

    这里自定义了一个包装类。

    2、装箱和拆箱:

    装箱:将基本数据类型变为包装类对象,利用每一个包装类提供的构造方法实现装箱处理。

    拆箱:将包装类中的基本数据类型取出,利用Number类中提供的六种方法。

    public static void main(String[] args){
        Integer num = new Integer(20);      //将20用Integer类进行装箱
        int data = num.intValue();          //用intValue()方法进行拆箱    
        System.out.println(data);
    }
    **在拆装箱这里依然存在着“equals”和“==”问题:
    public static void main(String[] args){
        Integer num1 = new Integer(10);
        Integer num2 = new Integer(10);
        System.out.println(num1 == num2);                 //false
        System.out.println(num1 == new Integer(10));      //false
        System.out.println(num1.equals(new Integer(10))); //true
        System.out.println("*********************");
        Integer num3 = 10;
        Integer num4 = 10;
        System.out.println(num3 == num4);                 //true
        System.out.println("*********************");
        Integer num5 = 128;
        Integer num6 = 128;
        System.out.println(num5 == num6);                 //false
    }

    解释:“==”永远比较的是值,num1与num2存的是两个对象的地址,所以num1 == num2是错误的;num1 == new Integer(10)同理,地址不同,也是错误的。equals比较的是所在地址里的内容,所以num1.equals(new Integer(10)),是正确的。num3与num4,这里用到了自动拆装箱(在JDK1.5之后,提供的自动拆装箱机制,可以直接利用包装类的对象进行各种数学运算),这里直接比较数值,所以正确。num5与num6这里说明一下:在-128~128范围内赋值,Integer对象是在常量池内产生,会复用已有的对象,在这个区间内可以用“==”进行比较,但在这个区间之外的所有数据,都会在堆上产生,并不会复用已有对象,只能用equals比较。所以错误。

3、字符串类型转换为基本数据类型:

    String类->int类(Integer):public static int parseInt(String s)throws NumberFormatException

    String类->double类:public static double parseDouble(String s)throws NumberFormatException

    String类->Boolean类:public static parseBoolean(String s)

    这里以整型为例:

    public static void main(String[] args){
        String str = "123";
        int num = Integer.parseInt(str);
        System.out.println(num);//123
    }

4、基本数据类型转换为字符串类型:

    (1)、任意数据类型使用“+”连接一个空白字符串就将该数据类型转换成了字符串类型。

    (2)、使用String类提供的valueof()方法,这种方法不会产生垃圾。

    看下面代码:

    public static void main(String[] args){
        String str = String.valueOf(123);
        System.out.println(str);//123
    }

完!

猜你喜欢

转载自blog.csdn.net/weixin_41890097/article/details/80968365