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?
- Package string;
- /**
- * Test the function parameter value passing in Java.
- * Pass the value and reference.
- * Function parameters include common types and objects.
- * When passing parameters in a function call,
- * 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.
- * If it is a common class object, it is a reference.
- * @ Author zhankunlin
- *
- */
- Public class testtransfvaluefunction {
- /**
- * Test the string type.
- * String corresponds to Char of the basic type.
- */
- Private void teststring (){
- System. Out. println ("test string ");
- String src = new string ("world ");
- System. Out. println (SRC );
- Procsstring (SRC );
- System. Out. println (SRC );
- }
- /**
- * Processing parameter Src in this method
- * @ Param SRC
- */
- Private void procsstring (string SRC ){
- SRC + = "hello ";
- }
- /**
- * Test the integer type.
- * Integer corresponds to the basic type Int.
- */
- Private void testinteger (){
- System. Out. println ("test integer ");
- Integer src = new INTEGER (500 );
- /// Integer src = 500;
- System. Out. println (SRC );
- Procsinteger (SRC );
- System. Out. println (SRC );
- }
- /**
- * Processing parameter Src in this method
- * @ Param SRC
- */
- Private void procsinteger (integer SRC ){
- SRC ++ = 500;
- }
- /**
- * Test the float type.
- * Float corresponds to the basic type float.
- */
- Private void testfloat (){
- System. Out. println ("test float ");
- Float src = new float (123.45 );
- System. Out. println (SRC );
- Procsfloat (SRC );
- System. Out. println (SRC );
- }
- /**
- * Processing parameter Src in this method
- * @ Param SRC
- */
- Private void procsfloat (float SRC ){
- SRC ++ = 500.00;
- }
- /**
- * Test parameters are common class objects.
- * A reference is passed.
- */
- Private void testt (){
- System. Out. println ("test common Class Object ");
- T src = new T ();
- Src-num = 100;
- SRC. Str = "hello ";
- System. Out. println (SRC. Str + "," + SRC. Num );
- Procst (SRC );
- System. Out. println (SRC. Str + "," + SRC. Num );
- }
- /**
- * Processing parameter Src in this method
- * @ Param SRC
- */
- Private void procst (t src ){
- Src-num = 500;
- SRC. Str = "Hello World ";
- }
- Public static void main (string [] argv ){
- Testtransfvaluefunction test = new testtransfvaluefunction ();
- Test. teststring ();
- Test. testinteger ();
- Test. testfloat ();
- Test. testt ();
- }
- }
- Class t {
- Public String STR;
- Public integer num;
- Public void setstr (string Str ){
- This. Str = STR;
- }
- Public String getstr (){
- Return this. STR;
- }
- Public void setnum (integer num ){
- This. num = num;
- }
- Public integer getnum (){
- Return this. Num;
- }
- }
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?
- Test string
- World
- World
- Test integer
- 500
- 500
- Test float
- 123.45
- 123.45
- Test common class objects
- Hello, 100
- Hello world, 500