Java and C + + differences

Source: Internet
Author: User

JAVA and C + + differences

Javaand theC + +are object-oriented languages. In other words, they are capable of implementing object-oriented thinking(package, secondary, polymorphic). And becauseC + +to take care of a lot ofClanguage users, and compatible with theC, making itself only a band-likeClanguage, more or less affects the thoroughness of its object-oriented! JAVAIt is a completely object-oriented language, with a clearer syntax, smaller size, and easier learning. It is based on a deep and detailed study of a variety of programming languages, and it has abandoned the shortcomings of other languages, fundamentally solvingC + +inherent flaws.

There are more similarities between Java and C + + , but the main differences between the two languages make java easier to learn, and the brothers Java EE training , and the programming environment is much simpler.

I cannot completely list the differences here, only the more significant differences are listed:

1. Pointers

The JAVA language makes it impossible for programmers to find pointers to direct access to memory without pointers, and to add automatic memory management capabilities, effectively preventing pointer manipulation errors, such as system crashes caused by wild pointers, in the C + + language. But it is not that JAVA does not have pointers, the virtual machine inside or use a pointer, but outsiders are not allowed to use it. This facilitates The security of Java programs.

2. Multiple Inheritance

C + + supports multiple inheritance, a feature of C + + that allows a class to be derived from a multi-parent class. Although multiple inheritance is powerful, it is complex to use and can cause a lot of trouble, and it is not easy for the compiler to implement it. Java does not support multiple inheritance, but allows a class to inherit multiple interfaces (extends+implement), Implementing C + + The multi-inheritance function avoids 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 variables 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,the structure and union in C + + is removed inJava , eliminating unnecessary hassles.

4. Automatic memory Management

all objects in a Java program are built on the memory stack with the new operator, which resembles the new operator of C + + . 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 ();

Statement Read R=new read (); On The stack structure, you create a Read the instance. Java automates garbage collection operations without the need for a programmer to delete them. and C 20 must be released by the program Shell memory resources, increase the program designer's negative throw. in Java, when an object is not reused, the garbage collector adds a label to it to delete it. useless memory recycling programs in JAVA run in the background in a threaded fashion, using idle time to work.

5. operator Overloading

operator overloading is not supported in Java. Operator overloading is considered 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 function

The preprocessing feature is not supported in Java. A pre-coding phase, known as a pre-processor, is available during the compilation process. The preprocessor provides convenience for developers, but increases the complexity of the Ding 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 Cc 20 adds classes, provides class algorithms, The algorithm is a function connected to the class, c class 20 methods and java c 20 still supports c c Span style= "font-family: ' The song Body '; > 20 developers use functions, resulting in mixed use of functions and methods to make the program more chaotic.

Java has no functions, and as a More Pure object-oriented language than C 20,Java forces developers to include all the routines in the class, in fact, Using methods to implement routines can motivate developers to better organize coding.

8 String

c and c 20 does not support string variables, in c and c 20 use java The string is used with the class object and to achieve, These class objects are java The core of the language, Implementing a string with a class object has several advantages:

(1) The method of building strings and accessing string elements is consistent across the system ;

(2) J3 the string class is defined as part of the Java language, not as an additional extension ;

(3) Java string Execution run-time check-out to help troubleshoot some runtime errors ;

(4) can be used for strings " 10 " the connection operation.

9 "Goto statement

“horrible"of theGotostatement isCand theC + +of the"Relics", which is a legitimate part of the language's technology, referencingGotostatements cause confusion in the structure of the program, difficult to understand,Gotolanguage sentences are used for unconditional transfer subroutines and multi-structure branching techniques. In view of the broad justification,JavaNot availableGotostatement, although it specifiesGotoas a keyword, but does not support its use, making the program concise and easy to read.

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 required, must be explicitly enforced by the program for type conversions.

One. Exceptions

exception mechanisms in JAVA are used to capture exception events and enhance system fault tolerance

try{// code that may produce exceptions

}catch (exceptiontype name) {

processing

}


Java and C + + differences

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.