Learn the difference between C + + and Java

Source: Internet
Author: User
Tags abstract definition

I originally learned Java, because of working reasons to contact C + +. In the C + + contact process, made a lot of mistakes, but still tangled in the grammatical details, it makes me difficult to adapt. At the beginning of the consensus that may be done less, a project can encounter the design of grammatical details of the aspect is quite limited, if only to complete the work insurance, but the understanding of C + + is limited to a code of the level of farmers.

Perhaps for the object-oriented perspective of the difference is too persistent, until the complete reading of the "C + + programming thought" to understand the subtle differences between, in fact, many grammatical rules are similar. It's just that the focus and the way of thinking is still very large, perhaps this is C + + to Java, it may feel Java simple, but the construction of a reasonable project is the C + + way, Java's convenience and ideas have not been touched. Java to C + + is, the complexity of the grammatical details and flexibility to fully dizzy the past. In fact, they do not have a good understanding of the parts of their language.

To understand the abstract definition, Java should focus on design issues such as the organization of classes, the level of management of class responsibilities, etc.

C + + to understand the language fully, in addition to the above mentioned, but also should pay attention to the memory problem of thinking and judgment.

1. Object-oriented

For C + +, when it comes to object-oriented, object-oriented design

For Java, when it comes to object-oriented, object-oriented thinking

Do not underestimate the difference between the two, C + + is still the C language of the derivative version, through the "C + + programming thought" In fact, for class design, inheritance, overloading these are not regarded as object-oriented characteristics to judge, but provides a code to take the form of expression and "late bundle" in C + + The difference between the compiler and the C language compiler is something. But in "Java programming thought", it is the class, inheritance, overloading, polymorphism and so on as a very important object-oriented thinking to see.

To give an example, the static modifier

In C + +, the existence of static member variables in the class and Java is in fact consistent, but C + + still has C language features, static life still has a global concept. There is no such thing as a global concept in Java, and more accurately, the global concept is hidden by the class boundary. The role is consistent, but Java is static provides class sharing, it may not be done by C + + to determine the appropriate

2. Code

This is also a lot of people ignore, language is just tools, as long as the algorithm to do is, but the tools are each has its own characteristics, often also ignored.

Take an example: null pointer judgment, array out of bounds, exception, assertion

What may be said above is that C + + is fully controlled, if not controlled, there will be a large problem, directly corresponding to the memory of the exception. However, Java, because it is a pure object-oriented language, coupled with a complete virtual machine-based exception control, so that the array out of bounds may not be a major problem, throws a exception and memory anomalies compared to a much smaller. Visible Java's hidden benefits for memory

3. Organization

Look at the C + + code, give me a very deep impression is that I can very accurately understand his organization and function. However, Java I need to fully understand the hierarchical structure and boundary problems between classes, often to build a library when mapped to the upper layer, it is difficult to see the entire form and function, can only be based on the invocation of the class to comb.

It can also be seen that C + + or inherited the C language features, is a document directory structure to do code organization. While there are object-oriented features, object-oriented only gives C + + a code to take and design-time logic to differentiate. For Java, however, object-oriented is consistent.

As an example: introduction of C + + header file

A lot of people are not used to why I write a class, but also to introduce a header file, and Java similar to write to a file do not?! In fact, C + +, which is strictly followed by the first declaration after the use of logic. Therefore, the introduction of the header file of the chaotic management can be achieved through this point of departure, a certain consistency.

4. Library function Difference

At first glance, I think C + + library functions can do, Java has done. But there are still some subtle distinctions.

Take an example: the sort () function

The sort of vectors in C + + is done by sort (vec.begin,vec.end,_compare)

In Java, it is necessary to replicate the Compare method that needs to be compared to the class.

So you still think C + + is an object-oriented language? Obviously is a class and another class comparison, this comparison is a class behavior, why do you want to put a comparison in sort, this jumps out?

Learn the difference between C + + and Java

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.