Difference between C ++ and Java: Name

Source: Internet
Author: User
I bought this c ++ primer. I feel that C ++ is much more complicated than Java. What is the psychological effect?

Summary The difference between C ++ and Java is that it is better to learn it by yourself. I am a beginner in Grade 1. I learned mechanical skills in college. Although I have studied tan haoqiang's C, I only took the test and threw it away after my freshman year. I learned j2se and Android directly after work, after work, I learned something C. The following sections show some of my views after C ++ primer. I have never worked on a C ++ project. I hope you can tell me something wrong.

This article focuses on the use of the identifier in the program.

1. Use the identifier

When I first read c ++, it was quite troublesome. I had to add many header files. In Java, all methods and fields are in the class, and classes are classified by package, just like the file directory of the resource manager in the computer. Each file in the package is a public class, the class name is the same as the file name. Only one class with the same name in the package can be created. The package name has a complicated set of naming rules, which basically guarantees the uniqueness of the absolute path class, even if multiple different projects are together.

In this way, when you use a class somewhere, you can directly use the absolute path + class name, And the JVM can find the class you want. You can also find fields and methods in the class based on the class.

C ++ differs greatly from Java in this respect. It's just me ,. In C ++, if a identifier is used somewhere in a certain place, you must make a declaration before using this file (from C ). No matter where you actually implement it, you have to make a statement before this file. Therefore, for convenience and correctness, a header file is included in the header file. The pre-compiled compiler completely copies the content in the header file to the include field, the field, method, and class .. The statement is added and can be used later. This is equivalent to a copy of the declaration by the compiler. (I guess I don't know if there is such a header file ). During compilation, the location of the compiler to the library file is located. find and use the same name.

For example, there is a class called text. In Java, it is placed in the com. Lee package.

 com.lee.Text text = new com.lee.Text(); 

That's all. In this way, the JVM knows where to find this class, And the programmer knows where to see this class.

C ++ must include the Declaration header file of the text class before it can be used.

#include "Text.h"

Text text();

In this way, the compiler can find the corresponding class, and we can also find the declaration of the text class, just in the text. h header file. But where is the specific implementation ?? To help you find the specific implementation, simply write the specific implementation in text. cpp. In this way, we can find the problem and solve it, but it is a bit of trouble. It is not concise in Java.

2. namespace.

Java is what we mentioned earlier. It is simple and clear to use packages to solve all problems. C ++ is a little complicated. C ++ uses namespace. Some namespaces can be defined to put fields, methods, classes, and so on into namespace. Different namespaces are the same as different packages in Java and can have the same name. You can use the path to distinguish between them. There are two points that can be manipulated: 1 is that the namespace is not consecutive. in Java, it is too easy to find the package, that is, to find a folder by path. The files in this folder, that is, the class is in this package. What can I do in C ++? For example, there is a namespace PS in the project. What exactly is there in this namespace? Why? The namespace is circled in the header file, and all the domain names are in the namespace. But I have to read all the header files to know what exists in PS.

2. If namespace is used, you have to include the header file. The header cannot be bent and the namespace is available. That is to say, the header file is already available. What else should we do?

For example:

ps::Text text();

The text class in the namespace ps. But the problem is that the header file text. h must be included.

In combination with the first question, I thought that the C ++ namespace seemed to be trying to solve the problem of conflicting names in the header file containing multiple headers at the same time. The location is not similar to that of 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.