Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Java String and StringBuffer and StringBuilder Comments     - How do you access Dropbox Linux command line (Linux)

- You really do need to know a variety of programming languages (Programming)

- CentOS 6.x and CentOS7 installation RPMforge (Linux)

- Bash environment is automatically install and initialize oh-my-zsh & autojump zsh (Linux)

- Ubuntu Telnet service settings (Linux)

- redis configuration in detail (English) (Database)

- Bitmap memory footprint of computing Android memory optimization (Linux)

- map and hash_map STL containers (Programming)

- Go constructed using an interpreted language (Programming)

- Docker Private Registry Installation Guide at CentOS6.X (Linux)

- Laravel configuration PhpStorm + Xdebug + Chrome settings Debug Environment (Server)

- To install Ganglia (Linux)

- Hadoop virtualization performance comparison and tuning experience (Server)

- The Sublime Text 3 configuration file (Linux)

- Talk about the Linux ABI compatibility Application (Linux)

- To configure linux transparent firewall (Linux)

- Developing a Web server yourself (Server)

- Linux regex awk Comments (Linux)

- PL / SQL in forall simple test (Database)

- JDK installation under CentOS (Linux)

 
         
  Java String and StringBuffer and StringBuilder Comments
     
  Add Date : 2018-11-21      
         
         
         
  Foreword

Recently I found that the quality of the team in terms of Java code is not high enough, ready to write some basic articles, for your reference.

First, the definition of

String is immutable character sequence.
StringBuffer is mutable sequence of characters.
StringBuilder is mutable sequence of characters.

The only difference between 1, StringBuffer and StringBuilder of

StringBuffer objects are thread-safe, which means that StringBuffer object can be simultaneously modify multiple parallel threads, because all of its methods are declared "synchronized (synchronous)."
StringBuilder class is not thread-safe class introduced in JDK 1.5 version, which means that all of its methods are asynchronous method.
 Thus, in the application of a single model, we should use StringBuilder, so the object will not be locked and unlocked, so performance will increase.
 In a single-threaded model, and performs operations in sequence, the object will not collapse.

2. When selected String and StringBuffer

If we do not need to modify the string stored in the same memory space, then we must use the String.
 And if you need to modify string stored in the same memory space, then we must use StringBuffer or StringBuilder.

3, String advantages and disadvantages

Advantages: different locations using String, if the string is modified, the modification is saved in memory, in this case, the memory will have a string value while the value of the original string and the modified.
 Cons: For each one of these operations, it will consume more memory, since it is the new string value stored in the memory space after modification. Therefore, it can cause performance problems.
 Solution: To solve this performance problem, developers should use StringBuilder or StringBuffer to achieve modification of the string, then converted to a String object to pass a string to the user.

4, StringBuffer and StringBuilder advantages and disadvantages

Advantages: StringBuffer and StringBuilder have better performance because they consume less memory, all modification operations on strings are stored in the same memory location.
 Disadvantages: original string value before the change will not be retained.

Second, create a String or StringBuffer object

Create a String object in two ways
1) directly create objects using string assignment
String str = "instance of java for us";

2) create an object using the String constructor
String str = new String ( "instance of java for us");

Creating StringBuffer object
 Use constructor
StringBuffer str = new StringBuffer ();

Create StringBuilder object
 Use constructor
StringBuilder str = new StringBuilder ();

Third, only the specified action in StringBuffer and StringBuilder

Appending the string may be performed, insert, delete, and other operations reverse
 Because String objects are immutable objects, so these operations can not be performed in a String object.

Fourth, the connection string

String object to use of the new string to connect to an existing string, there are two ways:

1, using the "+" operator

2, using the concat () method

The use of StringBuffer connection string, only one way: using the append () method
 Use StringBuilder connection string, only one way: using the append () method
 Sample code:
package com.ch;
public Class Demo {
public static void main (String args []) {
    String str = "Java";
    StringBuffer sb = new StringBuffer ( "Java");
    StringBuilder sbr = new StringBuilder ( "Java");

    System.out.println (str.concat ( "language"));
    System.out.println (sb.append ( "language"));
    System.out.println (sbr.append ( "language"));
    }
}

Output:
Java language
Java language
Java language

Fifth, compare

Object definition of equals () method is: a and b if the object is a reference to the same object, then a.equals (b) returns true, otherwise returns false.
 The String class overrides Object's equals () method, equals String object () method compares the contents, the contents are equal returns true.
StringBuffer and StringBuilder no rewrite equals () method, with the same definition of Object equals () method.
     
         
         
         
  More:      
 
- HBase Application Development Review and Summary of Series (Database)
- Some common regular expressions (Linux)
- Installation Atom text editor on Mint Ubuntu / Linux (Linux)
- tar decompression problems gzip: stdin: not in gzip format (Linux)
- How to clear the v $ archived_log view expiration information (Database)
- Linux folder and extract the differential file comparison techniques -rsync Magical (Linux)
- JavaScript event handling Detailed (Programming)
- Linux cut Command Study Notes (Linux)
- SVN hook code set to synchronize Web directory (Server)
- Ubuntu under VirtualBox virtual machine serial port settings (Linux)
- Java deserialization test (Programming)
- Protect your files, modify the Linux value Umask (Linux)
- Oracle database online redo logs are several methods of recovery of deleted (Database)
- How to use Android Studio to play more package names APK (Programming)
- Security of data to create a safe .mdb database (Linux)
- Linux operation and maintenance of the actual file system, file links (Linux)
- [Errno 4] IOError: [Errno ftp error] with yum appears as a workaround (Linux)
- MySQL Parameter Tuning Best Practices (Database)
- Install VMware Tools in Debian (Linux)
- Talk about Java EE Learning (Programming)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.