C ++, Java language and development tools

Source: Internet
Author: User

When talking about Java, people will immediately think of an object-oriented programming language similar to C and C ++ that is suitable for distributed environments, think of the features of this language, such as simplicity, stability, security, unrelated to the architecture, portability, interpretability, high performance, multi-clue and dynamic. These are the main features of Java as a programming language. However, if you want to better understand the Java language, the best way is to compare the language with the same type of language. Since Java is similar to C ++, it is logical to compare it with C ++.

I. Java language

Java was developed by a technical team of Sun. During the implementation of the Java language, the team's technical staff quickly realized that C ++ could not become a fully object-oriented and networked development language. C ++ is developed by adding object-oriented functions to the original C language. Therefore, it has inherent limitations. This is mainly reflected in the wide variety of C ++ features, a large number of redundancy, and no C ++ compiler can support all its functions. In view of this situation, Sun's technical staff decided not to expand C ++, but to develop a new computer language (Oak, the predecessor of Java ). However, C ++ has become a language that most programmers are familiar with. The Java design obviously cannot ignore this reality. If the difference between Java and C ++
If it is too large, it will undoubtedly take a lot of time and effort for programmers to learn this language. Therefore, Java retains as many C ++ styles as possible.

Java is completely object-oriented, which is different from C ++. Although C ++ is considered to be an object-oriented language, it still supports a procedural programming style. Java does not. It requires programmers to adopt fully object-oriented programming technology to eliminate the problems caused by mixing two identical programming styles. On the other hand, Java's "appearance" and "feeling" have many similarities with C ++. Java eliminates the redundancy in C and C ++ and all procedural programming styles, and does not support pointer data types. It also adds automatic array boundary checks and automatic memory garbage collection, in addition, the array is encapsulated in the class structure to facilitate the development of applications in distributed computing environments.

However, the most important reason why Java can become an ideal programming language is its "embedded" memory management and multi-clue functions. The C ++ language allows programmers to write very low-level programs to access machine hardware and manipulate the actual memory address. But these are at the cost of portability, because every program is targeted at a specific hardware environment. Java ensures code portability by providing various boundary checks and memory management. It also provides embedded multi-clue functions, this allows operations similar to memory garbage collection to be performed in the background, thus ensuring high execution efficiency.

Java has these advantages because it is designed as a brand new language. If only C ++ is expanded, Java will not be able to avoid a large number of procedural features left over from C ++, and its interpreter scale will increase sharply, thus reducing the overall system performance, even possible implementations of code portability and stability will be very difficult. In short, these features of Java have laid a good foundation for it to become the next generation programming language.

1. Java object-oriented features

The key to object-oriented technology lies in encapsulation, inheritance, and dynamic Association. Let's talk about how Java supports these key features.

The basic unit of object-oriented technology is objects, which are called classes in Java ). A class is actually a piece of code used to implement object behavior. It is fundamental to providing encapsulation. That is to say, the object and all its related operations are encapsulated into a class.

All objects have a state. The class is also in the status, represented by "instance variables. Instance variables are controlled by corresponding classes. Except in some special cases, none of the classes can access these variables. Instance variables are accessed through methods. A method is equivalent to a function in a procedural language. It is a code segment that can be called multiple times. The programmer must call a method to control the class, that is, to change the class State, and only implement the operations allowed by the class method. This is how object-oriented technology works.

Of course, in some cases (for some object-oriented languages), programmers can also directly access instance variables, but this is not a recommended practice, because it violates the original intention of object-oriented.

The main advantage of object-oriented programming is the reusability of code. When repetitive operations are required, programmers do not have to overwrite the same code segment repeatedly, but only need to create multiple instances of objects. Because programmers do not need to understand how objects are created, nor do they need to know the internal working mechanism of objects. All they need to know is the call mechanism of object methods, the rest of the work will be processed by the class to which the object belongs. In Java, programmers can write classes that complete various functions, publish compiled code, and only need to tell users how to control these classes.

Java implements objects through classes and provides multiple usage of classes. Class can be used as a template for each object instance. To create a new instance of a class, you can call its constructor. It is a special method that must be called whenever a new copy of the class is created. When all programs of a class have ended running, or Java feels that it is no longer necessary to call the class (for example, the application has ended), it must call the finalize function of the class ), to perform some necessary cleanup work. Of course, the final function of Java is different from the destructor in C ++, Because Java can automatically clear garbage.


In Java, access control for variables and methods is implemented through access modifiers. Java defines four types
Access Level: public, protected, private, and friendly ).
Methods and variables of the Public type can be accessed from any class. Methods and variables of the protected type can only be accessed from the current
Class and its subclass access; private methods and variables can only be accessed from the current class. If none of the three
An explicit description of any type indicates that the corresponding method or variable belongs to the "friendly" type, which can be
All classes in the same package are accessed. In Java, the "package" is a separate compilation ticket
It is a set of classes with a certain degree of commonality. You can define the unit of the package to implement related objects.
And isolate these objects from other objects in the package.

Object-Oriented Programming is an extremely important aspect of Java. In Java, if no new class is created,
You cannot create an application. Java defines a special superclass Object. All the classes created by the user are objects.
. The J ava program can be run either in the interpreter or in the browser.
You must create a class instance before submitting the job. In addition, using the Inheritance Mechanism, users can also make full use of profits.
Use functions provided by various superclasses.

2. Differences with C ++

Although C ++ programmers can master Java programming relatively easily, they must overcome
Old habits. Java cancels the following features or functions in C ++:

· Structure or pointer · # define statement

· Pointer · multi-Inheritance

· Independent functions · GOTO statements

· Operator Overloading · Automatic type conversion

Java does not support structures and pointers, and removes the # define or # typedef statements and header files in C ++.

In fact, the two statements constant and typedef contain the # define statement. Now, the structure and
The Union has been replaced by Java classes. The reason for deleting these features is that it wants to maintain
Backward compatibility. The C ++ language specification contains a large amount of redundancy. For example, the class actually includes the structure.
Therefore, the two data structures can be completely canceled. About # define statements, Java language rules
The author of Fan believes that although the starting point of the statement is to enhance the readability of the program, the actual effect is
On the contrary, it often leads to unreadable code, so it should be canceled. Java does not support independent functions. Therefore
All functions must be encapsulated in a class. It is widely believed that the superclass used by C ++ are very unstable.
Therefore, Java has abandoned the multi-inheritance in C ++ and replaced it with interfaces. Java interfaces refer
To a class. It only shows the methods, constants, and variables of a class, rather
The full structure of this class.

Finally, Java removes the GOTO statements, Operator overloading, automatic type conversion, and pointer data classes in C ++.
Type. The GOTO statement has been controversial for many years, but it has been quite confusing.
The statement has always been irrelevant. C ++ still supports automatic conversion of data types, but Java requires programmers
Explicitly converts data types. Automatic data type conversion makes two data types incompatible
Variables can be assigned values to each other without explicit descriptions. This sometimes causes some problems, the most common of which
The loss of precision. For example, if a signed 32-bit integer is assigned to an unsigned integer
All results are positive numbers. Java designers think that this can easily cause program errors and thus decide not to support
This conversion method is supported.

Some C and C ++ programmers may feel sorry, Because Java does not support the pointer classes they are very willing to use.
Type. But we all know that pointers are one of the biggest headaches during program debugging. Java designers
It is considered that the pointer is no longer useful because Ja va removes the structure type and encapsulates the array into the object.
Location. However, some C and C ++ senior programmers may still need some time to get out of the pointer shadow.
Although pointer usage is difficult to grasp, once you have mastered its usage skills, it will make people feel its functions
It is indeed very powerful. As a programming language for distributed computing environments, Java does not support pointers.
It is very logical, because only in this way can a stable, efficient, and
Secure Code.

3. Memory Management and clues

The advantage of Java is its automatic memory management and clue control. In C and C ++, the memory must be used
Free, malloc, and many other memory management standard libraries. Therefore, the system should know when to allocate and release
It is very difficult to store memory and track all memory usage. If you want to implement clues in C and C ++
Function, you must use a class library that can control the clues. In Java, although the clues still need
Class support, but the system includes the clue synchronization mechanism at the language level.

Java has embedded memory management programs. Once an object is created, the Java runtime system starts tracking
This object until it no longer needs to trace all references of this object. When an object does not have a reference
Java puts this object in a stack for garbage collection. To minimize performance loss
Provides an automatic garbage collection mechanism. This garbage collection program runs in the background or as a low-priority clue. This
Sample: Java's garbage collection software will be executed during the execution of front-end clues, or explicitly put forward
It is executed when the memory space occupied by the dead classes is used.

From the background Memory Management Program, we can clearly see that multiple clues can indeed enhance the relative performance of the Java environment.
Given the importance of multi-clue, Java provides a clue synchronization mechanism at the language level, making multi-clue an embedded
Features. Java supports synchronous modifiers for methods.

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.