<2020 04 19> What to pay attention to when switching from C ++ to Java

  Recently I want to play with the APP development of Android and learn Java from the perspective of C ++. Java can be said to be an optimized and compact version of C ++, which removes many features of the underlying C. Found this article.
  
  -------------------------------------------------- -------------------------------------------------- -----------------------------------
  
  Write to programmers: What to pay attention to when switching from C ++ to Java
  
  1 .Java running
  
  Java source code on a virtual machine is not compiled into ordinary machine code. Instead, it is translated into code that the virtual machine can execute. A Java interpreter eventually executes this code. Which is not connected to the process; interpretation dynamically load classes when needed;
  
  2.Java is a fully object-oriented
  
  Java is a fully object-oriented language. This means that the actions you make on any Java object are implemented through a method. The first point is that there is no such thing as an isolated main function. Instead, you must start looking at a program, a class of objects, from the perspective of an object. But what about this object? Most Java programs simply implement what is needed by inheriting the Java base class Object, but you can save time by creating program base classes for multiple applications with similar characteristics.
  
  Strict object-oriented regulations mean that the original C / C ++ code cannot be used without modification; the same is true of system calls. In C ++, you can use the original C procedure calls, including system calls, by declaring extern "C" outside the normal namespace of C ++.
  
  In Java, there is only a similar safe backtracking method, but it is not a very simple method. You must define a local method, the purpose of which is to provide an interface for the C language, and then provide a connection medium. The Java environment provides tools to accomplish this task, but the entire process and the extern provided in C ++ are trivial, and the process of completing the use of C ++ classes is more complicated, because it will introduce problems with the C interface and C functions and C ++ member functions .
  
  Fortunately, many commonly used system utility functions have been provided in methods in system classes, but these obviously do not include the classes and procedures you have created over the years. So, you should delve into it when you need it.
  
  3. There is no separate header file
  
  in Java In Java, everything about classes is put into a separate file. The location of a method can only appear in one place, and the implementation of a method must be performed simultaneously during its definition. The advantage of this is that it is not easy to fail because of the file's non-synchronization error when implementing the program, or obtain a statement that is not implemented. The class declaration can be used by the Java interpreter or even obtained from a compiled unit, so there is no need to have header files, as long as there are compiled files.
  
  The disadvantages of this are related to our programming process. Many C ++ programmers like to use header files instead of documents. To see the interface parameters of a member function, just look at the declaration in the header file. You can often see the header file to learn how to use this class. In Java, there is no such summary. Because the code that implements the class method must appear when the method is defined, and, for a single function code, it often takes up a whole page or more. In this way, it is difficult to get a preliminary understanding of how the class is used by looking at the Java code. You must prepare enough documentation for the classes you need. It goes without saying that the documentation is extremely scarce when dealing with non-commercial libraries.
  
  In the current Java environment, two tools are provided to compensate for these, javap to print the class ID, and javadoc to provide HTML documents for embedded programs.
  
  4. Use Package to decompose the Java namespace
  
  A problem often encountered in large C ++ projects is the namespace-how to ensure that some programmers of the project will not create a class with the same name as other programmers? Even worse, the supplier may provide A library containing classes with the same name as yours. There are many ways to solve this problem, but it is likely that the project has been started before the problem is discovered. It takes a lot of pain to correct the error.
  
  Java solves this problem through the concept of "Package", and Package effectively divides the namespace by collecting classes. Two classes with the same name in different packages are still different. The key question becomes whether the class is placed in the corresponding package.
  
  Remember, Java does not solve the problem of naming conflicts. Extending a base class caused conflicts in derived classes. For example, if your favorite vendor provides some classes, and you use them as base classes and derive a class with a foo method, it may appear when the vendor provides a new version of the class. A foo method is also provided in the new class.
  
  5. Exceptions are an important feature of Java.
  
  In C ++, exceptions and exception handling are very esoteric things; many C ++ programmers have never dealt with them or even know what they are. Exceptions are unexpected errors that occur during the normal process, so they will not be returned from the method or passed as parameters; however, they cannot be ignored! An example here is the method of calculating the square root of a number . The normal interface form is to pass a positive number as a parameter into the method, and then the method will return a positive real number as the result, the method can check these and throw an exception when an exception occurs. In most systems, the programmer does not have to do this, so that an unconsidered exception can cause the program to exit abnormally.
  
  In Java, exceptions have become a very mature part of the language. The method description contains the exception information, and the program processor also enforces that if you use a method that can generate an exception, you must check whether the exception has occurred. Almost all Java programmers will encounter exceptions, because many classes in very useful libraries will throw exceptions. Handling exceptions is not difficult, but it needs attention in some cases. The documentation of a method will indicate the type of exception thrown by the method. If you forget, it doesn't matter, the compiler will remind you.
  
  6. A string is no longer a character array
  
  object that includes a string in Java, and is a constant. Strings are not like character arrays, although you can simply construct a string from a character array. You should replace character arrays with strings as much as possible, because they will not be overwritten by misoperation.
  
  7. Java restricts constant objects and methods.
  
  In C ++, you can formally declare a function parameter or function return value as a const type, which can effectively prevent unauthorized modification of the parameter or return value. In addition, you can declare a member function to be const, indicating that it cannot modify any objects it operates on.
  
  Java supports constant operators and read-only variables, which are implemented through the final keyword. However, Java does not support forcing a writable variable to be read-only during the transfer and return of the function. Or define a constant method that does not manipulate and modify the object.
  
  In Java, the impact of this omission is very small compared to C ++, which is largely due to the difference between string variables and character arrays, but it also brings a hidden danger of causing errors. In particular, there is no way to test whether a method can change an object.
  
  8. Java has no pointers
  
  Understanding the concept of pointers is the most difficult problem for a C or C ++ programmer. Pointers are also a source of errors. There is no pointer in Java, the handle of the object is passed directly as a parameter, rather than passing a pointer. In addition, you must use arrays by index. These are not major problems. However, the absence of pointers causes a lot of trouble when writing systems that contain function pointers or member function pointers. This problem is more significant when dealing with callback functions.
  
  9. Java does not have parameterized types
  
  parameterized types provide a way to process many similar programs with a single program. An example is the square root method, which can operate on int or float. In C ++, this feature is provided by the template.
  
  The equivalent of templates in C ++ is not included in Java. If you often use templates to simplify programs, such as constructing many functions that use similar parameter types, this is simply a disaster. This means that more copy and paste processes are used for manual completion. However, if you use templates to generate classes, there is no easy way.
  
  10. Java uses garbage collection
  
  In the language of garbage collection, the runtime environment always monitors which memory is not used. When a piece of memory is not used, the system automatically reclaims the memory. For example, an object is generated in a method, but it is not called or returned or stored as a global variable, and cannot be used outside the method. The system itself will know which variables you ca n’t use and which ones can be used. Therefore, you no longer have to worry about reclaiming memory for destroying objects. In C ++, a lot of debugging time is used to check memory leaks. This method of Java greatly reduces the possibility of such errors. But he still can't handle the chaotic programs, they can't be recycled. The destructors in many C ++ classes are used to free memory referenced by objects. The fact that Java makes garbage collection shows that it is not necessary to write destructors in Java. But that doesn't mean you can forget to write destructors for your classes. For example, if an object opens a network connection, it must be properly cleaned to close the connection. In Java, the destructor is called the "finalization" method.
  
  11. 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 a table header, 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, Manager needs to inherit from Linked List and Employee.
  
  Java has no multiple inheritance. But you can declare an interface--to describe a programming interface that implements some functions. A class can be implemented by multiple interfaces, including its only function. Different classes can be implemented by the same interface. The parameters of a method can be declared as a class or as an interface. If it is an interface, the class that implements the interface can be passed into the method as a parameter.
  
  The concept of an interface is easier to understand than multiple inheritance, but it has certain limitations. In particular, you must code to reimplement the functionality of the class when implementing interfaces in the class.
  
  12. Java supports multithreading
  
  . Multithreading allows you to write programs that accomplish multiple tasks at the same time. For example, you can allow the user to edit the part that has been read before finishing reading a large file. You need to divide the program into multiple threads to execute. for safety reasons. Your program must be carefully designed, because there may be more than one thread that needs to access and modify the data.
  
  Java supports multithreading from the beginning. Classes and interfaces are used to decompose a program into different threads. The language simply synchronizes or locks important data.
  
  13.
  
  Java is based on some pre-defined classes. The default Java environment includes some packages implemented from Java base classes. These allow you to quickly write some useful programs, these packages are as follows:
  
  java.awt: Many applications today are very dependent on the GUI, java provides an Abstract Window Toolkid, which allows you to do not consider the premise of running platform Handle GUI objects.
  
  java.applet: The main purpose of the applet is to provide browsing related content. It itself is a font of awt components and supports some other features, such as sound, rendering, etc.
  
  java.io: java.io provides read and write operations for streams, files, and pipes.
  
  java.lang: provides the basic classes of java Objcet, Integra, Float ......;
  
  java.net: provides support for network programming. Including processing socket, URL, Internet addressing, etc.
  
  java.util: a set of general utility tools for data structures

Guess you like

Origin www.cnblogs.com/zhenhua1618/p/12729837.html