My 19th day of learning Java in Shanghai Le byte

HUIYL1 2020-11-11 10:33:39
19th th day learning java


Java character string

stay Java in , Strings are treated as String Object handling of type

String Brief introduction to common methods

establish String Object method

String s1= “com”; Create a string object com, be known as s1
String s2=new String; Create an empty string object , be known as s2
String s3=new String(“com”); Create a string object com, be known as s3

 Insert picture description here

int length()、charAt(int index)、String substring(int beginIndex)、String substring(int beginIndex, int endIndex)

 Insert picture description here

package com.SH.comProj;

public class StringDemo1 {

public static void main(String[] args) {

// Define a string "JAVA Programming Basics "

String str="JAVA Programming Basics ";

// Print the length of the output string

System.out.println(" The length of the string is :"+str.length());

        //charAt(int index)

// Take out the characters ' cheng ' And the output

System.out.println(str.charAt(6));

// Take out the string " Programming Basics " And the output

System.out.println(str.substring(5));

// Take out the string " Programming " And the output

System.out.println(str.substring(5, 7));

}

}

int indexOf(int ch)、int indexOf(String str)、int lastIndexOf(String str)、int lastIndexOf(String str, formIndex)

package com.SH.comProj;

public class StringDemo2 {

public static void main(String[] args) {

// Define a string "JAVA Programming based , I like java Programming "

String str=new String("JAVA Programming based , I like java Programming ");

// To find the character 'A' First occurrence in string

System.out.println(" character 'A' First occurrence in string "+str.indexOf('A'));

// Find substring " Programming " First occurrence in string

System.out.println(" Substring " Programming " First occurrence in string "+str.indexOf(" Programming "));

// To find the character 'A' Last occurrence in string

System.out.println(" character 'A' Last occurrence in string "+str.lastIndexOf('A'));

// Find substring " Programming " Last occurrence in string

System.out.println(" Substring " Programming " Last occurrence in string "+str.lastIndexOf(" Programming "));

// In string index The value is 8 The position begins , Find substring " Programming " First occurrence

System.out.println(" In string index The value is 8 The position begins , Find substring " Programming " First occurrence "+str.indexOf(" Programming ", 8));

}

}

byte[] arrs=str.getBytes();

  • String and byte Conversion between arrays
  • Coding problem :GBK Coding and UTF-8 code
package com.SH.comProj;

import java.io.UnsupportedEncodingException;

public class StringDemo3 {

public static void main(String[] args) throws UnsupportedEncodingException {

// String and byte Conversion between arrays

// Define a string

String str=new String("JAVA Programming Basics ");

// Convert string to byte Array , And print out

byte[] arrs=str.getBytes("GBK");

for(int i=0;i<arrs.length;i++){

System.out.print(arrs[i]+" ");

}

System.out.println();

// take byte Array to string

String str1=new String(arrs,"GBK");

System.out.println(str1);

}

}

== and equals The difference between ( Address and content )

Memory location in memory

 Insert picture description here

package com.SH.comProj;

public class StringDemo5 {

public static void main(String[] args) {

// == and equals Differences in methods

// Define three strings , It's all about com

String str1="imooc";

String str2="imooc";

String str3=new String("imooc");

System.out.println("str1 and str2 It's the same thing ?"+(str1.equals(str2)));

System.out.println("str1 and str3 It's the same thing ?"+(str1.equals(str3)));

System.out.println("str1 and str2 The same address ?"+(str1==str2));

System.out.println("str1 and str3 The same address ?"+(str1==str3));

}

}

String The immutability of

  • String Once the object is created , Can't modify , It's immutable
  • The so-called modification is actually to create a new object , The memory space pointed to does not change
package com.SH.comProj;

public class StringDemo6 {

public static void main(String[] args) {

//String The immutability of

//String Once the object is created , Can't modify , It's immutable

// The so-called modification is actually to create a new object , The memory space pointed to does not change

String s1="com";

String s2="hello,"+s1;

//s1 No more pointing com The memory space , It points to "hello,com"

System.out.println("s1="+s1);

System.out.println("s2="+s2);

String s3=new String("hello,com!");

System.out.println(" Substring :"+s3.substring(0,5));

System.out.println("s3="+s3);

}

}

character string StringBuilder

  • String and StringBuilder The difference between :
    – String Have no variability , and StringBuilder Do not have
    – StringBuffer and StringBuilder Class can be modified many times , And don't generate new unused objects

Differences among the three :

  • String: Immutable string ;
  • StringBuffer: Variable string 、 Low efficiency 、 Thread safety
  • StringBuilder: Variable character sequence 、 Efficient 、 Thread unsafe ;
  • Suggest :
    – When strings are frequently manipulated , Use StringBuilder

StringBuilder and StringBuffer

  • They are basically similar
  • StringBuilder Class in Java 5 It was proposed that , It and StringBuffer The biggest difference between them is StringBuilder Is not thread safe ( Can't sync access )
  • because StringBuilder Compare with StringBuffer Speed advantage , So in most cases, it is recommended to use StringBuilder class . However, when the application requires thread safety , Must be used StringBuffer class
  • StringBuffer It's thread safe ,StringBuilder There is no
  • Comparison in execution speed :StringBuilder > StringBuffer

API file

 Insert picture description here

StringBuilder Common methods of class

 Insert picture description here

package com.SH.comProj;

public class StringBuilderDemo1 {

public static void main(String[] args) {

// Define a string " Hello "

StringBuilder str=new StringBuilder(" Hello ");

// stay " Hello " Add the following , Change the string to " Hello ,com!"

   // str.append(',');

   // str.append("com!");

    //System.out.println("str="+str);

System.out.println("str="+str.append(',').append("com!"));

// Change the string to " Hello ,cOM!"

// Two ways :

//1、 Use delete Methods to remove om, Then insert OM

//System.out.println(" After replacement :"+str.delete(4, 6).insert(4, "OM"));

//2、 Use replace Method to replace

System.out.println(" After replacement :"+str.replace(4, 6, "OM"));

// In string " Hello ,cOM" Remove from " Hello " And the output

System.out.println(str.substring(0,2));

}

}

Usage method

 Insert picture description here

summary

  • String and StringBuilder
  • This paper introduces the common methods of these two classes
  • stay String This paper introduces the in == and equals() Differences in methods
  • It introduces String and StringBuilder The difference between , It's mainly immutability
  • Through the study , Learn more about java file (API) Use
  • The difference between the three :

 Insert picture description here

版权声明
本文为[HUIYL1]所创,转载请带上原文链接,感谢

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云