歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Java 中的傳值和傳引用

Java 中的傳值和傳引用

日期:2017/3/1 10:40:41   编辑:Linux編程
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. * 測試Java中的函數參數傳值.
  4. * 傳值、傳引用.
  5. * 函數參數是普通類型、對象等.
  6. * 在函數調用傳遞參數時,
  7. * 傳遞的若是基於基本類型的JAVA數據類型, 都是傳值. 如 8 種基本數據類型 int, float, double, long, char, byte, short, boolean 分別對應 Integer, Float, Double, Long, String, Byte, Short, Boolean.
  8. * 傳遞的若是普通類的對象, 則是傳引用.
  9. * @author zhankunlin
  10. *
  11. */
  12. public class TestTransfValueFunction {
  13. /**
  14. * 測試 String 類型.
  15. * String 對應基本類型 char.
  16. */
  17. private void testString() {
  18. System.out.println("測試 String");
  19. String src = new String("world");
  20. System.out.println(src);
  21. procsString(src);
  22. System.out.println(src);
  23. }
  24. /**
  25. * 該方法中處理參數 src
  26. * @param src
  27. */
  28. private void procsString(String src) {
  29. src += "hello";
  30. }
  31. /**
  32. * 測試 Integer 類型.
  33. * Integer 對應基本類型 int.
  34. */
  35. private void testInteger() {
  36. System.out.println("測試 Integer");
  37. Integer src = new Integer(500);
  38. //Integer src = 500;
  39. System.out.println(src);
  40. procsInteger(src);
  41. System.out.println(src);
  42. }
  43. /**
  44. * 該方法中處理參數 src
  45. * @param src
  46. */
  47. private void procsInteger(Integer src) {
  48. src += 500;
  49. }
  50. /**
  51. * 測試 Float 類型.
  52. * Float 對應基本類型 float.
  53. */
  54. private void testFloat() {
  55. System.out.println("測試 Float");
  56. Float src = new Float(123.45);
  57. System.out.println(src);
  58. procsFloat(src);
  59. System.out.println(src);
  60. }
  61. /**
  62. * 該方法中處理參數 src
  63. * @param src
  64. */
  65. private void procsFloat(Float src) {
  66. src += 500.00;
  67. }
  68. /**
  69. * 測試參數是普通類的對象的情況.
  70. * 此時是傳遞的是引用.
  71. */
  72. private void testT() {
  73. System.out.println("測試 普通類對象");
  74. T src = new T();
  75. src.num = 100;
  76. src.str = "hello";
  77. System.out.println(src.str + ", " + src.num);
  78. procsT(src);
  79. System.out.println(src.str + ", " + src.num);
  80. }
  81. /**
  82. * 該方法中處理參數 src
  83. * @param src
  84. */
  85. private void procsT(T src) {
  86. src.num = 500;
  87. src.str = "hello world";
  88. }
  89. public static void main(String []argv) {
  90. TestTransfValueFunction test = new TestTransfValueFunction();
  91. test.testString();
  92. test.testInteger();
  93. test.testFloat();
  94. test.testT();
  95. }
  96. }
  97. class T {
  98. public String str;
  99. public Integer num;
  100. public void setStr(String str) {
  101. this.str = str;
  102. }
  103. public String getStr() {
  104. return this.str;
  105. }
  106. public void setNum(Integer num) {
  107. this.num = num;
  108. }
  109. public Integer getNum() {
  110. return this.num;
  111. }
  112. }
運行結果:
  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
Copyright © Linux教程網 All Rights Reserved