Java 中的传值和传应用 .

 Java中在函数调用传递参数时,

 *   传递的若是基于基本类型的JAVA数据类型, 都是传值. 

             如 8 种基本数据类型 int, float, double, long, char, byte, short, boolean 分别对应 Integer, Float, Double, Long, String, Byte, Short, Boolean.

             此外,数组也是传值,和C/C++中不一样(验证了 byte 数组)

 *   传递的若是普通类的对象, 则是传引用.

测试代码:

  1. package string;  
  2.   
  3. /** 
  4.  * 测试Java中的函数参数传值. 
  5.  * 传值、传引用. 
  6.  * 函数参数是普通类型、对象等. 
  7.  * 在函数调用传递参数时, 
  8.  *    传递的若是基于基本类型的JAVA数据类型, 都是传值. 如 8 种基本数据类型 int, float, double, long, char, byte, short, boolean 分别对应 Integer, Float, Double, Long, String, Byte, Short, Boolean. 
  9.  *    传递的若是普通类的对象, 则是传引用. 
  10.  * @author zhankunlin 
  11.  * 
  12.  */  
  13. public class TestTransfValueFunction {  
  14.   
  15.     /** 
  16.      * 测试 String 类型. 
  17.      * String 对应基本类型 char. 
  18.      */  
  19.     private void testString() {  
  20.         System.out.println("测试 String");  
  21.         String src = new String("world");    
  22.         System.out.println(src);  
  23.         procsString(src);  
  24.         System.out.println(src);  
  25.     }  
  26.       
  27.     /** 
  28.      * 该方法中处理参数 src 
  29.      * @param src 
  30.      */  
  31.     private void procsString(String src) {  
  32.         src += "hello";  
  33.     }  
  34.       
  35.     /** 
  36.      * 测试 Integer 类型. 
  37.      * Integer 对应基本类型 int. 
  38.      */  
  39.     private void testInteger() {  
  40.         System.out.println("测试 Integer");  
  41.         Integer src = new Integer(500);  
  42.         //Integer src = 500;     
  43.         System.out.println(src);  
  44.         procsInteger(src);  
  45.         System.out.println(src);  
  46.     }  
  47.       
  48.     /** 
  49.      * 该方法中处理参数 src 
  50.      * @param src 
  51.      */  
  52.     private void procsInteger(Integer src) {  
  53.         src += 500;  
  54.     }  
  55.       
  56.     /** 
  57.      * 测试 Float 类型. 
  58.      * Float 对应基本类型 float. 
  59.      */  
  60.     private void testFloat() {  
  61.         System.out.println("测试 Float");  
  62.         Float src = new Float(123.45);    
  63.         System.out.println(src);  
  64.         procsFloat(src);  
  65.         System.out.println(src);  
  66.     }  
  67.       
  68.     /** 
  69.      * 该方法中处理参数 src 
  70.      * @param src 
  71.      */  
  72.     private void procsFloat(Float src) {  
  73.         src += 500.00;  
  74.     }  
  75.       
  76.     /** 
  77.      * 测试参数是普通类的对象的情况. 
  78.      * 此时是传递的是引用. 
  79.      */  
  80.     private void testT() {  
  81.         System.out.println("测试 普通类对象");  
  82.         T src = new T();    
  83.         src.num = 100;  
  84.         src.str = "hello";  
  85.         System.out.println(src.str + ", " + src.num);  
  86.         procsT(src);  
  87.         System.out.println(src.str + ", " + src.num);  
  88.     }  
  89.       
  90.     /** 
  91.      * 该方法中处理参数 src 
  92.      * @param src 
  93.      */  
  94.     private void procsT(T src) {  
  95.         src.num = 500;  
  96.         src.str = "hello world";  
  97.     }  
  98.       
  99.     public static void main(String []argv) {  
  100.         TestTransfValueFunction test = new TestTransfValueFunction();  
  101.         test.testString();  
  102.         test.testInteger();  
  103.         test.testFloat();  
  104.         test.testT();  
  105.     }  
  106. }  
  107.   
  108. class T {  
  109.     public String str;  
  110.     public Integer num;  
  111.       
  112.     public void setStr(String str) {  
  113.         this.str = str;  
  114.     }  
  115.       
  116.     public String getStr() {  
  117.         return this.str;  
  118.     }  
  119.       
  120.     public void setNum(Integer num) {  
  121.         this.num = num;  
  122.     }  
  123.       
  124.     public Integer getNum() {  
  125.         return this.num;  
  126.     }  
  127. }  

运行结果:

  1. 测试 String  
  2. world  
  3. world  
  4. 测试 Integer  
  5. 500  
  6. 500  
  7. 测试 Float  
  8. 123.45  
  9. 123.45  
  10. 测试 普通类对象  
  11. hello, 100  
  12. hello world, 500  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值