Java Basic Learning--19, String class

Source: Internet
Author: User

Java's core concepts, especially the object-oriented foundation, were discussed in the previous Java Foundation series. In the Java advance, I'll complement the Java Foundation and move to the application level.

Most programming languages have the ability to handle strings (string). The string is an ordered set of characters, such as "Hello world!". In Java, a string is stored as a string class object. A method that invokes a string object to implement string-related operations.

The string class is contained in the Java.lang package. This package will be automatically import when Java is started , so it can be used as a built-in class (built-in classes). We do not need to explicitly introduce the string class using import.

Create a string

We used classes to create objects before. When you need to be aware, creating a String class object does not require the new keyword. For example:

Class test{    void main (string[] args) {String s = "Hello world!" ; System.out.println (s); }}

In fact, when you write a "Hello world" expression, the object is already created in memory. If you use the new string ("Hello world!"), a string object is created repeatedly.

An Object

The string class is the only class that does not require the new keyword to create an object. Be careful when you use it.

string Manipulation

You can use + to implement a string connection (concatenate), such as:

"ABC" + S

The operation of the string is mostly implemented by the corresponding method of the string, such as the following method:

Method effect

S.length () returns s string length

S.charat (2) returns the characters in the S string labeled 2

S.substring (0, 4) returns a substring of 0 to 4 in the S string

S.indexof ("Hello") returns the subscript of the substring "Hello"

S.startswith ("") determines whether s starts with a space

S.endswith ("oo") to determine if S is "oo" end

S.equals ("Good world!") Determine if S equals "good world!"

= = can only tell if the string is stored in the same location. You need to use Equals () to determine whether the contents of the string are the same.

S.compareto ("Hello nerd!") Compare S string with "Hello nerd!" In the order of the dictionaries,

Returns an integer if <0, stating S in "Hello nerd!" Before

If >0, explain s in "Hello nerd!" After

If ==0, explain s with "Hello nerd!" Equal.

S.trim () Remove the space string around s and return the new string

S.touppercase () converts s to uppercase and returns a new string

S.tolowercase () converts s to lowercase and returns a new string

S.replace ("World", "Universe") replaces "world" with "Universe" and returns a new string

Immutable objects

The String class object is immutable (immutable object). A programmer cannot modify an existing immutable object. We can also create immutable objects ourselves, as long as we do not provide a way to modify the data in the interface.

However, the string class object does have the ability to edit strings, such as replace (). These editing functions are implemented by creating a new object, rather than modifying the original object. Like what:

s = S.replace ("World", "Universe");

The call to S.replace () on the right will create a new string, "Hello universe!", and return the object (the reference). By assigning a value, the reference s will point to the new string. If no other references point to the original string "Hello world!", the original string object will be garbage collected.

Immutable objects

Java API

Java offers a number of powerful packages. An important aspect of Java learning is understanding these packages and the APIs contained therein (application programming Interface). The String class is defined in java.lang.String. You can find the official documentation for this class by querying the following Oracle URLs:

Http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

This document contains the most comprehensive description of the string class.

In fact, the API documentation contains a wealth of content that you'll get at a glance through the following links:

http://docs.oracle.com/javase/6/docs/api/

Java Basic Learning--19, String class

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.