java的String学习加JDK源码的学习_string r1="green"; string r2=r1;r1="blue";system

img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上C C++开发知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以戳这里获取

public final class String
 implements java.io.Serializable, Comparable<String>, CharSequence
//String是被final修饰的,因此它是不可变的,并且不能被继承,
并且它实现了许多接口。(具体什么不用管,(JDK里提供的接口)也可以按住Ctrl点击即可。
接口中就定义了许多,方法。并且只有方法的什么没有方法的实例;(接口的概念)
//private final char value[];
private权限修饰词,修饰则外部类是不能访问的,(私有的)
final;意味常量的意思,不能被改变被继承。
char value[];定义一个数组,这个数组是不可变的。
final修饰的词仅仅是value这个引用的内容,只能指向一个数组对象,但是它指向这个数组对象的值是可以改变的,
但是这个语句,数组里面的值也不可变,因为它是私有的,不能提供可变的接口。
 public String() {
        this.value = new char[0];
    }
//这个就是String类的构造器。
当String str1 = new String();//就是声请长度为1的字符数组;
—————————————————————————————————————————————————————————————
public String(String original) {
        this.value = original.value;
        this.hash = original.hash;
    }
 //这个构造方法就是可以传进字符串进来。
 String str2 = new String("sssss");//可以传入字符串进来。
 -------------------------------------------------------------
 public String(char value[]) {
        this.value = Arrays.copyOf(value, value.length);
    }
//构造方法,传递一个数组,但是这里有数组的copy。需要把这个数组Arrany.copyOf到我们这个类的数组里面。
    char[] c = {'a', 'v'};
    String str3 = new String(c);
//传递一个数组进来,在这个构造方法里面调用了Array.copyOF(),
 //Array类中public static char[] copyOf(char[] original, int newLength)进行赋值
    }
  public int length() {
        return value.length;
    }
   //length()这个方法就是把这个数组的长度返回来了。
 public boolean isEmpty() {
        return value.length == 0;
    }
    //如果长度为0那么就是空嘛。
public char charAt(int index) {
        if ((index < 0) || (index >= value.length)) {
            throw new StringIndexOutOfBoundsException(index);
        }
        return value[index];
    }
// charAt,就是返回索引指向的字符,从0开始;
//注意中间还穿插了一个如果索引不在范围之内就会抛出一个异常,
String strr = "abcder";
        System.out.println(strr.charAt(2));//返回的是c

关于public boolean equals(Object anObject) 方法
这里写图片描述

        String strr1 = new String("sjjdsf");
        String strr2 = strr1;
        System.out.println(strr2.equals(strr2));
        //返回tur,因为指向同一个对象。直接在第一部分就是返回ture了。
        --------------------------------------------------------
        String strr1 = new String("sjjdsf");
        String strr2 = new String ("sjjdsf");
        System.out.println(strr1.equals(strr2));//比较的是内容是否相等。
        System.out.println(strr1==strr2);//比较的是他们引用是否相等
        //顺便回忆一下,equal方法默认方法是与==一致的,因此一般都是需要重写的。

        String str = "absd";
        String str1 = "absd";
        System.out.println(str.equals(str1));//肯定是ture
//这个应该也是true;”abcd“双引号括起来的字符串是表示在常量池中。

来个内存图的分析;

String str = new String("abcd");
String str2 = new String("abcd");
String str3 = "def";
String str4 = "def";

这里写图片描述

indexOf()方法
 Returns the index within this string of the first occurrence of
     * the specified character.
 返回索引,在这个字符串中第一次出现的这个字符。
 public int indexOf(int ch) {//调用重载方法
        return indexOf(ch, 0);//0;表示这个字符串,从最开头开始找。
 }
重载方法;
 public int indexOf(int ch, int fromIndex) {//formIndex开始找,


String str = new String("abcd");
System.out.println(str.indexOf('b'));//返回索引位置,并且注意索引都是从0开始的;
System.out.println(str.indexOf('e'));//如果没有找到那么就返回-1;
lastIndextOF()//从后面开始找。
        System.out.println("Abcdb".indexOf('d'));//3
        System.out.println("Abcdb".lastIndexOf('b'));//4

public String substring(int beginIndex)//截取字符串的方法。返回时字符串对象;
* Returns a new string that is a substring of this string. The
     * substring begins with the character at the specified index and
     * extends to the end of this string. <p>
//返回一个子字符串对象,并且子字符串的对象从传人的索引这个值所值的字符开始截取,一直到字符串结尾。
//如果传人的值为负数,则会抛出异常。

public String substring(int beginIndex, int endIndex) //一个重载方法;
//表示的是从beginInder到endIndex的截取。

String str = new String("abcd");
String str1 = str.substring(2);
System.out.println(str1);//返回cd
 * Returns a new string resulting from replacing all occurrences of
     * <code>oldChar</code> in this string with <code>newChar</code>.
 //返回一个新的字符串对象,从把字符串中的newchar用oldchar代替的新字符串
 If the character <code>oldChar</code> does not occur in the
     * character sequence represented by this <code>String</code> object,
     * then a reference to this <code>String</code> object is returned.
//如果这个老的字符没有出现在字符串里面,那么就会返回原字符串对象的引用。、return this。
String str = new String("abcd");
String str1 = str.replace('a', '*');
System.out.println(str1);//返回*bcd

这里写图片描述

public String[] split(String regex) //对字符串进行切割,返回一个数组。
String str = "abcd,abc,dmhjkl,";
        String[] strArray = str.split(",");//按逗号进行切割。
        for(int i=0;i<strArray.length;i++){
            System.out.println(strArray[i]);
        }
public String trim() //去除首尾空格。
String str = " assdf aw da f ";
String str1 = str.trim();//去除首尾空格;
System.out.println(str1);//返回assdf aw da f
public char[] toCharArray() 
//就是将String对象中的数组返回,返回的是一个新的数组,这个数组就可以修改了。
public boolean equalsIgnoreCase(String anotherString)//忽略大小写进行比较
Compares this {@code String} to another {@code String}, ignoring case
* considerations.



System.out.println("ASs".equalsIgnoreCase("asS"));//返回true
public boolean startsWith(String prefix)//表示字符串是否以这个开始。
public boolean endsWith(String suffix)//表示字符串是否以这个结尾。


        System.out.println("Abcdb".startsWith("Ab"));//ture
        System.out.println("Abcdb".endsWith("db"));//true;
public String toLowerCase() //把字符串中的字母全变成小写
public String toUpperCase()//把字符串的字母全变为大写;
        System.out.println("ADs".toLowerCase());//ads
        System.out.println("ADS".toUpperCase());//ADS

常见的拼字符串代码,但是并不好,很浪费空间。因为要声明很多对象**

String gh = "g";//这句话就是有两个对象,“g”也单独算一个对象/
        for(int i = 0; i < 10; i++){//这里会创建9个对象。一起就是11个对象(常做面试题)
            gh += i;//相当于,gh = gh + i;


![img](https://img-blog.csdnimg.cn/img_convert/eb5f0363c2c0f02e33765bcbe7d909dd.png)
![img](https://img-blog.csdnimg.cn/img_convert/c5bf1edc77a9869bd23f9d18214a2666.png)

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上C C++开发知识点,真正体系化!**

**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**

**[如果你需要这些资料,可以戳这里获取](https://bbs.csdn.net/topics/618668825)**

片转存中...(img-uCpfZJeW-1715764767290)]

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上C C++开发知识点,真正体系化!**

**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**

**[如果你需要这些资料,可以戳这里获取](https://bbs.csdn.net/topics/618668825)**

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值