Java reference C + + reference and C pointer differences

Source: Internet
Author: User
Tags java reference

Java references are essentially pointers in C, whereas C + + references are completely different; there is a class

Class Point {
int x;
int y;
}

The same point P; In Java, p represents a reference, which is equivalent to P in struct point *p in C, whereas in C + +, p is an object that looks no different and behaves differently when passed to a function:

Java Code

public class testpoint{
static void Swap (point point, int x, int y) {
Point temp_p = new Point ();
Temp_p.x =x;
Temp_p.y = y;
Point = Temp_p;
}
public static void Main (string[] args) {
Point P = new Point ();
p.x = 3;
P.Y = 4;
Swap (p,5,5);
System.out.println (p.x+ "" +p.y);
}
}

Execution results, 3 4, because in swap, point is a reference to Java, it is assigned the P reference of the main function, and point pulls a large balloon (point object) in the heap, creates a new object in the function, creates a p,p reference temp_p, The last point releases P, and the temp_p handle pulls the temporary object in the function, so the external p and the corresponding object are the same as in the function bar stuct point *p = temp_p;

C + + code

void swap (Point &point, Double X, double y) {
Point temp_p;
Temp_p.x =x;
Temp_p.y = y;
Point = Temp_p;
}
int main (int argc, char** argv) {
Point P;
p.x = 3;
P.Y = 4;
Swap (p,5,5);
cout << p.x << "" <<p.y<<endl;
}

Output 5 5 Because point is a reference to C + +, all modifications to it are equivalent to the modification of the original variable.

If you put the swap function in & to point, then no matter what is inside the function, the external point P is the same, because in C + + it is interpreted as the object itself, and the function's parameter passing is also passed by value in the same way as Java, so copy a touch-like object in the function. The operation on this object does not affect the outer main function of the object. And Java is different, see the following example

public class testpoint{
static void Swap (point point, int x, int y) {
Point.x =x;
Point.y = y;
}
public static void Main (string[] args) {
Point P = new Point ();
p.x = 3;
P.Y = 4;
Swap (p,5,5);
System.out.println (p.x+ "" +p.y);
}
}

Output 5 5, the function is also passed by value, but copy a touch of the same reference, pointing to the main function of the object, in the function to face the reference operation will affect the main function of the object

Java reference C + + reference and C pointer differences

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.