The similarities and differences between C + + and java,c# (i): value, address, reference

Source: Internet
Author: User

Java,c# is already familiar with the recent study of C + + from 0 onwards. The learning process will inevitably be compared with the java,c#, there are spit groove, also a bit praise.

First of all, the most basic is the most important part: the way the parameters are passed.

For a type, a Java sub-base type, a composite type, and a different angle are value types, reference types. Before we start the comparison, let's look at three key ways:

    • Value

      • Create a new copy that has no relationship with the original
    • Address (pointer)

      • Pass the address of the memory where the object resides
    • Reference

      • An alias that can be understood as a variable

As an example:

1 voidTestmethoda (intinput) {2input =999;3 }4 5 voidTestmethodb (int*input) {6     //int newvalue = 888;7*input =888;8 }9 Ten voidTESTMETHODC (int&input) { Oneinput =777; A } -  - intMain () { the  -     intinput =111; -cout <<"Original:"<< input <<Endl; - Testmethoda (input); +cout <<"After Testmethoda:"<< input <<Endl; -cout <<input; +Testmethodb (&input); Acout <<"After Testmethodb:"<< input <<Endl; atcout <<input; - testmethodc (input); -cout <<"After testmethodc:"<< input <<Endl; -  -Std::cin.Get(); -  in     //What is the value of input? -  to}

Results after the run:

111111888777

Explain:

The parameter of the function Testmethoda is the value passed, after the function is called, a copy of input is created, the value of the copy is changed, but the input value is not changed.

The parameter of the function Testmethodb is the address (pointer), which modifies the area of memory that the input points to, so the value of input is changed.

The behavior of the function testmethodc looks like the function Testmethodb, and the input value is changed, what is the difference between them?

The difference is:

The delivery address, within the function, changes the contents of the area of memory that the variable points to.

Passing a reference, the function changes the direction of the variable, in other words, the memory address to which input points is changed.

Such as:

In C + +, all types, regardless of the underlying data type, struct, or class, are passed by default "value", explicitly declared as a pointer, is a pass-through address, and explicitly declared as a reference, which can be thought of as an alias for the variable.

In Java, only the underlying data type (int, double, float, etc.), is the value of the pass, all the class object, is the address (note, not a reference), in fact, there is no reference in Java concept of the transfer.

In C #, the underlying data type, struct, is passed by default, all objects are addressed by default, and if you want to pass a reference, add the REF keyword before the argument, for example:

1  void testmethodc (refint  input) {2      777; 3  }

The similarities and differences between C + + and java,c# (i): value, address, reference

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.