In Java.

Source: Internet
Author: User

When passing parameters in function calls in Java,

* If the passed Java data type is based on the basic type, all values are passed.

For example, eight basic data types: int, float, double, long, Char, byte, short, and Boolean correspond to integer, float, double, long, String, byte, short, and Boolean respectively.

In addition, the array is also a pass value, which is different from the C/C ++ (verify the byte array)

* If it is a common class object, it is a reference.

Test code:

[Java]
View plaincopyprint?
  1. Package string;
  2. /**
  3. * Test the function parameter value passing in Java.
  4. * Pass the value and reference.
  5. * Function parameters include common types and objects.
  6. * When passing parameters in a function call,
  7. * If the passed Java data type is based on the basic type, all values are passed. for example, eight basic data types: int, float, double, long, Char, byte, short, and Boolean correspond to integer, float, double, long, String, byte, short, and Boolean respectively.
  8. * If it is a common class object, it is a reference.
  9. * @ Author zhankunlin
  10. *
  11. */
  12. Public class testtransfvaluefunction {
  13. /**
  14. * Test the string type.
  15. * String corresponds to Char of the basic type.
  16. */
  17. Private void teststring (){
  18. System. Out. println ("test string ");
  19. String src = new string ("world ");
  20. System. Out. println (SRC );
  21. Procsstring (SRC );
  22. System. Out. println (SRC );
  23. }
  24. /**
  25. * Processing parameter Src in this method
  26. * @ Param SRC
  27. */
  28. Private void procsstring (string SRC ){
  29. SRC + = "hello ";
  30. }
  31. /**
  32. * Test the integer type.
  33. * Integer corresponds to the basic type Int.
  34. */
  35. Private void testinteger (){
  36. System. Out. println ("test 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. * Processing parameter Src in this method
  45. * @ Param SRC
  46. */
  47. Private void procsinteger (integer SRC ){
  48. SRC ++ = 500;
  49. }
  50. /**
  51. * Test the float type.
  52. * Float corresponds to the basic type float.
  53. */
  54. Private void testfloat (){
  55. System. Out. println ("test float ");
  56. Float src = new float (123.45 );
  57. System. Out. println (SRC );
  58. Procsfloat (SRC );
  59. System. Out. println (SRC );
  60. }
  61. /**
  62. * Processing parameter Src in this method
  63. * @ Param SRC
  64. */
  65. Private void procsfloat (float SRC ){
  66. SRC ++ = 500.00;
  67. }
  68. /**
  69. * Test parameters are common class objects.
  70. * A reference is passed.
  71. */
  72. Private void testt (){
  73. System. Out. println ("test common Class Object ");
  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. * Processing parameter Src in this method
  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. }

Package string; </P> <p>/** <br/> * test the function parameter passing value in Java. <br/> * pass the value and reference. <br/> * function parameters include common types and objects. <br/> * when passing parameters in a function call, <br/> * If the passed Java data type is based on the basic type, all values are passed. for example, eight basic data types: int, float, double, long, Char, byte, short, and Boolean correspond to integer, float, double, long, String, byte, short, and Boolean respectively. <br/> * if it is an object of a common class, it is a reference. <br/> * @ author zhankunlin <br/> */<br/> public class testtransfvaluefunction {</P> <p>/** <br/> * test the string type. <br/> * string corresponds to Char of the basic type. <br/> */<br/> private void teststring () {<br/> system. out. println ("test string"); <br/> string src = new string ("world"); <br/> system. out. println (SRC); <br/> procsstring (SRC); <br/> system. out. println (SRC ); <br/>}</P> <p>/** <br/> * processing parameter Src in this method <br/> * @ Param SRC <br/> */ <br/> private void procsstring (string SRC) {<br/> SRC + = "hello"; <br/>}</P> <p>/** <br/> * test the integer type. <br/> * integer corresponds to the basic type Int. <br/> */<br/> private void testinteger () {<br/> system. out. println ("test integer"); <br/> integer src = new INTEGER (500); <br/> // integer src = 500; <br/> system. out. println (SRC); <br/> procsinteger (SRC); <br/> system. out. println (SRC ); <br/>}</P> <p>/** <br/> * processing parameter Src in this method <br/> * @ Param SRC <br/> */ <br/> private void procsinteger (integer SRC) {<br/> SRC + = 500; <br/>}</P> <p>/** <br/> * test the float type. <br/> * float corresponds to the basic float type. <br/> */<br/> private void testfloat () {<br/> system. out. println ("test float"); <br/> float src = new float (123.45); <br/> system. out. println (SRC); <br/> procsfloat (SRC); <br/> system. out. println (SRC ); <br/>}</P> <p>/** <br/> * processing parameter Src in this method <br/> * @ Param SRC <br/> */ <br/> private void procsfloat (float SRC) {<br/> SRC + = 500.00; <br/>}</P> <p>/** <br/> * The test parameter is a common class object. <br/> * The reference is passed. <br/> */<br/> private void testt () {<br/> system. out. println ("test common Class Object"); <br/> T src = new T (); <br/> SRC. num = 100; <br/> SRC. STR = "hello"; <br/> system. out. println (SRC. STR + "," + SRC. num); <br/> procst (SRC); <br/> system. out. println (SRC. STR + "," + SRC. num ); <br/>}</P> <p>/** <br/> * processing parameter Src in this method <br/> * @ Param SRC <br/> */ <br/> private void procst (t src) {<br/> SRC. num = 500; <br/> SRC. STR = "Hello World"; <br/>}</P> <p> Public static void main (string [] argv) {<br/> testtransfvaluefunction test = new testtransfvaluefunction (); <br/> test. teststring (); <br/> test. testinteger (); <br/> test. testfloat (); <br/> test. testt (); <br/>}</P> <p> class t {<br/> Public String STR; <br/> Public integer num; </P> <p> Public void setstr (string Str) {<br/> This. STR = STR; <br/>}</P> <p> Public String getstr () {<br/> return this. STR; <br/>}</P> <p> Public void setnum (integer num) {<br/> This. num = num; <br/>}</P> <p> Public integer getnum () {<br/> return this. num; <br/>}< br/>
Running result:

[Plain]
View plaincopyprint?
  1. Test string
  2. World
  3. World
  4. Test integer
  5. 500
  6. 500
  7. Test float
  8. 123.45
  9. 123.45
  10. Test common class objects
  11. Hello, 100
  12. Hello world, 500

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.