From C ++ to Java-Understanding object-oriented is the key

Source: Internet
Author: User
From C ++ to Java -- Understanding object-oriented is the key

If you have not been moved to an isolated island, you will surely hear the potential impact of Java on developers and users. In this article, I want to emphasize some differences between C ++ and Java. The goal is not to teach Java programming methods, but to let readers notice some potential problems that may occur when switching from C ++ to Java. This article will provide a concise explanation of these concepts, rather than providing some in-depth or usage questions. Remember, this is only based on some of the major differences I have made in Java.

Java runs on virtual machines
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 ;:

  
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.

Java does not have an independent header file
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.

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.

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.

Character strings are no longer character Arrays
Java contains a String object and is a constant. Unlike character arrays, a string can be constructed from a character array. You should try to replace character arrays with strings, because they will not be overwritten due to misoperations.

Java limits constant objects and Methods
In C ++, You can formally declare a function parameter or return a value of the const type, which effectively prevents improper modification of parameters or return values. In addition, you can declare a member function as const, indicating that it cannot modify any objects operated by it.
JAVA supports constant operators and read-only variables, which are implemented using the final keyword. However, Java does not support forcing a writable variable to be read-only during function transfer and return. Or define a constant method that does not modify objects.
In Java, this omission has a very small impact than in C ++. This is largely because of the differences between string variables and character arrays, but this also brings about a hidden risk of errors. In particular, there is no way to test whether a method can change objects.

Java has no pointer
Understanding pointer is the most difficult problem for C or C ++ programmers. Pointers are also a major source of errors. There is no pointer in Java, and the object's handle is directly passed as a parameter, rather than a pointer. In addition, you must use arrays through indexes. These are not big issues. However, having no pointer causes a lot of trouble when writing a system containing a function pointer or a member function pointer. This problem is more significant when processing callback functions.

Java has no parameterized type
Parameterized types provide methods to process many similar programs with a program. An example is the square root method, which can operate on Int or float. In C ++, this feature is provided by the template.
Java does not contain the template equivalents in C ++. If you often use templates to simplify programs, such as constructing many functions with similar parameter types, this is a disaster. This means that the process of copying and pasting is more manual. However, if you use a template to generate a class, there is no simple method.

Java garbage collection
In the garbage collection language, the runtime environment constantly monitors which memory is not used. When one piece of memory is not used, the system automatically recycles the memory. For example, an object is generated in a method but is not returned by calling or stored as a global variable. It cannot be used outside the method. The system itself will know which variables you cannot use and which ones can be used. Therefore, you do not have to worry about destroying the object to reclaim memory. In C ++, many debugging times are used to check memory vulnerabilities. This method of Java greatly reduces the possibility of such errors. But he still cannot handle logically disordered programs, and they cannot be recycled. The destructor in many c ++ classes are used to release the memory referenced by objects. The fact that Java makes garbage collection shows that it is not necessary to write the destructor in Java. But it does not mean you can forget to write destructor for your class. For example, if an object opens a network connection, it must be properly cleaned up to close the connection. In Java, destructor are called the "finalization" method.

Java does not support multiple inheritance
In any complex object-oriented system, it is very common to implement a new class with more methods. For example, a manager class needs to be used as the header of a connected table, but a manager must be an employee. There are many ways to deal with such problems. One method is to allow inheritance from multiple classes. In this example, the manager needs to inherit from the linked list and employee.
Java does not have multiple inheritance. However, you can declare interfaces to describe programming interfaces that implement some functions. A class can be implemented by multiple interfaces, including its unique functions. Different classes can be implemented by the same interface. Method parameters can be declared as classes or interfaces. If it is an interface, the class implementing the interface can be passed in as a parameter.
The concept of an interface is easier to understand than multi-inheritance, but it has some limitations. In particular, you must encode the class function when implementing the interface in the class.

JAVA supports Multithreading
Multithreading allows you to write programs that complete multiple tasks at the same time. For example, you can allow users to edit the read part between reading a large file. You need to divide the program into multiple threads for execution. For security. Your program needs to be carefully designed because more than one thread may need to access and modify the data.
JAVA supports multithreading at the beginning. Classes and interfaces are used to break down a program into different threads. The language simply synchronizes or locks important data.

Java is based on some predefined classes
The default Java environment includes some packages implemented from the Java basic class. These allow you to quickly write some useful programs. These packages are as follows:
Java. AWT: many applications today rely heavily on the GUI. Java provides an Abstract Window toolkid, which allows you to process GUI objects without considering the running platform.
Java. Applet: The main purpose of the applet is to provide browsing-related content. It is a word class of the AWT component and supports other features, such as sound and rendering.
Java. IO: Java. Io provides read and write operations on streams, files, and pipelines.
Java. LANG: provides Java basic classes such as objcet, integar, float ......;
Java.net: supports network programming. Including socket, URL, and Internet addressing.
Java. util: A common utility set for data structures.

Summary
Many people have said that Java is better than C ++ and regards it as a breakthrough. Some Java features are simpler than C ++, but learning has little to do with serious difficulty and language, but is a basic object-oriented concept. If you understand this, it is extremely easy to learn Java syntax. If this is not the case, you will find Java and C ++ as confusing.

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.