深刻理解String的關鍵點和方法

  String是Java開發中最最多見的,本篇博客針對String的原理和經常使用的方法,以及String的在開發中常見問題作一個總體性的歸納整理.由於以前對String的特性作過一些分析,因此不在詳細描述,以分條的形式html

整體記錄。java

一 :String變量保存位置

   Java JVM實現中採用 用永久代保存字符串常量池,字符串常量池中實際上保存的是 String對象引用,字面量形式定義的字符串 (String s1 = "hehe") 會先判斷字符串是否已存在。json

   可是 若是是變量的形式 或者是 new獲得的,會保存到堆中 。數組

   s1 s2 都是放在常量池 。s4 s5 都是運行時才能夠獲得值,是存在堆中的。app

String s1 = "haha";
String s2 = "haha";
System.out.println(s1 == s2);//true
String s3 = "ha";
String s4 = s3+"ha";
System.out.println(s1 == s4); //false

String s5 = new String("haha");
System.out.println(s5 == s1);//false

二 :String變量不可變 

    從JVM角度看,String由於用途廣,爲提升效率,每次都先去常量池中判斷String是否存在,若是存在就複用該對象。不存在時再去添加,達到共享String,提升效率的目的。工具

三:String.intern()方法

    官網描述: 當調用 intern 方法時,若是池已經包含一個等於此 String 對象的字符串(用 equals(Object) 方法肯定),則返回池中的字符串。不然,將此 String 對象添加到池中,並返回此 String 對象的引用測試

四:String 使用亂碼解決

    由於編碼差別,常常會出現中文字符串亂碼的問題。這是須要對String添加編碼的控制ui

     String.getBytes(String decode)方法會根據指定的decode編碼返回某字符串在該編碼下的byte數組表示編碼

    String的getBytes()方法是獲得一個字串的字節數組,這是衆所周知的。但特別要注意的是,本方法將返回該操做系統默認的編碼格式的字節數組spa

    而與getBytes相對的,能夠經過new String(byte[], decode)的方式來還原這個「中」字時,這個new String(byte[], decode)實際是使用decode指定的編碼來將byte[]解析成字符串

public static void main(String[] args) throws UnsupportedEncodingException {
         String s1 = "測試亂碼問題";
         String s2 = new String(s1.getBytes("GBK"),"GBK");
         System.out.println(s2);
    }

五:String 和其餘數據類型之間轉化 

 整數轉爲String ,能夠經過  「重載過的+運算符 」 實現,或者是  「調用Integer.toString()」,或者是「直接用String.valueOf(int)」

   String 轉爲Integer  Integer.parseInt(String)   或者 Integer.valueOf(s1)

 必定注意 「可能會拋出轉換異常」

 Integer i1 = 123;
// String s1 = ""+i1;
 String s1 = Integer.toString(i1);
 System.out.println(s1 instanceof  String);//true
System.out.println(s1.equals("123"));//true

 備註 : 結合第一條說的 利用+  將int轉爲String  會產生兩個String對象  ,因此推薦用Integer.toString()  

六:String 和String數組 和Char數組和byte數組的關係

   1:String 和 cahr[]數組 : 惟一的關聯大概就是 String 實際上採用 char數組保存 ,下面是 我反編譯String的源碼。

    其次String 和char的區別就是 char只保存單個字符,String保存的是多個字符

public final class String
    implements java.io.Serializable, Comparable<String>, CharSequence
{
    /** The value is used for character storage. */
    private final char value[];
....

  2:String 和byte[]數組

     主要解決中文亂碼問題

  3: byte 和char  (能夠參考  「char與byte的區別」 )

      byte是字節數據類型有符號型的、佔1個字節、大小範圍爲-128——127

      char是字符數據類型無符號型的、佔2個字節(unicode碼)、大小範圍爲0-65535

     因此 :

        char 不能夠表示負數,byte是有符號型的,能夠表示-128—127 的數 

char c =  (char) -3; // char不能識別負數,必須強制轉換不然報錯,即便強制轉換以後,也沒法識別  
        System.out.println(c);  
        byte d1 = 1;  
        byte d2 = -1;  
        byte d3 = 127; // 若是是byte d3 = 128;會報錯  
        byte d4 = -128; // 若是是byte d4 = -129;會報錯  
        System.out.println(d1);  
        System.out.println(d2);  
        System.out.println(d3);  
        System.out.println(d4);

      char能夠表中文字符,byte不能夠 。但我試圖去打印byte表示的漢字獲得的是 ASCII碼,當我試圖轉化爲char時發現並無的到中

    byte c1 = (byte)'中';
        System.out.println(c1);//45
    System.out.println((char)c1);//-    

         char、byte、int對於英文字符,能夠相互轉化

    byte c1 = (byte)'c';
    System.out.println((char)c1);//c
    System.out.println(c1);//99

  4:byte[] 和char的相互轉化 : 參考 「Java 中byte 與 char 的相互轉換」 。由於char直接強轉 byte 失敗 (byte一位而char是兩位),因此char --byte[]--char是能夠的

class Test{
        public static byte[] charToByte(char c) {
            byte[] b = new byte[2];
            b[0] = (byte) ((c & 0xFF00) >> 8);//高位
            b[1] = (byte) (c & 0xFF);
            return b;
        }
     public static char byteToChar(byte[] b) {
            char c = (char) (((b[0] & 0xFF) << 8) | (b[1] & 0xFF));
            return c;
        }
         public static void main(String[] args) {
                char c = '中';
                System.out.println(Test.byteToChar(Test.charToByte(c)));
        }
    }

  參考 : 「& 0xFF 與 & 0xFF00 的做用 

七:String  在Json中的用法

  Json中常常用到字符串轉爲json對象或者是轉爲json數組,必定注意這裏說的是 在java後臺用到json的時候的作法。與javaScript半毛錢不要緊

   json工具庫萬千,轉化的方法也不一樣。在這裏,我不一一列舉,我用的是net.sf.json

八:經常使用的拼接字符串方式

   + : String s1 = "ad"+"bc';

     Stringbuilder    stringbuilder.append("asda");

九:String經常使用方法

   toLowerCase() 轉換爲小寫

   valueOf() 轉換爲字符串

   trim() 去掉起始和結尾的空格

   substring() 截取字符串

   indexOf() 查找字符或者子串第一次出現的地方

   toCharArray()

   getBytes()

     charAt() 截取一個字符 

   length() 字符串的長度

相關文章
相關標籤/搜索