String概述
凡是帶”“的都是String的實例對象。
String s1 = "abc";
s1是一個類類型變量,“abc”是一個對象。
字符串的最大特點就是,一旦被初始化就不可以被改變。
那麼:
String s1 = "abc";
s1 = "kk";
這時候打印s1的值是kk;
這是爲什麼呢?不是說字符串一初始化就不能改變的嗎?
對,不能改變,abc沒有變,被賦值爲kk之後abc還在內存中,變了的是s1,s1的引用由abc變爲了kk。
定義字符串有兩種方法:
String s1 = new String("abc");
String s2 = "abc";
System.out.println(s1==s2);//false
System.out.println(s1.equals(s2));//true
實驗結果爲false 和true 這是爲什麼呢?
因爲“==”符號判斷的是hash值,也就是說比較的是數據在內存中的存放地址。
在Object類中定義的equals方法裏邊調用的也是“==”(比較地址值)
但是String類複寫了Object的equals方法,所以String類中調用的equals方法比較的是字符串的值。所以出現結果爲真的現象。
那麼在以上的s1和s2有什麼區別呢?
s1在內存中有兩個對象。(abc,s1)
s2在內存中有一個對象。(abc)
(字符串在內存中有一個常量池,裏邊是一個數組,當要新建一個abc字符串的時候,在數組中存入[a][b][c],如果要新建一個def字符串的時候,在數組中存入[d][e][f],如果這時候要創建一個新的字符串abf,就不會再數組中重新存儲數據,而是將之前存入的[a][b][f]重新組成一個字符串作爲引用。)
String s = “abc”;
這句話會在常量池中尋找有沒有abc這樣的字符串,有的話,直接引用過來,沒有的話在常量池中新建一個abc,再建立引用。
String s = new String (“abc”);
這句話指明瞭,我要新(new)的,我不要舊的,所以就不會在常量池中尋找,直接新建了一個字符串。
實例:
public class StringMethodDemo {
public static void main(String[] args)
{
String s1 = "abc";
String s2 = new String("abc");
String s3 = "abc";
System.out.println(s1==s2);
System.out.println(s1==s3);
}
}
運行結果:
String類中的常見功能
String類是用於描述字符串事物的,那麼他就提供了多個方法對字符串進行操作
常見的操作有:
1,獲取。
- 字符串中的包含的字符數,也就是字符串的長度。
- int length();獲取長度
- 根據位置獲取位置上的某個字符:
- char charAt(int index)
- 根據字符獲取該字符在字符串中的位置。
- int indexOf(int ch);返回的是ch在字符串中第一次出現的位置。
- int indexOf(int ch,int fromIndex);從fromIndex指定位置開始,獲取ch在字符串中出現的位置。
- int indexOf(String str );返回的是str在字符串中第一次出現的位置。
- int indexOf(String str,int fromIndex);從fromIndex指定位置開始,獲取str在字符串中出現的位置。
public class StringMethodDemo {
public static void sop(Object obj)
{
System.out.println(obj);
}
public static void method_get()
{
String s = "abcdefabc";
//獲取長度:
sop(s.length());
//獲取指定角標下的字符
sop(s.charAt(4));
//獲取指定元素第一次出現的位置
sop(s.indexOf('a'));
//獲取指定元素從指定位置開始在字符串中第一次出現的位置。
sop(s.indexOf('a',4));
//獲取指定字符串在字符串中第一次出現的位置
sop(s.indexOf("a"));
//獲取指定字符串從指定位置開始在字符串中第一次出現的位置。
sop(s.indexOf("a",4));
//從後往前找指定字符在字符串中第一次出現的位置
sop(s.lastIndexOf("a"));
}
public static void main(String[] args)
{
method_get();
}
}
運行結果是:
2,判斷:
- 字符串中是否包含某一個子串
- boolean contains(str)
- 特殊之處:indexOf(str)可以索引str第一次出現的位置,如果返回-1,表示該str不在字符串中存在。所以,也可以用於對指定判斷是否包含。
if(str.indexOf("aa")!=-1)
- 而且該方法既可以判斷,又可以獲取出現的位置。
- 字符串中是否有內容
- boolean isEmpty();原理就是判斷長度是否爲0
- 字符串是否是以指定內容開頭
- boolean startsWith(str);
- 字符串是否是以指定內容結尾
- boolean endsWith(str);
- 判斷字符串內容是否相同,複寫了Object類中的equals方法。
- boolean equals(str);
- 判斷內容是否相同,並忽略大小寫。
- boolean equalsIgnoreCase();
public class StringMethodDemo {
public static void sop(Object obj)
{
System.out.println(obj);
}
public static void method_is()
{
String str = "ArrayDemo.java";
//判斷是否是以“Array”開頭的
sop(str.startsWith("Array"));
//判斷中間是否包含“Demo”
sop(str.contains("Demo"));
//判斷是否是以“.java”結尾的
sop(str.endsWith(".java"));
//判斷字符串是否相同
sop(str.equals("arraydemo.java"));
//忽略大小寫判斷字符串是否相同
sop(str.equalsIgnoreCase("arraydemo.java"));
}
public static void main(String[] args)
{
method_is();
}
}
運行結果爲:
3,轉換;
- 將字符數組轉換成字符串
- 構造函數:
- String(char[]):將字符數組轉換成字符串
- String(char[],offset,count);將字符數組的一部分轉換成字符串,參數依次爲:字符數組,起始位置的角標,一共要轉換成字符串的個數。
- 靜態方法:
- static String copyValueOf(char[]);
- static String copyValueOf(char[] data,int offset,int count);
- static String valueOf(char[]);
- 構造函數:
- 將字符串轉成字符數組
- char[] toCharArray();
- 將字節數組轉換成爲字符串
- String(byte[])
- String(byte[],offset,count);將字節數組中的一部分轉成字符串
- 將字符串轉成字節數組:
- byte[] getBytes();
- 將基本數據類型轉換成字符串
- static String valueOf(int);
- static String valueOf(double);
- //3+”“和String.valueOf(3);沒什麼區別
- 特殊:字符串和字節數組在轉換過程中,是可以指定編碼表的。
示例:
public class StringMethodDemo {
public static void sop(Object obj)
{
System.out.println(obj);
}
public static void method_trans()
{
//字符數組轉換成字符串
char[] chs ={'a','b','c','d','e','f','g'};
String str = new String(chs);
String str1 = new String(chs,1,4);
sop("str="+str);
sop("str1="+str1);
//將字符串轉成字符數組:
char[] arr = str.toCharArray();
for (int x =0;x<arr.length ;x++ )
{
sop("char["+x+"]="+arr[x]);
}
//將字符串轉換成字節數組
byte[] bts = str.getBytes();
for (int x = 0;x<bts.length ;x++ )
{
sop("bts["+x+"]="+bts[x]);
}
}
public static void main(String[] args)
{
method_trans();
}
}
運行結果:
4,替換
- String replace(oldchar,newchar);將字符串中的勻速進行替換
示例:
public class StringMethodDemo {
public static void sop(Object obj)
{
System.out.println(obj);
}
//替換方法 String replace(oldchar,newchar);
public static void method_replace()
{
String s1 = "hello java";
String s2 = s1.replace("a","n");//可以單個字符進行調換
String s3 = s1.replace("java","world");//也可以將指定的字符串進行調換
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
}
public static void main(String[] args)
{
method_replace();
}
}
運行結果:
需要注意的是,替換之後的字符串在內存中還存在,因爲字符串只要一被創建就不會被改變。
5,切割
- String [] split(regex);
將給定字符串用指定的符號切割出來,顯然返回來的是一個字符串數組
示例:
public class StringMethodDemo {
public static void sop(Object obj)
{
System.out.println(obj);
}
//切割方法 String[] split(regex);
public static void method_split()
{
String s1 = "zhangsan,lisi,wangwu";
String[] arr=s1.split(","); //裏邊指定切割的參照符號,用“”括起來
for (int x =0;x<arr.length ;x++ )//用循環遍歷打印出切割出來的字符串
{
System.out.println("String["+x+"]=="+arr[x]);
}
}
public static void main(String[] args)
{
method_split();
}
}
運行結果:
6,子串,獲取字符串中的一部分
- String substring(begin);從指定位置開始到結尾
- String substring(begin,end);包含頭不包含尾
public class StringMethodDemo {
public static void sop(Object obj)
{
System.out.println(obj);
}
//獲取子串,substring(start,end)//substring(start)
public static void method_substring()
{
String s1 = "nihaowojiaoheshuaishuai";
String s2 = s1.substring(5);
//獲取從5角標開始到字符串結束中間的字符串
String s3 = s1.substring(0,s1.length());
//獲取從0角標開始,到字符串結束中間的字符串。(包含頭不包含尾)
sop(s1);
sop(s2);
sop(s3);
}
public static void main(String[] args)
{
method_substring();
}
}
運行結果:
7,轉換,去除空格,比較
- 將字符串轉成大寫或者小寫
- String toUpperCase();
- String toLowerCase();
- 將字符串兩端的多個空格去除
- String trim();
- 對兩個字符串進行自然順序的比較
- int compareTo(String);
public class StringMethodDemo {
public static void sop(Object obj)
{
System.out.println(obj);
}
//轉換,去空格,比較
public static void method()
{
//轉換。轉換字符串的大小寫
String s1 = " Hello Java ";
String s2 = s1.toUpperCase();//將所有字符轉換爲大寫形式
String s3 = s1.toLowerCase();//將所有字符抓換成小寫形式
sop(s1);
sop(s2);
sop(s3);
//去除字符串兩端的空格:
String s4 = s1.trim();
sop(s4);
//比較(按照字典順序比較兩個字符串的大小,實際上就是按照ASCII值比較的)
String s5 = "abc";
String s6 = "acd";
int x = s5.compareTo(s6);
//如果s5比s6小,則會返回一個<0的值,如果s5比s6大,則會返回一個>0的數,
//至於返回的是多少:在以上字符串中,先比較a相同,再比較第二位,s5第二位是b,s6第二位是c,所以b-c=-1(比較ASCII碼值)
sop(x);
}
public static void main(String[] args)
{
method();
}
}
運行結果:
練習:模擬trim方法的代碼
//需求:寫一個模擬trim方法的代碼。將字符串兩端的空格去除
/*
思路,取字符串開始的角標爲指針,判斷是不是空格,如果是空格,指針++
從後往前數也一樣的道理
然後把指針中間的字符串取出來就行了
需要注意的是,兩個指針一個往後走,一個往前走,不能跑的錯開了。
*/
public class MyTrim {
public static void main(String[] args)
{
String s = " ni hao ";
String s1 = mytrim(s);
sop(s);
sop(s1);
}
public static void sop(Object obj)
{
System.out.println(obj);
}
public static String mytrim(String s)//定義自己的去空格方法
{
int pos_start = 0,pos_end = s.length()-1;//定義連個指針 ,分別在開頭和結尾處
while (s.charAt(pos_start)==' ' && pos_start<pos_end)//判斷指針所在的位置的字符是不是空格,是則指針再往前走
{
pos_start++;
}
while (s.charAt(pos_end)==' ' && pos_start<pos_end)
{
pos_end--;
}
return s.substring(pos_start,pos_end+1);//返回兩個指針中間的字符串,記住獲取子串包含頭不包含尾。
}
}
運行結果:
練習二:定義方法將字符串進行反轉。
//需求:將一個字符串進行反轉
/*
思路:
先把字符串轉換成字符數組,
將字符數組進行反轉
再把字符數組變成字符串
*/
public class ReverseStringDemo {
public static void sop(Object obj)//爲了方便定義的打印方法
{
System.out.println(obj);
}
public static String reverseString(String s)//反轉字符串的方法
{
return reverseString(s,0,s.length());//返回反轉後的整個字符串。(包含頭不包含尾)
}
private static void reverse(char[] chs,int x,int y)//反轉數組的方法
{
//定義兩個指針,一前一後,循環一次,自增(減)一次,
for (int start = x,end = y-1;start<end ;start++,end-- )
{
swap(chs,start,end);//每循環一次,將前後兩個指針的數據調換位置
}
}
private static void swap(char[] arr,int x,int y)//調換位置的方法
{
char temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
//爲了提高方法的擴展性,這裏給定不同的參數進行函數的重載。
//將字符串中的指定位置的字符串進行反轉。
public static String reverseString(String s,int start,int end)
{
char[] chs = s.toCharArray();//先將字符串變成字符數組
reverse(chs,start,end);//反轉數組
return new String(chs);//將字符數組變成字符串並返回
}
public static void main(String[] args)
{
String s = " ab cd ";
//給定一個字符串
sop("("+s+")");//打印原字符串
sop("("+reverseString(s)+")");//將整個字符串反轉
sop("("+reverseString(s,4,9)+")");//將字符串中的指定序列的字符串反轉
}
}
運行結果爲:
練習三:獲取一段字符串在另一段字符串中出現的次數
//獲取一個字符串在另一個字符串中出現的次數
/*
思路:先要有一個計數器
需要用到indexOf,所以就需要有每次開始數的角標
然後每見到一次key的值,計數器加1
*/
public class KeyStringCount {
public static void sop(Object obj)
{
System.out.println(obj);
}
public static int function(String str,String key)//給兩個參數,一個是字符串,一個是要尋找的子串
{
int count = 0;//計數器
int index = 0;//開始查找的位置(角標)
while ((index = str.indexOf(key,index))!=-1)//當找到第一個匹配的時候,將找到的角標值加key的長度,作爲下次開始找的角標值
{
index = index+key.length();
count++;
}
return count;
}
public static void main(String[] args)
{
String s = "abqqcdqqefqqghqqijqqk";//5個qq
sop("count===="+function(s,"qq"));
}
}
運行結果:
練習四:獲取兩個字符串中的最大相同子串
//需求:獲取兩個字符串中的最大相同子串
/*
思路:按短的那個字符串先找,找到了,就中了
找不到,就字符串長度減1,找一次,然後兩邊角標自增,再找一次,
找不到,就字符串長度減2,角標自增,一共找三次。
如果找到了,就把找到的那個切出來
*/
public class GetMaxSubString {
public static void sop(Object obj)
{
System.out.println(obj);
}
public static String getMaxSubString(String s1,String s2)
{
//因爲不確定傳進來的字符串參數哪個更長,所以先將長短區分出來,分別引用給Max和min
String max,min;
max = (s1.length()>s2.length())?s1:s2;//三目運算符,s1的長度大於s2嗎,如果大於,將s1的值引用給max
min = (max == s1)?s2:s1;
//經過分析,判斷最大相同子串要把短字符串每次去一位然後角標自增的方式,
/*
長度 判斷次數
第一次判斷:0-min.length()-0 1
第二次判斷:0-min.length()-1 2
第三次判斷:0-min.length()-2 3
第四次判斷:0-min.length()-3 4
*/
for (int x = 0;x<min.length() ;x++ )//第一個for控制外層循環(判斷)次數的
{
for (int y = 0,z = min.length()-x;z!=min.length()+1 ;y++,z++ )//第二個for用來控制內層循環判斷次數的
{
String temp = min.substring(y,z);//每次循環都把判斷過的子串取出來
if (max.contains(temp))//判斷長字符串中是不是有該子串,
return temp;//有的話就返回該字符串。
}
}
return "";
}
public static void main(String[] args)
{
String s1 = "jfdlagehellorkajfalfd";
String s2 = "djkdhellodof";
sop(getMaxSubString(s2,s1));
}
}
運行結果:
根據老師寫的代碼,下面是自己寫的代碼,同樣可以實現這樣的功能:
//需求:獲取兩個字符串中的最大相同子串
/*
寫完後發現老師的代碼在for循環的那一段不太好理解,
然後根據自己的想法,寫了一段,可以運行
代碼不分家,只要能實現功能就好
*/
public class GetMaxSubString2 {
public static void sop(Object obj)
{
System.out.println(obj);
}
public static String getMaxSubString(String s1,String s2)
{
String max,min;
max = (s1.length()>s2.length())?s1:s2;
min = (max == s1)?s2:s1;
//經過分析,判斷最大相同子串要把短字符串每次去一位然後角標自增的方式,
/*
長度 判斷次數
第一次判斷:0-min.length()-0 1
第二次判斷:0-min.length()-1 2
第三次判斷:0-min.length()-2 3
第四次判斷:0-min.length()-3 4
*/
for (int x = min.length();x>-1 ;x-- )//第一個for控制字符串長度的
{
for (int y = 0,z = x;z!=min.length()+1 ;y++,z++ )//第二個for用來控制內層循環判斷次數的
{
String temp = min.substring(y,z);
if (max.contains(temp))
return temp;
}
}
return "";
}
public static void main(String[] args)
{
String s1 = "jfdlagehellorkajfalfd";
String s2 = "djkdhellodof";
sop(getMaxSubString(s2,s1));
}
}
運行結果和上圖相同。
StringBuffer
StringBuffer是字符串緩衝區,可以用於對字符串進行修改的
是一個容器。
特點:
1 長度是可變化的。
2 可以直接操作多個數據類型
3 最終會通過toString方法變成字符串。
C : create U : update R : read D: delete
面盆理論:StringBuffer就是一個面盆,在裏邊加各種東西,完了返回來的還是這個面盆,同一個面盆,沒有任何的改變,
public class StringBufferDemo {
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer();
StringBuffer sb1 = new StringBuffer();//新new了一個,不一樣
StringBuffer sb2 = sb.append(345);//經過操作返回來的一個,還是原來的面盆
sop(sb==sb1);//false
sop(sb==sb2);//true
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
1,存儲。
StringBuffer append();將指定數據作爲參數添加到已有數據的結尾處。
StringBuffer insert(Index,數據)可以將數據插入到指定位置。
class StringBufferDemo
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer();//定義一個字符串容器
sb.append("abc").append(true).append(34);//在裏邊數據的末尾處先後添加各種類型的數據
sop(sb.toString());//abctrue34
sb.insert(1,"qq");//在1角標位置處添加qq字符串
sop(sb.toString());//aqqbctrue34
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
2,刪除
StringBuffer delete(start,end)刪除緩衝區中的數據,包含頭不包含尾
StringBuffer deleteCharAt()刪除指定位置的字符。
//字符串緩衝區的刪除方法演示
public class StringBufferDelete {
public static void sop(Object obj)
{
System.out.println(obj);
}
public static void stringBufferDelete()
{
StringBuffer sb = new StringBuffer("abcdefghijk");
sop(sb.toString());
sb.delete(2,6);//將指定範圍內的字符串刪除
sop(sb.toString());//abghijk
sb.deleteCharAt(0);//刪除指定位置的字符
sop(sb.toString());//bghijk
sb.delete(0,sb.length());//清空緩衝區
sop(sb.toString()+";");
}
public static void main(String[] args)
{
stringBufferDelete();
}
}
運行結果:
3,獲取。
char charAt(Index)獲取一個
int indexOf(String str);獲取指定字符串出現的角標
int laseIndexOf(String str);倒序查找指定字符串出現的角標
int length() 獲取容器裏邊字符串的長度
String subString(int start,int end)切出來的字符串
這裏和Sting類中的方法一樣的,,就不演示了。
4,修改
StringBuffer replace(start,end,string);修改區間內的一整片
void setCharAt(int Index,char ch);將指定位置的元素換成指定的字符。
//演示字符串緩衝區中的修改元素的方法
public class StringBufferReplace {
public static void sop(Object obj)
{
System.out.println(obj);
}
public static void stringBufferReplace()
{
StringBuffer sb = new StringBuffer("hello world");
sop(sb.toString());
sb.replace(7,12,"java");//將sb中的元素,從角標7到角標12中的元素,替換成java
sop(sb.toString());
sb.setCharAt(7,'J');//將hello java中的第7的元素換成J
sop(sb.toString());
}
public static void main(String[] args)
{
stringBufferReplace();
}
}
運行結果:
5,反轉
StringBuffer reverse()反轉容器中的字符串
示例:
//演示字符串緩衝區的反轉元素的方法
public class StringBufferReverse {
public static void sop(Object obj)
{
System.out.println(obj);
}
public static void stringBufferReverse()
{
StringBuffer sb = new StringBuffer("abcdefghijk");
sop(sb.toString());//打印原字符串緩衝區中的字符串
sb = sb.reverse();//將容器中的字符串反轉
sop(sb.toString());//打印反轉後的字符串
}
public static void main(String[] args)
{
stringBufferReverse();
}
}
運行結果:
6, 將字符串緩衝區中的元素轉移到指定數組中
void getChars(int srcBegin,int srcEnd,char[] dst,int dstBegin);
StringBuffer sb = new StringBuffer();
sb.append("abcdef");
char[] chs = new char[4];
sb.getChars(1,4,chs,3);//將sb中從角標1到角標4的元素轉給chs數組,在數組中從1角標開始依次插入
for(int x = 0;x<chs.length;x++)
{
System.out.println("chs["+x+"]="+chs[x]+";");
}
運行結果:
在jdk1.5之後出現了StringBuilder。
- StringBuffer是線程同步的。
- StringBuilder是線程不同步的。
- 以後開發建議使用StingBuilder
升級三個因素
- 提高效率
- 簡化書寫
- 提高安全性
基本數據類型對象包裝類
根據面向對象的原則,Java中將所有的東西都進行了包裝,包括基本數據類型
那麼將基本數據類型封裝成對象有什麼用呢?
看下邊的一個例子:
//基本數據類型的對象包裝類
public class IntegerDemo {
public static void sop(Object obj)
{
System.out.println(obj);
}
public static void main(String[] args)
{
//假如我們想知道int類型的數值的最大值
sop(Integer.MAX_VALUE);
//相同的道理,int類型的數值的最小值
sop(Integer.MIN_VALUE);
}
}
實驗結果:
這並不是基本數據類型對象的最常見的作用,
最常見的作用就是:基本數據類型和字符串類型之間做轉換,
基本數據類型轉換成字符串:
- 基本數據類型+”“;
- 基本數據類型.toString(基本數據類型值);
- 如:Integer.toString(34);//將34整數變成“34”。
字符串轉基本數據類型的操作:
//基本數據類型的對象包裝類
public class IntegerDemo {
public static void sop(Object obj)
{
System.out.println(obj);
}
public static void main(String[] args)
{
//將一個字符串轉成整數
int num = Integer.parseInt("123");
sop("num = "+(num+4));//127
}
}
int a = Integer.parseInt(“123”);字符串中必須全是int類型的值,否則就是數字格式異常,這個事靜態調用方式。
Integer i = new Integer(“123”);
int num = i.intValue();//這個是對象調用方式
其他類型道理也一樣的
double b = Double.parseDouble(“12.23”);
boolean b = Boolean.parseBoolean(“true”);
十進制轉成其他進制數
Integer.toBinaryString(6);//110
Integer.toOctalString(100);//144
Integer.toHexString(60);//3c
其他進制轉換成十進制
parseInt("110",2);//6
parseInt("144",8);//100
parseInt("3c",16);//60
jdk1.5以後的新特性:
看一串代碼:
//jdk1.5以後的新特性
public class IntegerDemo {
public static void sop(Object obj)
{
System.out.println(obj);
}
public static void main(String[] args)
{
Integer a = new Integer("123");
Integer b = new Integer(123);
sop("a==b:"+(a==b));
sop("a.equals(b):"+a.equals(b));
}
}
運行結果爲:
出現這樣的結果是爲什麼呢?
a==b爲假,正常,因爲都是新new的對象,地址值肯定不一樣
a.equals(b)是因爲他和字符串一樣比較的是值。所以結果爲真
再有一個示例:
//jdk1.5以後的新特性
public class IntegerDemo {
public static void sop(Object obj)
{
System.out.println(obj);
}
public static void main(String[] args)
{
Integer i = 4;//怎麼可以直接將數字賦值給對象?在這裏4是一個對象,因爲Integer是一個類,i就是一個對象了,i指向的一定是對象。
//在這裏,4會進行自動裝箱,也就是會自動將4封裝成對象,賦值給i(new Integer(4))
//在這裏,Integer還可以賦值爲null(新特徵),編譯不會出錯,就是運算的時候會出現空指針異常
i = i+2; //新特性:i是一個對象,2是一個數字,兩個不同類型的東西怎麼能相加?
//可以,在這裏進行運算的時候,i會進行自動的拆箱,i.intValue();轉換成int類型的值再進行運算
//計算完結果之後再將結果自動裝箱,封裝成對象,賦值給i
}
}
新特徵3:
//jdk1.5以後的新特性
public class IntegerDemo {
public static void sop(Object obj)
{
System.out.println(obj);
}
public static void main(String[] args)
{
Integer a = 128;
Integer b = 128;
sop("a==b:"+(a==b));
Integer c = 127;
Integer d = 127;
sop("c==d:"+(c==d));
}
}
運行結果:
爲什麼會出現這樣的結果呢?
定義abcd的過程可以看成是:Integer a = new Integer(128);
abcd都是不同的對象,但是,區別在於賦給的數值不一樣,
新特徵3:對於byte範圍內的值,如果該數值已經存在,則不會開闢新的空間,所以說c和d指向了同一個對象。(byte範圍-128~127)