Java string, java string Truncation

Source: Internet
Author: User

Java string, java string Truncation
Java string

I read the Java string these days, so I wrote it as a note.

1. String and StringBuilder

First, String is immutable, and StringBuilder is variable. Therefore, there must be a difference in usage. When a String changes frequently, it is best to use StringBuilder. It is better to use String when a String is rarely changed. The following example compares the performance of a frequently changed String when using String and StringBuilder respectively.

// Use Stringpublic class Test1 {public static void main (String [] args) {String str = ""; // The difference between for (int I = 0; I <10; I ++) {str = str + I; // The difference lies in this }}}
// Use StringBuilderpublic class Test2 {public static void main (String [] args) {StringBuilder str = new StringBuilder (); // difference in this for (int I = 0; I <10; I ++) {str = str. append (I); // The difference lies in this }}}

Let's take a look at the above Code into the Assembly format:

// Use String public static void main (java. lang. string []); Code: 0: ldc #2 // String 2: astore_1 3: iconst_0 4: istore_2 5: iload_2 // jump from row 33rd to this 6: bipush 10 8: if_icmpge 36 11: new #3 // class java/lang/StringBuilder 14: dup 15: invokespecial #4 // Method java/lang/StringBuilder. "<init>" :() V repeatedly generates many StringBuilder objects 18: aload_1 19: invokevirtual #5 // Method java/lang/StringBuilder. append :( Ljava/lang/String;) Ljava/lang/StringBuilder; 22: iload_2 23: invokevirtual #6 // Method java/lang/StringBuilder. append :( I) Ljava/lang/StringBuilder; 26: invokevirtual #7 // Method java/lang/StringBuilder. toString :() Ljava/lang/String; 29: astore_1 30: iinc 2, 1 33: goto 5 jump to line 1 36: return
// Use StringBuilder public static void main (java. lang. string []); Code: 0: new #2 // class java/lang/StringBuilder 3: dup 4: invokespecial #3 // Method java/lang/StringBuilder. "<init>" :() V initializes a StringBuilder object 7: astore_1 8: iconst_0 9: istore_2 10: iload_2 11: bipush 10 13: if_icmpge 28 16: aload_1 17: iload_2 18: invokevirtual #4 // Method java/lang/StringBuilder. append :( I) Ljava/lang/StringBuilder; 21: astore_1 22: iinc 2, 1 25: goto 10 // jump to line 28: return

First, I do not understand assembly, but the goto of the above two compilation codes can understand it. He just jumps to other lines after execution, that is, the loop we wrote, we can see that Java helps us replace String with StringBuilder. This is an optimization made by Java considering its own performance. But the optimization is good, because we use String to repeatedly change a String in a loop, this actually generates a series of String objects, which are not changed after Java optimization. A StringBuilder object is generated for each loop. When we use StringBuilder, we find that a StringBuilder object is generated before the loop, and no new objects are generated within the loop. Obviously, using StringBuilder directly is of higher performance.

Ii. Format Output 1. The simplest format output

Use System, out. printf (); // as in C.
Or use System. out. format ();
In fact, the implementation of the former is to call the latter

2. Use the Formatter class

During initialization, you can set where to write the output, which can be a display or file.
Formatter f = new Formatter (System. out); // standard output
F. format (); // control the output format, similar to c, but more powerful. For example, the following code shows the time

Calendar c = new GregorianCalendar (); Formatter f = new Formatter (System. out); f. format ("Date: % 1 $ tY-% 1 $ tm-% 1 $ te \ n", c); // % 1 $ indicates displaying the first given data, t indicates the display time. Yme indicates the year, month, and date in the month respectively.

Result example: Date: 2015-06-1

3. Construct A formatted string

Use String. format (); The returned value is a formatted String. The preceding example is modified.

Calendar c = new GregorianCalendar (); Formatter f = new Formatter (System. out); String s = String. format ("Date: % 1 $ tY-% 1 $ tm-% 1 $ te", c); f. format ("% s \ n", s );

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.