Java Method Pass Parameter

Source: Internet
Author: User

This is my younger brother's first blog, write bad also please crossing forgive Ha!

Java has two data types: the base data type (Int,short,long,byte,float,double,boolean,char), the reference data type (arrays outside the base data type, String, objects, and so on).

There are two ways of passing parameters in a method: value passing and reference passing

Let's look at one of the following examples.

 Public classStu {String name;intAge ; PublicString GetName () {returnname;} Public voidsetName (String name) { This. Name =name;} Public intGetage () {returnAge ;} Public voidSetage (intAge ) { This. Age =Age ;}} Public classTest {protected Static voidUpdatestu (Stu Stu) {stu.setage (5); Stu.setname ("Li");//stu=new Stu ();//Stu.setage (5);//stu.setname ("Li");//Replace the annotated code with the above comment, the Stu is the same as before and after the modification, that is 3,zhang}protected Static voidupdatestr (String str) {str=NewString ("Update");//str= "Update";//these two lines of code effect the same}protected Static voidUpdateinteger (Integer i) {i=Ten;}protected Static voidUpdatebasic (inti) {i=Ten;}protected Static voidUpdatearray (int[] test) {if(test.length>1) test[0]=Ten;}protected Static voidUpdatechararray (Char[] CS) {CS=New Char[3];cs[0]='a'; cs[1]='b'; cs[2]='C';} Public Static voidMain (string[] args) {Stu Stu=NewStu (); Stu.setage (3); Stu.setname ("Zhang"); System. out. println ("1-----------When the method parameter is a normal object"); System. out. println ("Initial value"); System. out. println (Stu.getage ()); System. out. println (Stu.getname ()); Updatestu (Stu); System. out. println ("after modification"); System. out. println (Stu.getage ()); System. out. println (Stu.getname ()); System. out. println ("2-----------When the method argument is string"); String str1=NewString ("haha"); String str2="hehe"; System. out. println ("Initial value"); System. out. println (STR1); System. out. println (STR2); Updatestr (str1); Updatestr (STR2); System. out. println ("after modification"); System. out. println (STR1); System. out. println (STR2); System. out. println ("3-----------When the method parameter is integer"); integer integer=NewInteger (5); System. out. println ("Initial value"); System. out. println (integer); Updateinteger (integer); System. out. println ("after modification"); System. out. println (integer); System. out. println ("4-----------When the method parameter is of eight basic types");intt=5; System. out. println ("Initial value"); System. out. println (t); Updatebasic (t); System. out. println ("after modification"); System. out. println (t); System. out. println ("5-----------When the method parameter is an array of integers");int[] Test ={1,2,3}; System. out. println ("Initial value"); System. out. println (test[0]+"--"+test[1]+"--"+test[2]); Updatearray (test); System. out. println ("after modification"); System. out. println (test[0]+"--"+test[1]+"--"+test[2]); System. out. println ("6-----------When the method argument is a character array");Char[] CS ={'1','2','3'}; System. out. println ("Initial value"); System. out. println (CS); Updatechararray (CS); System. out. println ("after modification"); System. out. println (CS);}}

======================================= output

1-----------When the method parameter is a normal object
Initial value
3
Zhang
After modification
5
Li
2-----------When the method argument is string
Initial value
haha
Hehe
After modification
haha
Hehe
3-----------When the method parameter is integer
Initial value
5
After modification
5
4-----------When the method parameter is of eight basic types
Initial value
5
After modification
5
5-----------When the method parameter is an array of integers
Initial value
1--2--3
After modification
10--2--3
6-----------When the method argument is a character array
Initial value
123
After modification
123

According to the above results, we can draw the conclusion that

1, the basic data type is the value of the pass, (like a C inside the value), it does not modify the value of the argument

2, reference data type is a reference pass, (like a pointer in C), reference can be understood as a Java pointer, it points to the object in the heap assigned to the first address

3, String modified why the actual argument has not changed, this I read a lot of articles said string is equivalent to char[] packaging class. One of the characteristics of a wrapper class is that it reflects the nature of its underlying type when it operates on its value.

But the younger brother has another understanding: str= "Update", this sentence will have the following action, in the stack to find there is no value= "update" string reference, if there is a str point to the heap inside the address, if not it will re-create a String Object " Update, and STR points to the address of the object in the heap. In the above example, calling the Updatestr method assigns the reference of STR to the parameter str, and in this method str points back to another string object, but the process does not modify the string object pointed to by the argument, as is the case with the Updatestu method. As soon as a new object is newly, the object pointed to by the argument does not change.

Java Method Pass Parameter

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.