Home > Java > javaTutorial > body text

Sample code sharing of parameter passing method in java

黄舟
Release: 2017-03-23 10:36:39
Original
1813 people have browsed it

This article mainly introduces relevant information about the detailed explanation of parameter passing methods in java. Friends who need it can refer to

Detailed explanation of parameter passing methods in java

java A classic topic faced by novices. This article is intended to end this topic. There is a saying in Java: Parameter passing in Java is passed by value. How to understand this sentence? I'm afraid it's not easy to explain it in words, and it's probably hard to understand it clearly.

Premise

Let me clarify first, the concept of by value or by reference, it comes from the C++ language, reference is not a word in the Chinese dictionary, but a C++ Concept - Do you remember the symbol "&"?

Why is there this topic? First, there is an incomplete understanding of pass-by-reference; second, many Java books and discussion arguments do not hit the mark.

In one sentence, whether to pass parameters by value or by reference, since it is a parameter passing method, it only applies to formal parameters and actual parameters. What we are talking about here is the parameter itself, not the sub-object or grandchild of the parameter object. object.

With the premise, let’s go to the c++ code:

#include <iostream>

using namespace std;

class User
{
 private:
  int m_id;
 public:
  User(int id=0){m_id = id;}
  void setId(int id){m_id = id;}
  int getId(){return m_id;}
};

void test0(User t){//按值传参
 User s;
 t = s;
 t.setId(1002);
 cout << "test1:" << t.getId() << endl;
}

void test1(User *t){//按值传参
 t = new User();//指针指向了一个新对象,外面实参没变
 t->setId(1002);
 cout << "test1:" << t->getId() << endl;
}

void test2(User* & t){//按引用传参
 t = new User();//指针指向了一个新对象,外面实参也跟着变了
 t->setId(1002);
 cout << "test2:" << t->getId() << endl;
}

int main(int argc, char const *argv[]) {
 cout<< "\npass by ref:"<<endl;
 User* t = new User();
 t->setId(1001);
 cout << t->getId() << endl;
 test2(t);
 cout << t->getId() << endl;

 cout<< "\npass by value:"<<endl;
 t = new User();
 t->setId(1001);
 cout << t->getId() << endl;
 test1(t);
 cout << t->getId() << endl;
 return 0;
}
Copy after login

Output result:

pass by ref:
1001
test2:1002
1002

pass by value:
1001
test1:1002
1001
Copy after login

c++ summary:

Pass by value, then the formal parameters are modified within the function to point to a new object, and the external actual parameters are not affected.

Pass by reference, then the formal parameters are modified within the function to point to a new object, and the external actual parameters are also changed.

Aimed to illustrate the problem, the code may have a memory leak.

Go to java:

package com.pollyduan.bean;

@Data
public class User {
 private Integer id;

 public static void testObject(User t){
  t=new User();//指向了一个新对象,外面实参没变
  t.setId(1002);
  System.out.println("testObject="+t);
 }

 @Test
 public void testObject(){
  User user=new User();
  user.setId(1001);
  System.out.println("user="+user);
  testObject(user);
  System.out.println("user="+user);
 }
}
Copy after login

Output result:

user=User(id=1001)
testObject=User(id=1002)
user=User(id=1001)
Copy after login

java summary:

With c++ Compare the logic and make your own decision.

The above is the detailed content of Sample code sharing of parameter passing method in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!