From C ++ to Java (1)

Source: Internet
Author: User

1. Java runs on a virtual machine

Java source code is not compiled into common machine code. It is translated into code that can be executed by a virtual machine. A Java interpreter finally executes the code. There is no connection process, and some classes are dynamically loaded as needed;

2. Java is fully object-oriented

Java is a fully object-oriented language. This means that the actions you perform on any Java object are implemented in one way. The first point is that there is no such isolated thing as the main function. Instead, you must start to look at a program and a class object as an object. But what is this object? Most Java programs simply implement what they need by inheriting Java basic class objects, but you can save time by creating program basic classes for applications with similar characteristics.

Strict object-oriented regulations mean that the use of the original C/C ++ Code cannot be directly modified. The same applies to system calls. In C ++, you can declare extern "C" outside the namespace of C ++ to use the original C process call, including system call.

In Java, there is only one similar method for security backtracking, but it is not very simple. You must define a local method to provide an interface for the C language and then provide the connection medium. The Java environment provides tools to complete such tasks. However, the entire process is insignificant compared with the extern provided in C ++, and the process of using C ++ classes is more complex, this will introduce the excuse for C and the problems of C functions and C ++ member functions.

Fortunately, many common system utility functions have been provided in the methods of the system class, but these obviously do not include the classes and processes you have created over the years. So you should study it when you need it.

3. There is no independent header file in Java

In Java, everything about classes is put in a separate file. The location of a method may only appear in one place, and the implementation of a method must be performed simultaneously in its definition process. The advantage of this is that it is not easy to fail due to non-synchronization errors of files during the implementation of the program, or to get an unimplemented declaration. The class declaration can be obtained by the Java interpreter or even from a compiled unit, so there is no need for header files, as long as there are compiled files.

The disadvantage of doing so is related to our programming process. Many C ++ programmers prefer to use header files instead of documents. To view the interface parameters of a member function, you only need to view the declaration in the header file. You can read the header file frequently to learn how to use this class. There is no such summary in Java. Because the code that implements the class method must appear when the method is defined, and the code of a separate function often occupies a whole page or even more. In this way, it is difficult to get a preliminary understanding of how classes are used by looking at Java code. You must prepare enough documents for the classes you need. It is self-evident that documents are extremely lacking when processing non-commercial class libraries.

In the first Java environment, two tools are provided to compensate for this. javap is used to print class identifiers, and javadoc provides HTML documents for embedded programs.

4. Use Package to break down Java namespaces

A common problem in large C ++ projects is the namespace. How can we ensure that some programmers in the project do not create classes with the same name as other programmers? Worse, the supplier may provide a library containing the same name as your class. There are many ways to solve this problem, but it is likely that the project has been started before the problem is found, correct the error requires a lot of pain.

Java solves this problem through the concept of "Package". Package effectively divides namespaces through collection classes. Two classes with the same name in different packages are still different. The key issue is whether the class is placed in the corresponding package.

Remember, Java does not solve the naming conflict issue. The extension of a base class causes a conflict between the derived classes. For example, if your favorite vendor provides some classes, then you use them as base classes and derive classes with a foo method, when the supplier provides a new version of the class, it may appear. If the supply business also provides a foo method in the new class.

5. Exceptions are an important feature of Java.

In C ++, exception and exception handling are very esoteric. Many C ++ Programmers never process them or even know what they are. Exceptions are unexpected errors in the normal process. Therefore, they are not returned from the method or passed as parameters. However, they cannot be ignored! Here is an example of how to calculate the attention of a book. The normal interface form is to pass a positive number as a parameter into the method, and then the method returns a positive number as the result. The method can verify this and throw an exception when an exception occurs. In most systems, programmers do not have to do this. In this way, an unspecified exception can cause the program to exit abnormally.

In Java, exceptions have become a very mature part of the language. The description of the method contains the exception information. The program processor also forces you to check whether an exception occurs if you use a method that can generate an exception. Almost all Java programmers encounter exceptions, because classes in many very useful libraries throw exceptions. It is not difficult to handle exceptions, but you must pay attention to them in some cases. The document of a method specifies the type of the exception thrown by the method. If you forget, it doesn't matter. The compiler will remind you.


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.