A piece of code helps you understand in seconds whether the java method is to pass the value or to the address

Source: Internet
Author: User

First look at the code and running results:

The comment is clearly written, so I won't say much about it.


I will talk about my conclusions. In fact, there is no concept of passing values or transferring addresses in java. java only has a reference concept. The reference is similar to the address transfer, but the address of the object is saved in a variable name, and the address points to an object. The reference is similar to passing a value. For the method, the form parameter and real parameter are both variable names, and the addresses in the variable names are the same. It means that the passed value is the value of the variable (the saved address) it is passed from the real parameter to the form parameter.


The above is my opinion. You are welcome to discuss this opinion together.


On the source code, do not want to write and want to try it again:

package com.elong.valueoraddress;public class Student {private int id;private String name;private int classId;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getClassId() {return classId;}public void setClassId(int classId) {this.classId = classId;}@Overridepublic String toString() {return "Student [id=" + id + ", name=" + name + ", classId=" + classId+ "]";}}

Package com. elong. valueoraddress; import org. junit. test; public class TestStudent {// The object address is stored in the variable name @ Testpublic void test () {Student student = new Student (); student. setId (1); student. setName ("elong"); student. setClassId (1); // the address of the object stored in student is changed (student); System. out. println (student);} public void change (Student student) {Student student2 = new Student (); student (2); student2.setName ("elongdeo"); student2.setClassId (2 ); // change the attributes of the objects pointed to by student. student. setId (3); student. setName ("dingyinlong"); student. setClassId (3); // student = student2 indicates assigning the address of the object pointed to by student2 to studentstudent = student2; // changing the attributes of the object pointed to by the original student2 student. setId (4); student. setName ("yinlong"); student. setClassId (4 );}}

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.