function value transfer and object Transfer in Java block chain

Source: Internet
Author: User
Tags wrapper
function Value delivery and object delivery in Java article link: http://blog.csdn.net/qq_16628781/article/details/72810012 Knowledge Points:The problem of function argument is put forward, and the summary of function argument is given, and the basic data type and object of the function are explained by examples. 4. Term Record {Java function pass value and pass reference} Overview

When we write functions, we often need to pass parameters to go in, but there is a reference here is not very noticeable knowledge, if not to understand, then there are some strange things that will make you "unexpected."

failure to properly understand the problem of communication can lead to some strange phenomena. For example, once in a project, a list type parameter was passed, but it was strange that the list object outside the function was empty each time the method was executed.

It never occurred to me at that time what caused the problem. Today specially searched for the question which the Java function passes the parameter, only then understood this question.

Let's start with a summary. Summary

In the Java parameter pass, it is divided into value passing and reference passing. (Many people say that the following is an object transfer, but Java, everything is object, or say that the reference is better), the following first to a summary: If the transfer of the basic data type values, the modification of the formal parameters will not affect the argument. If you pass a non-basic data type, such as a wrapper type object, the formal parameters and arguments point to the same memory address (the same object), so modifications to the formal parameters affect the properties of the argument.

Explanation 1: The basic data types include, byte,short,int,long,double,char,string, and their wrapper type. They are immutable data types.

EXPLANATION 2: Non-basic data types include, List,stringbuffer, and custom entity classes all belong to a non-basic data type. Example Explanation

Passing basic data type int

The method of the Paramspass class is public
void ParamPassTest1 (Integer a) {
    a =;
    System.out.println (TAG + ":" + a);
}
Call the above method
Paramspass paramspass = new Paramspass ();
Integer a = new Integer (a);
Paramspass.parampasstest1 (a);
System.out.println (TAG + a);

Execute the above method and see that the int type argument passed is called inside the function, and the value of the outside a has not changed. The results are shown in the following illustration:

Passing objects

Userbean entity Classes

public class UserBean implements Serializable {//serialized version uniform identifier private static final long serialversionuid = 1L;

private String password;

Public String GetPassword () {return password;}
public void SetPassword (String password) {this.password = password;}} 1. Test method public void ParamPassTest2 (UserBean UserBean) {System.out.println ("Inside_func_userbean_hashcode:" + Userbe
    An.hashcode ());
    Userbean.setpassword ("000000");
System.out.println ("Inside_func_userbean_password:" + Userbean.getpassword ());}
    2, call test method Paramspass Paramspass = new Paramspass ();
    UserBean UserBean = new UserBean ();
    Userbean.setpassword ("123456");
    System.out.println ("Outside_func_before_password:" + Userbean.getpassword ());
    System.out.println ("Outside_func_before_hashcode:" + userbean.hashcode ());
    Paramspass.parampasstest2 (UserBean);
    System.out.println ("Outside_func_after_password:" + Userbean.getpassword ()); System.out.println ("Outside_func_after_hashcode:"+ Userbean.hashcode ()); 

In the above, a Userbean entity class object is passed in, the initial password is "123456", then the password is changed to "000000", then the Userbean object in the outside and the hashcode of the object passed into the function are printed. The result is the same address, which means that passing the object is a reference to the object, in the method to modify the parameters of the object, the referenced object will be modified.

Incoming List Object

The 
//userbean is the same as the above, where the//test method is omitted, the print hashcode and the value public void ParamPassTest3 (list<string
    > list) {System.out.println ("Inside_func_hashcode:" + list.hashcode ());
    List.add ("other value");
    System.out.println ("inside_func_after_added_size:" + list.size ());
    List.clear ();
System.out.println ("inside_func_after_cleared_size:" + list.size ());}
    Invoke the above test method list<string> stringlist = new arraylist<> ();
    Stringlist.add ("value1");
    Stringlist.add ("value2");
    System.out.println ("Outside_func_before_hashcode:" + stringlist.hashcode ());
    System.out.println ("outside_func_before_size:" + stringlist.size ());
    Paramspass.parampasstest3 (stringlist);
    System.out.println ("outside_func_after_size:" + stringlist.size ()); System.out.println ("Outside_func_after_hashcode:" + stringlist.hashcode ()); 

The results are shown in the following illustration:

You can see the printout, two objects are the same object, so when passing the list object, it is also a reference to the object, the parameter list object is manipulated, but also affects the properties of the outside argument. Because the list is not a basic data type anymore.
Similarly, so is stringbuffer,stringbuilder. Pseudo Summary

Having said so much, the question that was raised at the beginning was solved. Here is no longer long-winded, summed up in the previous has been written.

The above is all content, if have any question, please contact me in time, thank you.

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.