J2ME 技術的學習與實踐者

          導航

          <2008年3月>
          2425262728291
          2345678
          9101112131415
          16171819202122
          23242526272829
          303112345

          統計

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          [導入]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 閱讀(60) 評論(0)  編輯  收藏


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


          網站導航:
           
          主站蜘蛛池模板: 财经| 麻江县| 车险| 舒兰市| 静宁县| 蒙阴县| 高阳县| 驻马店市| 义乌市| 五寨县| 樟树市| 宣汉县| 达州市| 靖西县| 晋宁县| 龙里县| 周至县| 双流县| 明光市| 葫芦岛市| 商河县| 修文县| 南通市| 临邑县| 绍兴市| 疏附县| 沂南县| 石台县| 镇原县| 淮南市| 西昌市| 芒康县| 英超| 呈贡县| 富蕴县| 佳木斯市| 万源市| 柞水县| 家居| 华安县| 五寨县|