This method can change the properties of this object and return the changed results, so is this a value pass or a reference pass?

Source: Internet
Author: User

For:

is a value pass. Method calls in the Java language only support value passing of parameters. When an object instance is passed as a parameter to a method, the value of the parameter is a reference to the object. The properties of an object can be changed during the call, but changes to the object reference do not affect the caller. In C + + and C #, you can change the value of an incoming parameter by passing a reference or transmitting a parameter. You can write code like the following in C #, but you can't do it in Java.

Using System;

Namespace CS01 {

Class Program {
public static void swap (ref int x, ref int y) {
int temp = x;
x = y;
y = temp;
}

public static void Main (string[] args) {
int a = 5, B = 10;
Swap (ref a, ref B);
A = ten, B = 5;
Console.WriteLine ("A = {0}, B = {1}", A, b);
}
}
}
Description: There is no reference in Java is very inconvenient, this point in Java 8 is still not improved, it is so in Java code is written in a large number of wrapper class (will need to be modified by the method call to the reference in a wrapper class, Then passing the wrapper object into the method will only make the code bloated, especially for developers who are transitioning from C and C + + to Java programmers.

This method can change the properties of this object and return the changed results, so is this a value pass or a reference pass?

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.