字符串的简单介绍和字符串的大小比较

以前就写过一篇关于String的文章,今天再来写一篇,更加深入了解一下String类

🕐1.String类的定义

🕑2.String类的创建

🕒3.字符串的大小比较

1.之前在C语言中我们已经学到了字符类型,但是C语言没有String类,但是在Java中有String类,今天我们就来说一说String类

如何定义一个字符串

可以直接

String   ret="wyb";

也可以

直接new一个,因为String类算一个类,是引用类型,直接通过构造一个对象,产生字符串,就像这样

String  str=new String("wyb ");

还可以,这个方法比前两个复杂一点

通过数组进行构造

比如

char  []ret ={w,y,b};

String str=new String(ret);

这句话可以理解为是把数组强制类型转换为字符串类型

String是一个引用类型,所以自己不会存字符串,那么存在哪里呢,打开它的源码有惊喜

可以看到String类是被final修饰的,那么说明不能被继承,它里面有两个成员变量

一个是由private修饰的value[]数组,另一个是private修饰的hash,new出来的字符串就放在value数组里面

就像上图中的例子一样,而hash存的是一个地址,具体在数据结构中我们再说

在Java中,只要被双引号引起来的变量就是字符串

下面来说一说字符串的比较

1 用==

在基本类型的比较中,==比较的就是内容是否相等,在引用类型中==比较的是所指对象的地址是否相等

public class TestDemo {
    public static void main(String[] args) {
        String s1="hello";
        String s2="hello";
        System.out.println(s1==s2);//true
        System.out.println("======");
        String s3=new String("hello");
        String s4=new String("hello");
        System.out.println(s3==s4);//false
    }
}

s1和s2就是普通的那样的比较,不是引用类型,所以直接比较内容即可,s3和s4是两个引用,所以需要比较地址

2.用equals方法

public class TestDemo {
    public static void main(String[] args) {
        String s1="hello";
        String s2="hello";
        System.out.println(s1==s2);//true
        System.out.println("======");
        String s3=new String("hello");
        String s4=new String("hello");
        System.out.println(s3==s4);//false
        System.out.println("====");
        System.out.println(s1.equals(s2));//true
        System.out.println(s3.equals(s4));//true
        System.out.println(s1.equals(s3));//true
        System.out.println(s1 == s3);//false
    }
}

 

equals比较的是其中的内容是否相等,最后一个s1和s3采用==比较一定是错误的,因为s1看内容,s2看指向,二者比较的东西都不一样,所以是错误的

我们再来看看equals方法

这是具体使用equals时具体实现的源码 ,可以看出笔记比较的是内容是否相同

3.用compareTo方法进行比较

public class TestDemo {

    public static void main(String[] args) {
        String s1=new String("abandon");
        String s2=new String("abandondefg");
        System.out.println(s1.compareTo(s2));
        System.out.println("======");
        String s3=new String("abcdrfg");
        String s4=new String("abcdefg");
        System.out.println(s3.compareTo(s4));
        System.out.println("=====");
        String s5=new String("abcd");
        String s6=new String("abcd");
        System.out.println(s5.compareTo(s6));
    }

先来看看compareTo的源码

 返回值是int ,具体实现是这样的,根据字典序来实现的,当出现不一样的字母时返回差值,如果两个字母不一样长,但是前面的字母都一样,就是多出来的不一样,那么就返回多出来的几个字母个数,如果字符串相等,返回0;

4.使用compareToIgnorceCase方法进行比较

这里就代表忽略大小写,和compareTo方法比较方式一样

先看看源码

 

 

public class TestDemo {
    public static void main(String[] args) {
        String s1=new String("WYB");
        String s2=new String("wyb");
        System.out.println(s1.compareToIgnoreCase(s2));
        String s3=new String("ABCDE");
        String s4=new String("abcdeuuu");
        System.out.println(s3.compareToIgnoreCase(s4));
        String s5=new String("ABcde");
        String s6=new String("Abyde");
        System.out.println(s5.compareToIgnoreCase(s6));
    }

 

来总结一下,比较字符串大小的方法

1.用==,比较的是两个内容是否相同

2.用 equals方法,比较的是引用所指对象的地址是否相同

3.用compareTo方法比较的是字母字典顺序,返回差值

4.用compareToIgnorceCase方法,和compareTo方法一样,就是忽略了大小写

今天的分享就到这里,我们下期再见啦,886

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值