Java String Collation notes (i)

Source: Internet
Author: User

1. String

Strings are concatenated by a number of characters, and the Java string can consist of any character, but these characters must be contained within a pair of "" "" (double quotation marks). Java uses the classes in the Java.lang package to create strings, so strings are a string class

The instance object.

1.1. String constants

Many of the characters contained in Java by "" "are strings and cannot be used for other types, for example," 20+30 "can never be equal to 50 because it is a string constant instead of an integer.

1.2. Declaring string variables

Its syntax format is: String str [=null];

Note: A Java object (the string is also a Java object) must be initialized before it can be used, or the compiler will report an error "The used variable is not initialized".

1.3. Create a string

The sample code is as follows:

String str= "Hello";

Assign the "Hello" string constant directly to the STR variable so that the STR variable is initialized and ready to be used.

There are two common ways to construct the string class:

1.3.1. String () Construction method

This method creates a new string object, which is the default constructor for the string class, does not receive any arguments, and the constructed object is an empty string. Its syntax is as follows:

New String ();

Where "new" is a keyword for Java syntax that is used to create an instance object of the class specified after the keyword.

Note: An empty string does not say that its value is equal to null (null value), that an empty string and null (null value) are two concepts. An empty string is defined by an empty "" "symbol, which is a string object after instantiation, but does not contain

Any character. For example: "A" +new String () + "B", after execution the result is: "Ab". A null (null value) is not an instance of a string, it is a constant, but does not contain anything, such as a number, a character, or an empty string.

For example: "A" +null+ "B", after execution the result is: "ANULLB". As you can see, null (null value) and string concatenated are converted to the string "null", otherwise it is nothing.

1.3.2. String (byte[] bytes) Construction method

The construction method constructs a new string using the current platform's default character set decoding and the specified byte number (byte number), and the length of the new string is not necessarily equal to the length of the byte number.

The syntax is as follows:

New String (ByteArray);

The demo example is as follows:

Java String Collation notes (i)

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.