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)  編輯  收藏


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


          網站導航:
           
          主站蜘蛛池模板: 阿荣旗| 舟曲县| 柏乡县| 襄城县| 克什克腾旗| 准格尔旗| 鸡泽县| 巢湖市| 电白县| 雅江县| 盈江县| 隆子县| 乐业县| 巴青县| 巧家县| 苍山县| 海安县| 武邑县| 轮台县| 祥云县| 景德镇市| 建平县| 绥德县| 广昌县| 阿巴嘎旗| 砚山县| 灌南县| 泸水县| 泗洪县| 姚安县| 淮北市| 香格里拉县| 静安区| 贺州市| 丹凤县| 大理市| 五台县| 虹口区| 汤原县| 延吉市| 怀远县|