A summary of string usage in Java _java

Source: Internet
Author: User

The examples in this article summarize the use of strings in Java. Share to everyone for your reference. The specific analysis is as follows:

The essence of a string is an array of char types, but in Java, all strings declared with double quotes "" are objects of a string class. This is also a manifestation of Java's fully object-oriented language features.

String class

1. A String class object represents a constant string. It is immutable in length. that is, once an instance of a string class is created, the string represented by this instance cannot be changed. Similar to

str = str + "Hello";

This action essentially combines the two string objects of str with "Hello" to create a new string object and then assigns a reference to the new string object to Str. Extensive use of this operation can result in performance flaws. If you need to change the contents of a string frequently, you should use the StringBuffer class or the StringBuilder class. We can use a small program to see how much the performance difference between String and StringBuffer classes is.

Class Stringtest 
{public 
 static void Main (string[] args) 
 { 
  /** 
  * Executes 10 with String objects and StringBuffer objects, respectively) , 000 additional operations
  * Test Execution Time 
  * 2013.3.18 
  /String conststr = ""; 
  Long ltime = System.currenttimemillis (); 
  for (int i = 0; I < 10000 ++i) 
   conststr + = i; 
  System.out.println ("Const String:" + (System.currenttimemillis ()-ltime));
  StringBuilder strbuf = new StringBuilder ("");
  Ltime = System.currenttimemillis (); 
  for (int i = 0; I < 10000 ++i) 
   strbuf.append (string.valueof (i)); 
  System.out.println ("Buffered String:" + (System.currenttimemillis ()-ltime));
 } 

Execution results:

As you can see, the StringBuilder seconds are over, and the string class takes 300ms of time, and there is a huge difference in performance.

Both StringBuilder and stringbuffered represent a variable-length (mutable) string object, and the difference is that the Stringbuffered class does some security work on thread synchronization. And StringBuilder is vice versa. So if you're just programming on a single line, StringBuilder is a little bit more efficient than stringbuffered (the difference is really not big)

2, "Hello World" is a string object
we can use "Hello world" as an object directly, such as:

if ("Hello". Equals ("Hello")) 
   System.out.println ("Yes");

The output result is yes.

3, the string object comparison should use Equals () method, but cannot simply use = = to judge. the Equals () method compares the equality of strings in two objects, because = = compares the two reference names to the same instance.

A common question is how many string objects are generated by the following code fragment altogether?

String str1 = new String ("Hello");
String str2 = new String ("Hello");

Probably most people would answer, 2. But in fact there were 3 string objects. In addition to STR1 and str2, don't forget that "Hello" is a string object, too.

4. String pools (pool)

A string pool is maintained during Java execution. When a string is declared in double quotes, the JVM first checks for the existence of the same string object (the same character) in memory and, if it exists, returns a reference to an object that already exists in memory, and creates a new string object if it does not exist. This can save memory. As in the following code, STR1 and str2 are actually pointing to the same string object.

String str1 = "Hello";
String str2 = "Hello";

5. Receive command line parameters

When we declare the main method, we declare a formal parameter of the string[type. The array of this string object holds the command-line arguments that the user passes in when the program is executed. Note that unlike the C language, command-line arguments in Java start with the first variable after the name of the program. In other words, the string[] data does not include the application name. For example:

Class Strcmd 
{public 
 static void Main (string[] args)
 { 
  if (args.length > 0) 
  {for 
   (String Str:args) 
    System.out.println (str);}} 

The output for executing Java strcmd Hello world! is:

6, the separation of strings

Similar to the C language, the Strtok,string class also has a split method that separates a string in the specified format. The Split method returns an array of String objects, representing each segment of the string that is detached. Such as:

Class Strsplit 
{public 
 static void Main (string[] args)
 { 
  String str = "hello\tworld\ti\tlove\tyou!"; 
  System.out.println ("Original String:" + str);
   
  string[] Strarr = Str.split ("T");
  for (String S:strarr)
   System.out.println (s);
 } 
}

Where the parameter of the split () method can be a regular expression. You can use the static method matches () of the Matcher class in the Java.util package to determine whether a string conforms to a regular expression

There are more powerful features in the string class, and we should learn to read API documentation. The importance of English is embodied here,

I hope this article will help you with your Java programming.

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.