Java Advanced Tutorials String class _java

Source: Internet
Author: User

The previous Java Basics series discussed Java's core concepts, especially object-oriented basics. In the Java advanced, I'll complement the Java basics and move on to the application level.

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

The string class is contained in the Java.lang package. This package is 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 use import to introduce the String class.

Creating a String

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

Copy Code code as follows:

public class Test
{
public static 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. Need attention when using.

String manipulation

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

Copy Code code as follows:

"ABC" + S

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

Copy Code code as follows:

Method effect

S.length () returns the length of the s string

S.charat (2) returns the character in the middle of the s string labeled 2

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

S.indexof ("Hello") returns the sub-string "Hello" subscript

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

S.endswith ("oo") determines if s ends with "oo"

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

= = can only determine if the string is saved 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 and "Hello nerd!" Equal.

S.trim () removes the space string before and after S, and returns a 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). The programmer cannot modify an existing Mutable object. We can 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 features are implemented by creating a new object, rather than modifying the original object. Like what:

Copy Code code as follows:

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

The call to the right of S.replace () creates a new string "Hello universe!" and returns the object (the reference). By assigning a value, the reference s will point to the new string. If no other reference points to the original string "Hello world!", the original string object is garbage collected.

Immutable objects

Java API

Java provides a number of powerful packages. An important aspect of Java learning is understanding these packages and the APIs they contain (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 Web site:

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, there is a wealth of content in the API documentation, and you can use the links below to overview:

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

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.