Java常用类

Java中常用的类

字符串相关的类

String类:

  • String:字符串,使用一对""引起来表示。
    1. String声明为final的,不可被继承
    2. String实现了Serializable接口:表示字符串是支持序列化的。实现了Comparable接口:表示String 可以比较大小
    3. String内部定义了final char[] value用于存储字符串数据
    4. String:代表了不可变的字符序列。简称:不可变性
      • 当对字符串重新赋值时,需要重写指定内存区域赋值,不能使用原有的value进行赋值
      • 当对现有的字符串进行连接操作时,也需要重新制定内存区域赋值,不能使用原有的value进行赋值
      • 当调用String的replace()方法修改指定字符或字符串时,也需要重新指定内存区域赋值。
    5. 通过字面量的方式(区别于new)给一个字符串赋值,此时的字符串值声明在字符串常量池中。
    6. 字符串常量池中是不会存储相同内容的字符串的。
  • String对象的创建
String str = "hello";
//本质上this.value = new char[0];
String s1 = new String();
//this.value = original.value;
String s2 = new String(String original);
//this.value = Arrays.copyOf(value,value.length);
String s3 = new String(char[] a);
String s4 = new String(char[] a,int startIndex,int count);
  • 面试题:String s = new String("abc");方式创建对象,在内存中创建了几个对象?
    • 两个:一个时堆空间中new结构,另一个时char[]对应常量池中的数据:"abc"
	String s1 = "javaEE";
	String s2 = "hadoop";
	String s3 = "javaEEhadoop";
	String s4 = "javaEE"+"hadoop";
	String s5 = s1+"hadoop";
	String s6 = "javaEE"+s2;
	String s7 = s1+s2;
	String s8 = s5.intern();//返回值得到的s8使用的常量池中已经存在的"javaEEhadoop"
	System.out.println(s3==s4);//true
	System.out.println(s3==s5);//false
	System.out.println(s3==s6);//false
	System.out.println(s3==s7);//false
	System.out.println(s3==s8);//true
	//结论:
	//常量与常量的拼接结果在常量池。且常量池中不会存在相同内容的常量。
	//只要其中有一个是变量,结果就在堆中
	//如果拼接的结果调用intern()方法,返回值就在常量池中
  • 面试题
    image

  • String中常用的方法:

    • int length():返回字符串的长度:return value.length
    • char charAt(int index):返回某索引处的字符return vvalue[index]
    • boolean isEmpty():判断是否为空字符串:return value.length == 0
    • String toLowerCase():使用默认语言环境,将String中所有字符转换为小写
    • String toUpperCase():使用默认语言环境,将String中的所有字符转换为大写
    • String trim():返回字符串的符本,忽略前导空白和尾部空白
    • boolean equals(Object obj):比较字符串的内容是否相同
    • boolean equalsIgnoreCase(String anotherString):与equals方法类似,忽略大小写
    • String concat(String str):将指定字符串连接到此字符串的结尾。等同于用"+"
    • int compareTo(String anotherString):比较两个字符串的大小
    • String substring(int beginIndex):返回一个新的字符串,它是此字符串从beginIndex开始截取到最后一个子字符串
    • String substring(int beginIndex,int endIndex):返回一个新字符串,它是此字符串从beginIndex开始截取到endIndex(不包含)的一个子字符串。)
    • boolean endsWith(String suffix):测试此字符串是否以指定的后缀结束
    • boolean startsWith(String prefix):测试此字符串是否以指定的前缀开始
    • boolean startsWith(String prefix,int toffset):测试此字符串从指定索引开始的子字符串是否以指定前缀开始
    • boolean contains(CharSequence s): 当且仅当此字符串包含指定的char值序列时,返回true
    • int indexOf(String str):返回指定子字符串在此字符串中第一次出现处的索引
    • int indexOf(String str,int fromIndex):返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始
    • int lastIndexOf(String str):返回指定子字符串在此字符串中最右边出现处的索引
    • int lastIndexOf(String str,int fromIndex):返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索
      注: indexOf和lastIndexOf方法如果未找到都是返回-1
    • String replace(char oldChar,char newChar):返回一个新的字符串,它是通过newChar替换此字符串中出现的所有oldChar得到的
      image
  • String与基本数据类型、包装类之间的转换

    • String --> 基本数据类型、包装类:调用包装类的静态方法:parseXxx(str)
    • 基本数据类型、包装类-->String:调用String 重载的valueOf(xxx)
    • 字符数组 -> 字符串
      • String类的构造器:String(char[])和String(char[],int offset,int length)分别用字符数组中的全部字符和部分字符创建字符串对象。
    • 字符串->字符数组
      • public char[] toCharArray():将字符串中的全部字符存放在一个字符数组中的方法。
      • public void getChars(int srcBegin,int srcEnd, char[] dst,int desBegin):提供了将指定索引范围内的字符串存放到数组中的方法。
      String str1 = "abc123";
      char[] charArry = str1.toCharArry();
      for(int i = 0;i<charArray.length;i++){
      	System.out.println(charArray[i]);
      }
      char[] arr = new char[]]{'h','e','l','l','o'};
      String str2 = new String(arr);
      System.out.println(str2);
      
      • String 与字节数组转换
        • 字节数组->字符串
          • String(byte[]):通过使用平台的默认字符集解码指定的byte数组,构造一个新的String。
          • String(byte[],int offset,int length):用指定的字节数组的一部分,即从数组起始位置offset开始取length个字节构造一个字符串对象
        • 字符串->字节数组
          • public byte[] getBytes():使用平台的默认字符集将此String编码为byte序列,并将结果存储到一个新的byte数组中
          • public byte[] getBytes(String charsetName):使用指定的字符集将此String编码到byte序列,并将结果存储到新的byte数组。
          /*String 与byte[]之间的转换
          编码:String --> byte[]:调用String的getBytes()
          解码:byte[]-->String;调用String的构造器
          编码:字符串到字节
          解码:编码的逆过程,字节-->字符串
          说明:解码时,要求解码使用的字符集必须与编码时使用的字符集一致,否则会出现乱码。
          */
          String str1 = "abc123中国";
          byte[] bytes = str1.getBytes();//使用默认的字符集,进行转换
          System.out.println(Arrays.toString(bytes));
          byte[] gbks = str1.getBytes("gbk");//使用gbk字符集进行编码
          System.out.println(Arrays.toString(gbks));
          System.out.println("***********************");
          String str2 = new String(bytes);//使用默认的字符集,进行编码。
          System.out.println(str2);
          String str3 = new String(gbks);
          System.out.println(str3);//出现乱码。原因:编码集和解码集不一致!
          String str4 = new String(gbks,"gbk");
          System.out.println(str4);
          

image
image
image

/*
    String、StringBuffer、StringBuilder三者的一同
    String:不可变的字符序列;底层使用char[]存储
    StringBuffer:可变的字符序列;线程安全的,效率偏低;底层使用char[]存储
    StringBuilder:可变的字符序列;线程不安全,效率高;底层使用char[]存储
     */

image
image
image
image

posted @ 2022-05-15 21:36  生活的样子就该是那样  阅读(70)  评论(0编辑  收藏  举报