Summary of method parameters in Java

Source: Internet
Author: User

JavaProgramThe design language uses value calls. That is to say, the method obtains a copy of all parameter values. In particular, the method cannot be modified to any basic data class passed to it.

There are two types of method parameters:
1. Basic data types (numbers and boolean values ).
2. Object reference.

A method cannot modify a parameter of the basic data type. The object reference as a parameter is different. The object state referenced by the object parameter can be changed because the Method

What we get is the copy of the object reference. The object reference and its copy reference the same object at the same time.

The Java programming language does not use reference calls for objects. In fact, object references are passed through values.

Summarize the usage of method parameters in the Java programming language:
1. A method cannot modify a parameter of the basic data type (that is, an open value or a Boolean value ).
2. A method can change the state of an object parameter.
3. A method object parameter references a new object without changing the value of the object outside the method.

 

 1   Package Com. Action;  2   3   Public   Class  Test {  4   5       Private   Static   Int I = 1 ;  6       Private   Static String S = "string1" ;  7      Private   Static String [] Ss = {"11", "BB", "cc" };  8       Private   Static   Int J = 10 ;  9   10       Public   Int  II;  11       Static  { 12 System. Out. println ("static Block 1" );  13   System. Out. println (I );  14   }  15       Static  {  16   17 System. Out. println ("static Block 2" );  18   }  19   20      /**  21   *  @ Param  ARGs  22        */  23       Public   Static   Void  Main (string [] ARGs ){  24           25 Test T = New  Test ();  26  T. changeint (I );  27 System. Out. println ("Incoming int value:" + I + ":" + J );  28   29   T. changestring (s );  30 System. Out. println ("Incoming string value 1:" + S );  31   32   T. changestring2 (s );  33 System. Out. println ("Incoming string value 2:" + S );  34  35   T. changestrings (SS );  36 System. Out. println ("Incoming array:" + SS [0 ]);  37   38   T. changetest (t );  39 System. Out. println ("Incoming object" + T. I );  40   41 T. II = 111 ;  42   T. changetest1 (t ); 43   System. Out. println (T. II );  44   }  45   46       Public   Void Changeint ( Int  I ){  47 I = 10 ;  48 J ++ ;  49   } 50   51       Public   Void  Changestring (string s ){  52 S = New String ("changestring" );  53   }  54   55       Public   Void  Changestring2 (string s ){  56 S. Replace ("O", "" );  57   }  58   59       Public   Void  Changestrings (string [] S ){  60 S [0] = "numberchange" ;  61   }  62   63       Public   Void Changetest (test T ){  64 Test TT = New  Test ();  65 Tt. I = 100 ;  66 T = TT;  67   68   }  69   70       Public   Void  Changetest1 (test T ){ 71 Test TT = New  Test ();  72 TT. II = 1 ;  73 T = TT;  74   }  75   76 }

 

Result:

Static Block 1
1
Static Block 2
Int value: 1: 11
String Value 1: string1
String Value 2: string1
Into array: numberchange
Round object 100
111

A method can contain multiple static blocks.

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.