ArticleDirectory
Author: vamei Source: http://www.cnblogs.com/vamei welcome reprint, please also keep this statement. Thank you!
The previous Java basic series discussed the core concepts of Java, especially the basics of object-oriented. In the Java advanced tutorial, I will supplement the Java basics and turn to the application layer.
MajorityProgramming LanguageAll can be processedString). A string is an ordered Character Set combination, such as "Hello world! ". In Java, strings are storedString typeObject. You can call the string object method to perform string-related operations.
The string class is included inJava. LangPackage. This package will be started in JavaAutomaticSo it can be used asBuilt-in class(Built-in class ). We do not need to explicitly use import to introduce the string class.
Create a string
We used classes to create objects. Create a String object.The new keyword is not required.. For example:
Public ClassTest {Public Static VoidMain (string [] ARGs) {string s= "Hello world! "; System. Out. println (s );}}
In fact, when you write a "hello World" expression, this object is already created in the memory. If you use new string ("Hello world! "), A string object is created again.
An object
The string class isThe only one that does not require the New KeywordTo create an object class. Pay attention to the usage.
String operation
Available+Implement stringConnection(Concatenate), for example:
"ABC" + S
String operations are mostly implemented through the corresponding methods of strings, such as the following method:
Method Effect
S. Length () returns s String Length
S. charat (2) returns the characters marked as 2 in the S string.
S. substring (0, 4) returns the substring from 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") determines if s ends with "oo"
S. Equals ("good world! ") Judge whether s is equal to" Good world! "
=You can only determine whether a string is saved in the same position. You need to use equals () to determine whether the content of the string is the same.
S. compareto ("Hello nerd! ") Compare the S string with" Hello nerd! "Order in the dictionary,
Returns an integer. If <0, the value of S is "Hello nerd! "Before;
If it is greater than 0, it means S is in "Hello nerd! "Later;
If = 0, it indicates s and "hello nerd! "Equal.
S. Trim () removes spaces before and after S and returns a New String
S. touppercase () converts s to uppercase letters and returns a new string.
S. tolowercase () converts s to lowercase and returns a new string.
S. Replace ("world", "Universe") Replace "world" with "Universe" and return a New String
Immutable object
The string class object isImmutable object(Immutable object ).ProgramYou cannot modify existing immutable objects. We can also create immutable objects by ourselves, as long as the interface does not provide a method to modify data.
However, string objects do 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. For example:
S = S. Replace ("world", "Universe ");
The call to S. Replace () on the Right creates a new string "Hello universe! ", And return this object (reference ). By assigning values, referencing s will point to the new string. If no other reference points to the original string "Hello world! ", The original string object will be recycled.
Immutable object
Java API
Java provides many powerful packages. An important aspect of Java learning is understanding these packages andAPI(Application Programming Interface ). The string class is defined in Java. Lang. String. You can query the following Oracle website to find the official documents for this class:
Http://docs.oracle.com/javase/6/docs/api/java/lang/String.html
This document contains the most comprehensive introduction to the string class.
In fact, the API documentation contains a wealth of content. You can use the following link for an overview:
Http://docs.oracle.com/javase/6/docs/api/
Welcome to continue reading the "Java quick tutorial" series of articles