Java api ----- String class and StringBuffer class, stringbufferapi

Source: Internet
Author: User

Java api ----- String class and StringBuffer class, stringbufferapi

The String and StringBuffer classes are mainly used to process strings. These two classes provide processing methods for many strings. The String class is an unchangeable class, indicating that the String class contained in the object cannot be changed. The StringBuffer class is a variable class. The string content contained in its object can be added or modified.

For the common methods for these two classes to process strings, see: http://www.runoob.com/java/java-string.html

Http://www.runoob.com/java/java-stringbuffer.html

The following describes the differences between equals and = in the use of strings and non-strings.

Class Test {int x = 1;} public class Test {public static void main (String args []) {String a1, a2, a3 = "abc ", a4 = "abc"; a1 = new String ("abc"); a2 = new String ("abc"); System. out. println ("a1.equals (a2):" + (a1.equals (a2); System. out. println ("a1 = a2:" + (a1 = a2); System. out. println ("a1.equals (a3):" + (a1.equals (a3); System. out. println ("a1 = a3:" + (a1 = a3); System. out. println ("a3.equals (a4):" + (a3.equals (a4); System. out. println ("a3 = a4:" + (a3 = a4); System. out. println ("--------------------------"); Test one = new Test (); Test two = new Test (); System. out. println ("one. equals (two): "+ (one. equals (two); System. out. println ("one = two:" + (one = two); one = two; System. out. println ("after assignment:"); System. out. println ("one. equals (two): "+ (one. equals (two); System. out. println ("one = two:" + (one = two ));}}

1. = can be used to compare the basic type and reference type to determine the content and memory address

2. equals can only be used to compare the reference type. It only determines the content.

Java data types can be divided into two types:
1. The basic data type, also known as the original data type. Byte, short, char, int, long, float, double, boolean
The comparison between them applies the double equal sign (=) to compare their values.
2. Composite data type (class)
When they use (=) for comparison, they compare their storage addresses in the memory,
Therefore, unless it is a new object, their comparison result is true, otherwise the comparison result is false.

 

String Buffer Pool: A string buffer pool is created when the program is running.
When String a3 = "abc"; is used to create a String, the program first searches for objects with the same value in the String buffer pool.

In String a3 = "abc";, a3 is first placed in the pool. When a4 is created, the program finds a4 with the same value and references the object "abc" referenced by a3 ".

Using the new operator, he clearly told the program: "I want a new one! Don't be old! "So a new" abc "Sting object is created in the memory. They have the same value but different positions.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.