I want to talk to you about Java and C + +

Source: Internet
Author: User

Headache Headache One:

In Java, essentially, a class is a programmer-defined type, which is a reference type (reference type), which means that a variable of that class type can refer to an instance of the class. On the surface, an object reference variable appears to hold an object, but in fact it simply contains a reference to the object. Strictly speaking, object reference variables and objects are different, objects (new Person ()), object reference variable (person p). But in most cases, this difference can be ignored. Therefore, it is easy to say that p is a person object, rather than a lengthy description, p is a variable that contains a reference to the person object. -----Java Basics P278

So, person p; P.A = 2; This is not possible, because at this time P has not pointed to any memory. Is null

So I need person P = new person ();

Instead, C + + direct person p; You can call P.A = 2;

Headache Headache Two:

The method inside Java,

     Public void fuck (final  StringBuilder str) {        str.append ("haha");    }

This final only says that Str cannot be changed, that is, Str cannot point to other places, or it can modify the content it points to. For example, the above is OK, will be modified

But C + +

void Fun (const

It has been shown that the content of STR is immutable, as far as the point, which is a value copy, changes the impact on me. It's just a temporary change in the function.

Another example

void Fun (charconst  str) {    str[0] = ' 0 ';}

The pointer constant indicates that the pointer is a constant and can no longer be changed, but it is still possible to modify the value of the block of memory it points to.

Note: This time the block of memory needs to exist, if you are char * str = "liuweiming", so that it will change the error, because the string constant cannot be changed

String constants are saved in the code area

So the big difference is that the final string str, which is different from the const char * str

void Fun (char * const STR) {str[0] = ' 0 ';}

I want to talk to you about Java and C + +

Related Article

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.