Brief analysis: The difference between C + + and Java (beginner must see) __python

Source: Internet
Author: User
Tags garbage collection goto

With the development of the Times, more and more programming languages have come into our life and come into our classroom. In the face of a variety of programming languages, so that the students are not too good to learn more confused, recently, from the online collation of some of the data on C + + and Java for beginners reference, but also hope to get the teachers to correct and comments, we would like to learn from each other and common progress.

Introduction of C + +

C + + is a very broad use of computer programming language, is based on the C language development evolved. It is a kind of static data type checking, and it supports the universal programming language of multi-paradigm. C + + supports process programming, data abstraction, object-oriented programming, generic programming, design based on principles and many other programming styles.

The C + + language retains both the validity and flexible, easy to transplant and so on all the essence and characteristics, but also added object-oriented programming support, with a powerful programming function, can easily construct the simulation of real problem entities and operations, the program has a clear structure, easy to expand and other excellent features, suitable for a variety of application software , the System software program design. Written in C + + program Readability is good, generated code quality is high, running efficiency is only slower than assembly language 10%~20%.

The C + + language has the following characteristics:

(1) C + + is a superset of the language. It keeps the C language concise, efficient and close to assembly language features, but also overcome the shortcomings of C language, its compilation system can check more grammatical errors, therefore, C + + is more secure than C language.

(2) C + + has maintained compatibility with the language. The vast majority of C language programs can be directly run without modification in the C + + environment, many library functions written in C can be used in C + + programs.

(3) Supporting the characteristics of object-oriented programming. C + + supports both process-oriented programming and object-oriented programming.

(4) C + + programs in terms of reusability, scalability, maintainability and reliability are more than C language has been improved, so that it is more suitable for the development of large and medium-sized system software and applications.

Second, Java Introduction

Java is a generic term for the Java programming language (hereinafter referred to as the Java language) and the Java platform, which was launched by Sun Microsystems in May 1995. Java-enabled HotJava browsers (Java applets) show the Java charm: cross-platform, Dynamic web, Internet computing. Since then, Java has been widely accepted and facilitated the rapid development of the web, and common browsers now support Java applets. On the other hand, Java technology is constantly being updated.
The Java platform consists of Java virtual machine (Java Machine) and Java application Programming interface (application programming Interface, abbreviation API). The Java application programming interface provides a standard interface for Java applications that is independent of the operating system, and can be divided into basic and extended parts. After you install a Java platform on a hardware or operating system platform, the Java application runs. The Java platform is now embedded in almost all operating systems. This allows the Java program to be compiled only once and run on a variety of systems. The Java application programming interface has been developed from 1.1X to version 1.2. The most common Java platform is based on Java1.4, and the most recent version is Java1.7.
Java is divided into three systems J2se (JAVA2 Standard Edition), Java EE (2 platform,enterprise Edition), J2ME (Java 2 Micro Edition).

Java is a simple, object-oriented, distributed, interpretive, robust, secure, structurally neutral, portable, high-performance, multi-threaded dynamic language.

The three main features of the Java language are: encapsulation, inheritance, polymorphism

Encapsulation: Hides the attributes and implementation details of an object, exposes only the interface, and controls the level of access to read and modify attributes in the program. The abstraction of the data and behavior (or function) combined to form an organic whole, that is, the data and operation of the source code for the organic combination of "class", where the data and functions are members of the class. The purpose of encapsulation is to enhance security and simplify programming, and the user does not need to know the specifics of the implementation, but only to use the members of the class through the external interface, a specific access right. The basic requirement for encapsulation is to privatize all attributes, provide getter and setter methods for each property, and, if there is a constructor with a parameter, write a constructor with no parameters. The classes that have been written are often tested during development, so there are times when the ToString method is rewritten, but this is not necessary.

Inheritance: Code reuse is implemented through inheritance. All classes in Java are obtained by inheriting the Java.lang.Object class directly or indirectly. The inherited class is called a subclass, and the inherited class is called the parent class. Subclasses cannot inherit member variables and methods that have access to private in the parent class. Subclasses can override methods of the parent class and name a member variable with the same name as the parent class. However, Java does not support multiple inheritance, that is, the ability of a class to derive from multiple superclass classes. Minimize inheritance relationships in development in order to reduce the coupling of programs.

Polymorphism: Polymorphism is divided into design-time polymorphism and Run-time polymorphism, for example, overloading is also called design-time polymorphism, and for overridden or inherited methods, the Java runtime system determines which method is called Run-time polymorphism based on the type of instance that invokes the method. All in all, the typical feature of object-oriented design is inheritance, encapsulation, and polymorphism, which are also key to the popularity of object-oriented objects.

Three, C + + and Java differences

Java and C + + are both object-oriented languages. In other words, they are able to implement object-oriented thinking (encapsulation, relay, polymorphism). And because C + + to take care of a lot of C language users,

And the compatibility of C, make itself only become a Class C language, more or less affect the thoroughness of its object-oriented. Java is a complete object-oriented language, its syntax is clearer, smaller, and easier to learn. It is based on a detailed study of a variety of programming languages, according to abandon the shortcomings of other languages, fundamentally solve the inherent defects of C + +.

Java and C + + have more similarities than differences, but the two major differences in the language make Java easier to learn, and the programming environment simpler.

I am not here to list the differences entirely, but to list the more significant differences:

1. Pointer

The Java language makes it impossible for programmers to find pointers to directly access memory without pointers, and adds automatic memory management capabilities to effectively prevent pointer manipulation errors in C + + languages, such as system crashes caused by wild pointers. But it is not that Java has no pointers, the virtual machine is still using the pointer, but outsiders can not use it. This facilitates the security of Java programs.

2. Multiple inheritance

C + + supports multiple inheritance, which is a feature of C + + that allows multiple parent classes to derive a class. Although multiple inheritance is powerful, complex, and can cause a lot of trouble, it is not easy to compile programs to implement it. Java does not support multiple inheritance, but allows a class to inherit multiple interfaces (extends+implement), achieve multiple inheritance functions of C + +, and avoid the inconvenience caused by multiple inheritance implementations in C + +.

3. Data types and Classes

Java is a fully object-oriented language, and all functions and variable parts must be part of the class. In addition to the basic data types, the rest are used as class objects, including arrays. Objects combine data and methods to encapsulate them in a class so that each object can implement its own characteristics and behavior. C + + allows functions and variables to be defined as global. In addition, Java eliminates the unnecessary hassle of eliminating the structure and the Union in C + +.

4. Automatic memory management

All objects in the Java program are built on the memory stack using the new operator, which is similar to the new C + + operator. The following statement consists of an object that establishes a class read, and then calls the work method of the object:

Read R=new read ();

R.work ();

The statement read R=new read (), and an instance of read is established on the stack structure. Java automates garbage collection operations without the need for programmers to delete them. and the C 20 must release the memory resources by the program shell, increased the program designer's negative throw. In Java, when an object is not reused, the garbage collector will label it as a deletion. In Java, the garbage collector runs in the background in a threaded fashion, working with idle time.

5. Operator overloading

Operator overloading is not supported in Java. Operator overloading is considered to be a prominent feature of C 20, although in Java the class can generally implement such a function, but the convenience of operator overloading is still missing a lot. The Java language does not support operator overloading in order to keep the Java language as simple as possible.

6. Preprocessing features

The preprocessing feature is not supported in Java. C/E 20 has a pre-warp phase in the compilation process, known as a preprocessor. The preprocessor provides convenience for developers, but increases the complexity of butyl compilation. The Java Virtual machine does not have a preprocessor, but it provides an introduction statement (import) similar to the functionality of the C 20 preprocessor.

7. Java does not support default function parameters, while C 20 supports

In c, code is organized in functions, and functions can access the program's global variables. C 20 Adds a class, provides a class algorithm, which is a function connected to the class, the C 20 class method is very similar to the Java class method, however, because C 20 still supports C, so it is not possible to prevent C 20 developers from using functions, resulting in mixed use of functions and methods to make the program more confusing.

Java has no functions, as a more pure object-oriented language than C 20, Java forces developers to include all routines in the class, and in fact, using methods to implement routines can motivate developers to better organize their code.

8 string

C and C 20 do not support string variables, the use of NULL terminators in C and C 20 programs to represent the end of strings, in Java strings are implemented using class objects (STRINR and StringBuffer), which are the core of the Java language, Implementing a string with a class object has several advantages:

(1) The method of establishing the string and accessing the string elements in the whole system is consistent;

(2) J3-Yang string classes are defined as part of the Java language, not as an additive extension;

(3) The Java string performs runtime check NULL to help troubleshoot some run-time errors;

(4) The string can be connected with "ten".

9 "goto Statement"

The "terrible" goto statement is the C and C + + "relics", which is the legal part of the language, the reference to the GOTO statement caused the confusion of the structure of the program, difficult to understand, the goto sentence to be used for unconditional transfer subroutine and multiple structure branch technology. Given a wide range of reasons, Java does not provide a goto statement, although it specifies goto as a keyword, but does not support its use, making the program simple and readable.

L0. Type conversions

Implicit conversions of data types sometimes occur in C and C 20, which involves automatic coercion of type conversions. For example, in C 20 You can assign a floating-point value to an integer variable and remove its mantissa. Java does not support automatic coercion of type conversions in C 20 and, if necessary, must be explicitly coerced by the program.

11. Abnormal

Exception mechanisms in Java are used to catch exception events and enhance system fault tolerance

try{//code that may produce exceptions

}catch (exceptiontype name) {

Processing

}

Where Exceptiontype represents the exception type. C + + does not have such a convenient mechanism.

_______________ excerpt from the IT Company interview manual

Java is not just a variant of the C + + language, but it is fundamentally different on some essential issues:

(1) Java is higher than C + + program reliability. It has been estimated that there is at least one bug in every 50-line C + + program. Let's not talk about whether this number is exaggerated, but any C + + programmer has to admit that the C + + language provides powerful functionality while also increasing the likelihood of bugs being included in the program. The Java language greatly improves the reliability of the program by changing the characteristics of the language.

(2) The Java language does not require programs to allocate and recycle memory. Java discards those features that are rarely used in C + +, which are difficult to understand, confusing, such as operator overloading, multiple inheritance, and automatic coercion of type conversions. In particular, the Java language does not use pointers and provides automatic scrap collection, EXAMDA hints: In the Java language, memory allocation and recycling are automatic, and programmers do not need to consider the problem of memory fragmentation.

(3) The concept of no pointers in the Java language introduces a true array. Unlike the "pseudo array" implemented in C + + using pointers, Examda,java introduces real arrays and removes easily troublesome pointers from the language, which helps prevent unsafe problems with the illegal reading and writing of system data that is common in C + + programs because of the manipulation of array operations beyond the bounds of such pointers.

(4) Java uses interface (INTERFACE) technology to replace the multiple inheritance of C + + programs. Interfaces have the same functionality as multiple inheritance, but obviate the complexity of implementing and maintaining multiple inheritance.

Java and C + + have their own advantages, no need to argue that the language is good, which language is bad, can exist must have its advantages, as long as you decide to learn programming on a solid, programming language is the same, learn one, the other learn it is very easy.

———————————— excerpted from the Forum of the People

Note: This article is not all original, reference works have been indicated.

Related Article

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.