151 recommendations for writing high-quality code to improve Java programs--[52-57]string!about String How to use them?

Source: Internet
Author: User

Original Address: http://www.cnblogs.com/Alandre/ (mud and brick pulp carpenter), need to reprint, keep under! Thanks

Although the world was full of suffering, it was full also of the overcoming of it. -hellen Keller

I believe that I can read it, I believe I could write it, and I started to write it. In fact, it's simple.-Sediment brick and mortar carpenter

Written in the Font

Three pieces[52-3]:

52.suggestion:use the string direct value for the assignment [recommended use of String value assignment]

How to use the string, Stringbuffer,stringbuilder [correct usage of string, Stringbuffer,stringbuilder]

55.Easy Time:pay attention to the address of string [note the seat of the string]

57.Complex string manipulation using regular expressions [complex string operation using the normal form]

Suggestion:use the String direct value for the assignment

Do u knw the String Object? If u do some projects,u can see the String is used usually. A object is created by the key word:new. Therefore, we can create a String obejct by: "

New String ("Jeff"); ” .

Here, in my word,using the String direct value for the assignment is a better.

For example:

 Public class STRING01 {public    staticvoid main (string[] args)     {        String str1 = "Jeff";        String str2 = "Jeff";        New String ("Jeff");        String STR4 = Str3.intern ();                Boolean B1 = (str1 = = str2);        boolean b2 = (str1 = = STR3);        Boolean B3 = (str1 = = STR4);                System.out.println ("B1:" +b1+ ""  + "B2:" +b2+ "  " + "B3:" +b3+ "  ");}    } #outputs: B1:true  B2:false  B3:true

B1:true B2:false
U'll think, thats why they r different.
As we all kno, the operator "= =" Show whether the objects ' address references is the same. Java designed a string Pool for storing all the string used to avoid there is to many String Objects created in a system.  So string str3 =new string ("Jeff"); is creating a object in Javaheap memory not the String Pool.

Intern () is a method of String. We can see from the JDK for help Doc.

 Public  for class if  This  This  This String object is returned.

It follows that for any of the strings s and T, s.intern () = = T.intern () is true if and only if S.equals (t) is true.

All, using String str = "Jeff"; U dont mind the thread-security or garbage collection mechanism. String is a nice man, treat it as a little boy Pelasse.

How to use the String, Stringbuffer,stringbuilder

Look at the Pic:

String, StringBuffer, StringBuilder implement the charsequence.but they is different.

String
String Object is a non-variable. It can ischanged and in the memory when u create it.

For example:

    String str  = "abc";    String str1 = str.substring (1);            System.out.println ("str1" + str1);
#outputs: BC

SUBSTRING () method creates a new String Object and links the reference of it to str1. But when "str.substring (0)", str1 and Str both link to the ' ABC ' by the JVM.

StringBuffer StringBuilder

They is very similar and they r variables of the sequence of characters. Only different, the StringBuffer have the methods which are synchronized where necessary. String buffers is safe for use by multiple threads. Different from String, if z refers to a String of buffer object whose current contents start is "", then the method call would cause the string buffer to contain " startle ", whereas z.insert(4, "le") would alter the string buffer to contain " starlet ".

All in all:

String can is used for the constants.

StringBuffer can used for some operating methods in multithreaded Environment.likeXML analyze, the Parameters O F HTTP analyze etc.

StringBuilder can used for hql/sql splice,JSON package etc.

Easy Time:pay attention to the address of String

For example:

 Public Static void Main (string[] args) {    String str1 = 1 + 2 + "apples";    String str2 = "Apples" + 1 + 2;        System.out.println (STR1);    System.out.println (STR2);} #outputs: 3APPLESAPPLES12

What are we can see from the result-values.why? How? They did.

Because the JAVA handling mechanism to the operator "+". When there was a string in the expression, the expression data would change itself to the string class.if the Object, it would call its ToString method.

So,string str1 = 1 + 2 + "apples" just like String str1 = (1 + 2) + "apples". thats all.

Complex string manipulation using regular expressions

Just reading!! The part, I'll write in the future.

Write to Reader

Based on.

Thank u!

Tao can also, non-Heng Dao also. Name may also, non-permanent name also. Nameless, the beginning of everything is also famous, the mother of all things. Therefore, the constant no desire also, to view its Miao, constant desire also, to view its total self abandonment. The two are the same, the different name is same. Convoluted, the gate of the Miao.

151 recommendations for writing high-quality code to improve Java programs--[52-57]string!about String How to use them?

Related Article

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.