2016-03-05 113 views
-1

我想將字符串文字轉換爲全大寫字母。這裏是我的代碼:當字符串文字改變時會發生什麼?

// a and b are the same literal 
    String a = "Test"; 
    String b = "Test"; 
    // now I want to change all b's letter 
    // into uppercases, but fail. 
    b.toUpperCase(); 
    System.out.println("a = " + a + ", " + "b = " + b); 
    // print: a = Test, b = Test 

    String c = "Test1"; 
    System.out.println("c = " + c + " , c.toUpperCase() = " 
       + c.toUpperCase()); 
    // print: c = Test1 , c.toUpperCase() = TEST1 

    // change letters of "Test" literal 
    // into uppercase and success 
    System.out.println("Test".toUpperCase()); 
    // print: TEST 

我的問題是:1。 可以b沒有變成大寫之一,但c和​​即可。爲什麼?

我知道的是: 1. ab引用字符串池中的同一個對象。 2.字符串是不可變的,但它似乎與此問題無關。

+4

什麼讓你覺得你已經改變了'c'的任何東西?你已經顯示了'c.toUpperCase()'的*返回值*,就這些。這不會改變'c'的值(一個引用)或'c'的值所指的字符串中的數據。 –

+2

你在哪裏持有聲明結果//b.toUpperCase(); – 2016-03-05 12:12:58

+0

在Java中,'String'變量是_immutable_,所以一旦創建,就無法更改它們。 –

回答

4

字符串是不可變的。所以對於變化b:

b = b.toUpperCase(); 

每次你做一些改變一個字符串的東西,一個新的字符串對象被創建。所以你需要改變對象的參考。

1

字符串是不可改變的,但它似乎對這個問題不相關

其實,這是對問題

b不能變成大寫一個

非常相關

由於toUpperCase()通過作用於調用字符串返回一個新字符串,請使用

b = b.toUpperCase();

c和 「測試」 即可。爲什麼?

c還沒有改變它的結果已經被添加到System.out.println()

1

會發生什麼字符串時一個字符串文字被改變?

沒有。那就是字符串文字對象不能改變,因爲當你指出你已經知道它是不可改變的。可以引用它(變量如a,b,c)以引用其他字符串,但該字符串實例不會從​​更改。

不過來解釋你的代碼:

這是bc之間的區別:

b.toUpperCase(); //there's a result from this function you are not using 
System.out.println("b = " + b); 

System.out.println("c = " + c.toUpperCase()); //you're using the result here. 

字符串是不可改變的,但似乎不相關的這個問題

這是相關的,如果你知道它是不可變的,很顯然b不能將更改爲大寫,並且必須因toUpperCase而創建新字符串,因此您必須使用該字符串。然而b,可向引用新的字符串,這不會影響a或其他任何東西仍然引用舊的字符串:

b = b.toUpperCase(); //b now is set to the new upper case string 
System.out.println("b = " + b); 
0

你需要改變這樣的,因爲stringsimmutable

public static void main(String[] args) { 
     // a and b are the same literal 
     String a = "Test"; 
     String b = "Test"; 

     // now I want to change all b's letter 
     // into uppercases, but fail. 
     b= b.toUpperCase(); 
     System.out.println("a = " + a + ", " + "b = " + b); 
     // print: a = Test, b = Test 

     String c = "Test1"; 
     // c=c.toUpperCase(); 
     System.out.println("c = " + c + " , c.toUpperCase() = " 
        + (c=c.toUpperCase())); 
     // print: c = Test1 , c.toUpperCase() = TEST1 

     // change letters of "Test" literal 
     // into uppercase and success 
     System.out.println("Test".toUpperCase()); 
     // print: TEST 
1

我的問題是:1.b不能改成大寫,但c和「測試」可以。爲什麼?

我的回答是,當你打印c.toUpperCase(),可變c在所有沒有改變。

您只需返回另一個字符串這是建基於對c內容爲大寫。

同樣適用於字符串"test"


即使你這樣做,你只指向c到一個新的字符串:

String c = "Test1"; 
c = c.toUpperCase(); 

事情是這樣的:

//String c = "Test1"; 
+-------+ 
|"Test1"| <--- c 
+-------+ 

//c = c.toUpperCase(); 
+-------+ 
|"TEST1"| <--- c 
+-------+ 
+-------+ 
|"Test1"| <--- waiting to be collected by Garbage collector 
+-------+ 
0

,我建議你看看到Java API 。通過使用toUpperCase,你將得到一個新的String對象。如果要用新文本打印出變量,則應將新對象分配給變量。如果是c,則打印出對象的返回「新」內容。變量c將變成小寫字母了。

1

讓我們把你的代碼一行一行地請閱讀我的意見:

// a and b are the same literal 
/* FIRST POINT : 
    Here you assigned two times the same value "Test", 
    BUT IT'S 2 DIFFERENT OBJECTS IN MEMORY */ 
String a = "Test"; 
String b = "Test"; 
// now I want to change all b's letter 
// into uppercases, but fail. 
/* SECOND POINT : 
    Here you just apply a function (toUpperCase()) on "b" object. 
    This function returns a string object but 
    YOU ARE NOT DOING ANYTHING WITH IT 
    i.e. displaying it or reassigning it to another variable! 
    */ 
b.toUpperCase(); 
System.out.println("a = " + a + ", " + "b = " + b); 
// THAT'S WHY IT STILLS PRINT 
// print: a = Test, b = Test 

String c = "Test1"; 
System.out.println("c = " + c + " , c.toUpperCase() = " 
      + c.toUpperCase()); 
/* THIRD POINT : 
    Here you apply a function (toUpperCase()) on "c" object but this time 
    YOU ARE REUSING THE RETURN STRING :) 
    i.e. you are displaying it! 
    */ 
// print: c = Test1 , c.toUpperCase() = TEST1 

// change letters of "Test" literal 
// into uppercase and success 
/* LAST POINT : 
    Here you do the same as you did before on "c" object 
    YOU ARE REUSING THE RETURN STRING AGAIN :) 
    i.e. you are displaying it! 
    */ 
System.out.println("Test".toUpperCase()); 
// print: TEST 

最後但並非最不重要調用toUpperCase()/ toLowerCase()函數的字符串對象將永遠不會重新分配對象的值。這些函數只返回一個字符串。

重新分配字符串值的方式是通常的方式:

String a = "Test"; 
a = a.toUpperCase(); 

請注意,正如許多人說,這將在內存中創建「TEST」的另一個對象,並指定爲「a」和你舊字符串「測試」將成爲垃圾收集器的候選者。

我希望現在更有意義。

乾杯,

相關問題