Java:string detailed

Source: Internet
Author: User

The string class is used to store strings and is immutable.

1. The basic data type , also known as the original data type. Byte,short,char,int,long,float,double,boolean
The comparison between them, applying the double equals sign (= =), compares their values.

2. Composite data type (Class)
When they compare with (= =), they compare the storage address in memory, so unless it is the same new object, their comparison result is true, otherwise the result is false. Use the Str.equals (str2) method to compare the values of strings for equality.

3. The difference between the length property, the length () method, and the size () method:

    • Length Property- for An array, say you declare an array, you want to know that the length of the array is used for this property.
    • Length () Method- for string strings, the length () method is used if you want to see how long this string is.
    • size () method- for a generic collection , call this method to see if you want to see how many elements of the generic are available.

If you want to traverse a string:

String s = "ABCDE"= s.length ();  Note that there are brackets   for (int i=0; i<strlen;i++)    {//  ...}

or this:

 for (char  ch:s.tochararray ()) {      //  ...}

Char into String:

 String s = string.valueof (ch); //  The most efficient method  String s = string.valueof (new  char  [ ]{' C '}); //  String s = character.tostring (' C '); //  character.tostring (char) method actually directly returns String.valueof (char)  String s = new  Character (' C '  = new  String (new  char  []{' C '}); 
string s = "" + ' C '; //Although this method is simple, this is the least efficient method

< Span class= "hljs-comment" >< Span class= "Hljs-keyword" >java is actually immutable and is a final variable.

< Span class= "hljs-comment" >< Span class= "Hljs-keyword" > so every time we make any changes to the string, we initialize a completely new string object and point the original variable to the new string.

< Span class= "hljs-comment" >< Span class= "Hljs-keyword" >

< Span class= "hljs-comment" >< Span class= "Hljs-keyword" > String Direct add connection actually calls the following method:

New

In addition to string, you can also use the StringBuilder class or the StringBuffer class to store strings, so what is the difference between the three?

string- immutable, each modification does not modify the String itself, but an additional object is created. When strings are frequently manipulated, there are many temporary variables that can seriously affect performance.

stringbuffer- variable, thread-safe, slightly lower performance.

StringBuilder -variable, non-thread safe, high performance. Priority is given to using

StringBuilder to a String object:

New StringBuilder ();   = str.tostring ();

 Packagetest;Importjava.lang.*; Public classTest1 { Public Static voidMain (string[] args) {//to create an empty StringBuilder objectStringBuilder str =NewStringBuilder (); //Append StringStr.append ("JAEWKJLDFXMOPZDM"); //Insert a comma every three bits from the forward       For  (int i=str.length ()-3; i>=0;i=i-3) {Str.insert (I, ","); }        //converts a StringBuilder object to a string object and outputsSystem.out.print (str.tostring ()); }}

Java:string detailed

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.