一、請運行如下示例代碼StringPool.java,查看其輸出結果。如何解釋這樣的輸出結果?從中你能總結出什麼?java
答:在Java中,內容相同的字串常量(「Hello」)只保存一份以節約內存,因此s0,s1,s2實際上引用的是同一個對算法
象。編譯器在編譯s2一句時,會去掉「+」號,直接把兩個字串鏈接起來得一個字串(「Hello」)。這種優化工做由Java編程
編譯器自動完成。當直接使用new關鍵字建立字符串對象時,雖然值一致(都是「Hello」),但仍然是兩個獨立的對象。數組
請運行如下示例代碼,查看其輸出結果。如何解釋這樣的輸出結果?從中你能總結出什麼?學習
答:給字串變量賦值意味着:兩個變量(s1,s2)如今引用同一個字符串對象「a」!優化
String對象的內容是隻讀的,使用「+」修改s1變量的值,其實是獲得了一個新的字符串對象,加密
其內容爲「ab」,它與原先s1所引用的對象」a」無關,因此,s1==s2返回false;代碼中的「abspa
」字符串是一個常量,它所引用的字符串與s1所引用的「ab」對象無關。String.equals()方法能夠設計
比較兩個字符串的內容。3d
二、請查看String.equals()方法的實現代碼,注意學習其實現方法。
代碼:
public class StringEquals {
public static void main(String[] args) {
String s1=new String("Hello");
String s2=new String("Hello");
System.out.println(s1==s2);
System.out.println(s1.equals(s2));
String s3="Hello";
String s4="Hello";
System.out.println(s3==s4);
System.out.println(s3.equals(s4));
}
}
結果截圖
分析:使用equals()或equalsIgnoreCase()方法比較兩字串內容是否相同,使用==比較兩字串變量是否引用同一
字串對象。而s1和s2分別用new所申請了各自的字串對象,因此係統輸出false。
3、用法:
1)Length():獲取字串長度
2)charAt():獲取指定位置的字符
3)getChars():獲取從指定位置起的子串複製到字符數組中(它有四個參數,在示例中有介紹);
4)replace():子串替換
5)toUpperCase()、 toLowerCase():大小寫轉換
6)trim():去除頭尾空格:
7)toCharArray():將字符串對象轉換爲字符數組
解釋
定義一個String類型的a,a.length()表示字串a的的長度;
用for(int i=0;i<a.length();i++)給a標號Atchar(i)表示a的第i個字符;
charArray = new char[ 5 ];
a.getChars( 0, 5, charArray, 0 );
//四個參數的含義
//1.被拷貝字符在字串中的起始位置
//2.被拷貝的最後一個字符在字串中的下標再加1
//3.目標字符數組
//4.拷貝的字符放在字符數組中的起始下標,
Repalace(a,b)用a替換b;
a.toUpperCase()\A.toLowerCase()分別是小寫轉大寫,大寫轉小寫;
a.Trim()即去掉字串a的頭尾空格;
a.toCharArray()將字串a轉化爲字符數組。
public class Az
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
String a=" This is my original string ,it is very good! ";
String b="";
char a1[]=a.toCharArray();
for (int i=a.length()-1; i>=0; i--){
b += a1[i];
}
String c=a.toUpperCase();
System.out.println(b+"\n"+c);
String r = "your";
a = a.replace("my",r);
System.out.println(a.trim());
}
}
四、String str="abc";
String result=str.trim().toUpperCase().concat("defg");
請閱讀JDK中String類上述方法的源碼,模仿其編程方式,編寫一個MyCounter類,它的方法也支持上述的「級聯」調用特性,其調用示例爲:
MyCounter counter1=new MyCounter(1);
MyCounter counter2=counter1.increase(100).decrease(2).increase(3);
public class MyCounter
{
public static void main(String[] args) {
// TODO Auto-generated method stub
String str="abc";
String result=str.trim().toUpperCase().concat("defg");
System.out.println(result);
}
}
結果:
課後做業一:字串加密
古羅馬皇帝凱撒在打仗時曾經使用過如下方法加密軍事情報:
請編寫一個程序,使用上述算法加密或解密用戶輸入的英文字串要求設計思想、程序流程圖、源代碼、結果截圖。
設計思想:輸入要加密的子串str,定義字符串長度,將字符串轉化爲單個字符,每一個字符+3,
向後移3個,定義output,將新獲得的每一個字符加到output後,最終將其輸出,此字符串即爲加密後的字符串。
程序流程圖:
源代碼:
import java.util.Scanner;
public class Saa
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
System.out.println("請輸入字符");
Scanner in=new Scanner(System.in);
String key=in.nextLine();
int l=key.length();
char a;
System.out.println("加密後爲:");
for(int i=0;i<=key.length()-1;i++)
{
a=key.(i);
if(a<(char) 'x')System.out.println((char)(a+3));
if(a==(char)'x')System.out.println('a');
if(a==(char)'y')System.out.println('b');
if(a==(char)'z')System.out.println('c');
}
}
}
結果截圖: