Java基础整理(十二)

本文详细解析了Java String类的基础概念,如内存管理、不可变性、常量池,以及构造方法、charAt、compareTo等关键方法的使用。讲解了字符串操作的易错点和技巧,包括equals与equalsIgnoreCase的区别,以及字符串操作中的常用API如contains、startsWith等。
摘要由CSDN通过智能技术生成

String类

1. 基本概念及内存

  • String表示字符串类型,属于引用数据类型,不属于基本数据类型

  • Java中随便使用双引号括起来的都是String对象

  • Java中规定双引号括起来的字符串是不可变的,也就是说“abc"自始至终都不变,都不可能变成”abcd“

  • 在JDK中双引号括起来的字符串直接存储在”方法区“中的”字符串常量池“中

    字符串存储在”字符串常量池“中的原因是:字符串在实际开发中使用频繁。为了提高效率,所以把字符串放到了方法区的字符串常量池中(JDK7以后字符串常量池好像移动到了堆当中)

  • 内存

    String s="abc"; //s保存的不是"abc"字符串,而是对象的内存地址
    

在这里插入图片描述

String s = new String("abc");

在这里插入图片描述

  • ” == “ 和 ”equlas"

    String s1="hello";
    String s2="hello";
    System.out.println(s1==s2); // true 比较的是变量中保存的内存地址
    

在这里插入图片描述

String s1=new String("hello");
String s2=new String("hello");
System.out.println(s1.equals(s2)); //false 地址不相同

在这里插入图片描述

  • 易错

    • String s1="abcdef";
      String s2="abcdef"+"xy";
      

      实际创建了三个对象 ”abcdef“、”xy"、”abcdefxy“,都存储在字符串常量池中

    • String k=new String("hello");
      System.out.println("hello".equals(k)); //推荐使用这种,避免空指针异常
      System.out.println(k.equals("hello")); //万一k为空指针 会报错
      
    • String s1 =new String("hello");
      String s2 =new String("hello");
      

      上述代码中创建了几个对象?

      三个,字符串常量池中有一个"hello",堆内存中有两个String对象


2. 构造方法

  • String s = new String("");

  • String s = “”;

  • String(字节数组)

    byte[] bytes = {97,98,99};
    String s2 = new String(bytes);
    System.out.println(s2); //abc
    
  • String(字节数组,数组元素的起始下标,长度)

    String s3 = new String(bytes,1,2) //bc
    
  • String(char数组)

    char[] chars = {'a','b','c'};
    String s4 = new String(chars); //abc
    
  • String(char数组,起始下标,长度)

    String s5 = new String(chars,1,2);//bc
    

3. charAt方法

//char charAt(int index)
char c ="中国人".charAt(1);//国  

"中国人"是一个字符串String对象,只要是对象就可以“.”


4. compareTo方法

对两个字符串进行比较,并且同时看出两个字符串的大小

//int compareTo(String anotherString)
int result1 = "abc".compareTo("abc"); //0
int result2 = "abcd".compareTo("abce");   //小于0  前小后大
int result3 = "abce".compareTo("abcd");   //大于0  前大后小

5. contains方法

判断前面的字符串是否包含后面的字符串

// boolean contains(CharSequence s)
"HelloWorld.java".contains(".java"); //true
"http://www.".contains("https://"); //false

6. endWith方法

判断当前字符串是否以某个字符串结尾

"text.txt".endWith(".java"); // false
"text.txt".endWith("xt"); //true

7. equalsfa方法

"abc".equals("abc"); //true

8. equalsIgnoreCase方法

判断两个字符串是否相等,并且同时忽略大小写

//boolean equalsIngoreCase(String anotherString)
"Abc".equalsIgnoreCase("abC"); //true

9. getBytes方法

//byte[] getBytes()
byte[] bytes = "abc".getBytes(); //97 98 99

10. indexOf方法

判断某个字符串在当前字符串中第一次出现处的索引

//int indexOf(String str)
"oraclejavac++".indexOf("java")  //6

11. isEmpty方法

判断某个字符串是否为“空字符串” 注意 不是null 是空字符串,因为底层源代码调用的是字符串的length()方法

//boolean isEmpty()
String s="a";
s.isEmpty() //false

12. length方法

判断数组长度是length属性,判断字符串长度是length()方法

//int length()
"abc".length(); //3

13. lastIndexOf方法

判断某个子字符串在当前字符串中最后一次出现的索引

// int lastIndexOf(String str)
"oraclejavagolangjavac++".lastIndexOf("java"); //16

14. replace方法

//String place(CharSequence target, CharSequence replacement) String的父接口是CharSequence
"http://www.baidu.com".replace("http://","https://") //https://www.baidu.com

15. startsWith方法

判断某个字符串是否以某个子字符串开始

//boolean startsWith(String prefix)
"http://www.baidu.com".startsWith("http") //true

16. substring方法

//String substring(int beginIndex)
"http://www.baidu.com".substring(7); //www.baidu.com

包括beginIndex,不包括endIndex

//String substring(int beginIndex, int endIndex)
"http://www.baidu.com".substring(7,10) //www

17. toCharArray方法

将字符串转换成char数组

//char[] toCharArray()
char[] chars = "我是中国人".toCharArray();

18. toLowerCase和toUpperCase

转换为小写

//String toLowerCase()
"ABCd".toLowerCase(); //abcd

转换为大写

//String toUpperCase()
"abcd".toUpperCase(); //ABCD

19. trim方法

去除字符串前后的空白

//String trim()
" hello   world  ".trim(); //hello   world

20. valueOf方法

将“非字符串”转换成“字符串”,底层调用toString()方法

String s1 = String.value(100);
String s2 = String.value(true);
String s3 = String.value(6.12);
String s4 = String.value(new Customer); //如果Customer类没有重写toString()方法就输出对象的内存地址 重写了就输出                                              toString()方法内的重写内容
  • 研究一下println()方法

    为什么输出一个引用时,会调用toString()方法?

    本质上System.out.println()这个方法输出任何数据类型时都先转换为字符串,再输出,它的底层代码会先去调用valueOf()方法,将对象转化为String对象,而valueOf()方法的底层又去调用了toString()方法

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值