J2ME 技術的學習與實踐者

          [導入]Java 初學者應對以下代碼問個為什么


          網站: JavaEye  作者: iwinyeah  鏈接:http://iwinyeah.javaeye.com/blog/169280  發表時間: 2008年03月08日

          聲明:本文系JavaEye網站發布的原創博客文章,未經作者書面許可,嚴禁任何網站轉載本文,否則必將追究法律責任!

          public void main(){
          	Integer nullInt = null;
          	tryChangeInteger(nullInt);
          	if(nullInt == null){
          		System.out.println("\nThe Object nullInt not be changed.");
          	}else{
          		System.out.println("\nThe Object nullInt now is " + nullInt);
          	}
          }	
          
          private void tryChangeInteger(Integer theInt){
          	theInt = new Integer(100); 
          }
          
          // 控制臺應打印出什么呢?(The Object nullInt not be changed.)
          //
          // 關鍵要理解好Java的參數傳遞是傳值而不是傳引用,因而在tryChangeInteger方法
          // 里得到的theInt不是main方法里的nullInt,而是nullInt的副本,nullInt值并沒有被改變。
          
          // 再請看以下代碼
          
          public void main() {
          	char[] initChars = new char[10];
          	initChars[0] = 'a';
          	tryChangeCharArray(initChars);
          	if(initChars[0] == 'a'){
          		System.out.println("\nThe Object initChars[0] not be changed.");
          	}else{
          		System.out.println("\nThe Object initChars[0] now is " + initChars[0]);
          	}
          }		
          
          private void tryChangeCharArray(char[] theChars){
          	if(theChars != null && theChars.length > 0){
          		theChars[0] = 'b';
          	}
          }
          	
          // 控制臺應打印出什么呢?(The Object initChars[0] now is b")
          // Why?
          // 弄明白了這個,JAVA引用基本就明白了。
          

          本文的討論也很精彩,瀏覽討論>>


          JavaEye推薦




          文章來源:http://iwinyeah.javaeye.com/blog/169280

          posted on 2008-03-08 07:16 iwinyeah 閱讀(59) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 扶风县| 本溪市| 洪洞县| 申扎县| 康马县| 永康市| 姚安县| 永嘉县| 紫金县| 沙田区| 通榆县| 田东县| 锦屏县| 湄潭县| 宜州市| 宾阳县| 龙门县| 郎溪县| 阿图什市| 察雅县| 湘潭市| 新郑市| 陵水| 曲水县| 远安县| 青龙| 双牌县| 铁力市| 道孚县| 建平县| 喜德县| 准格尔旗| 息烽县| 隆尧县| 马鞍山市| 子洲县| 黄大仙区| 郁南县| 英吉沙县| 洪洞县| 霸州市|