current position:Home>Java FAQ -- string
Java FAQ -- string
2022-01-26 22:24:12 【Yan Long】
String is a common data type . stay Java in , Common string types include String
、StringBuffer
and StringBuilder
.
String
from String You can see the source code of ,String Use an array to store the contents of a string , Arrays use keywords final modification , Therefore, the contents of the array are immutable , Use String The value of the defined string is also immutable .
because String The value of type is immutable , So every time String Any modification will create a new String object , Leads to inefficiency and takes up a lot of memory space .
tips: string Data is stored in a constant pool , Not in the pile also Not in the stack .
StringBuffer and StringBuilder
StringBuffer and StringBuilder All are AbstractStringBuilder Subclasses of , Also use an array to store the contents of a string , Because the array does not use keywords final modification , Therefore, the contents of the array are variable ,StringBuffer and StringBuilder Are all variable types , You can modify the contents of the string , And no new objects will be created due to modification .
When the content of the string needs to be modified frequently , You should use StringBuffer or StringBuilder, Significantly better than... In terms of time and space String.
StringBuffer and StringBuilder What are the differences ? You can see from the source code ,StringBuffer Keywords are used for defined methods or called methods synchronized modification , and StringBuilder The method does not use keywords synchronized modification . because StringBuffer Add synchronization lock to the method , Therefore, its efficiency is slightly lower than StringBuilder, But in a multithreaded environment ,StringBuilder Thread safety cannot be guaranteed , therefore StringBuffer It's a better choice .
summary
String It's an immutable type , Every time the String Any modification will create a new String object , Leads to inefficiency and takes up a lot of memory space , therefore String For string constants , It is not suitable for situations that require a large number of modifications to the string . StringBuffer It's a variable type , You can modify the contents of the string without creating new objects , And StringBuffer It's thread safe , Suitable for multi-threaded environment . StringBuilder It's a variable type , And StringBuffer be similar , In a single threaded environment StringBuilder The efficiency is slightly higher than StringBuffer, But in a multithreaded environment StringBuilder Thread safety is not guaranteed , therefore StringBuilder Not suitable for multi-threaded environment .
copyright notice
author[Yan Long],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201262224096460.html
The sidebar is recommended
- Spring IOC container loading process
- [thinking] the difference between singleton mode and static method - object-oriented programming
- Hadoop environment setup (MySQL environment configuration)
- 10 minutes, using node JS creates a real-time early warning system for bad weather!
- Git tool
- Force deduction algorithm - 92 Reverse linked list II
- What is the sub problem of dynamic programming?
- C / C + +: static keyword summary
- Idea does not have the artifacts option when configuring Tomcat
- Anaconda can't open it
guess what you like
-
I don't know how to start this
-
Matlab simulation of transportation optimization algorithm based on PSO
-
MySQL slow log optimization
-
[Vue] as the window is stretched (larger, smaller, wider and higher), the text will not be displayed
-
Popular Linux distributions for embedded computing
-
Suzhou computer research
-
After installing SSL Certificate in Windows + tomcat, the domain name request is not successful. Please answer!!
-
Implementation time output and greetings of jQuery instance
-
The 72 year old uncle became popular. Wu Jing and Guo fan made his story into a film, which made countless dreamers blush
-
How to save computer research
Random recommended
- Springboot implements excel import and export, which is easy to use, and poi can be thrown away
- The final examination subjects of a class are mathematical programming, and the scores are sorted and output from high to low
- Two pronged approach, Tsinghua Professor Pro code JDK and hotspot source code notes, one-time learning to understand
- C + + recursive knapsack problem
- The use of GIT and GitHub and the latest git tutorial are easy to understand -- Video notes of crazy God speaking
- PostgreSQL statement query
- Ignition database test
- Context didn't understand why he got a high salary?, Nginxfair principle
- Bootstrap switch switch control user's guide, springcloud actual combat video
- A list that contains only strings. What other search methods can be used except sequential search
- [matlab path planning] multi ant colony algorithm grid map path planning [including GUI source code 650]
- [matlab path planning] improved genetic algorithm grid map path planning [including source code phase 525]
- Iinternet network path management system
- Appium settings app is not running after 5000ms
- Reactnative foundation - 07 (background image, status bar, statusbar)
- Reactnative foundation - 04 (custom rpx)
- If you want an embedded database (H2, hsql or Derby), please put it on the classpath
- When using stm32g070 Hal library, if you want to write to flash, you must perform an erase. If you don't let it, you can't write continuously.
- Linux checks where the software is installed and what files are installed
- SQL statement fuzzy query and time interval filtering
- 69. Sqrt (x) (c + + problem solving version with vs runnable source program)
- Fresh students are about to graduate. Do you choose Java development or big data?
- Java project: OA management system (java + SSM + bootstrap + MySQL + JSP)
- Titanic passenger survival prediction
- Vectorization of deep learning formula
- Configuration and use of private image warehouse of microservice architect docker
- Relearn JavaScript events
- For someone, delete return 1 and return 0
- How does Java dynamically obtain what type of data is passed? It is used to judge whether the data is the same, dynamic data type
- How does the database cow optimize SQL?
- [data structure] chain structure of binary tree (pre order traversal) (middle order traversal) (post order traversal) (sequence traversal)
- Webpack packaging optimization solution
- 5. Operation element
- Detailed explanation of red and black trees
- redhat7. 9 install database 19C
- Blue Bridge Cup notes: (the given elements are not repeated) complete arrangement (arrangement cannot be repeated, arrangement can be repeated)
- Detailed explanation of springboot default package scanning mechanism and @ componentscan specified scanning path
- How to solve the run-time exception of test times
- Detailed explanation of k8s management tool kubectl
- Android system view memory command