What is the difference between java and c++
Difference: Java cannot use a non-Boolean value as a Boolean value in a logical expression, but C can. C introduced an operator overloading mechanism, which Java does not support. C has goto, Java does not have goto. There is multiple inheritance in C and only single inheritance in Java, but Java can implement multiple inheritance through interfaces.
The operating environment of this tutorial: windows7 system, C 17&&java8 version, Dell G3 computer.
The difference between C and Java:
Although Java is based on C, in comparison, Java is a purer object-oriented programming language . In Java, everything is treated as an object.
1. C After creating an object, you need to call the delete method to destroy it after using it; Java has a garbage collection mechanism to monitor all new objects and identify objects that will no longer be referenced. Then free up memory space.
2. Scope (the scope of C, C, and Java is determined by the position of the curly braces):
But the following code is legal in C and C, and the compiler will report the variable in Java Defined. Because C and C will hide variables with larger scopes, but Java does not allow it.
1 2 3 4 5 6 |
|
3. Default values of basic members
When variables are used as members of a class, Java only gives default values to ensure that those member variables of basic types are initialized. But C doesn't have this functionality. For local variables, the C compiler will warn you, and Java will treat it as an error.
4. Java cannot use a non-Boolean value as a Boolean value in a logical expression, but C can.
In C, if the value of an expression is 0, it is false, and if it is not 0, it is true.
For example:
1 2 |
|
5. Shift operator
There is a new "unsigned" right shift operator in Java (>>> ), which uses "zero extension", inserting 0's in the high bits regardless of whether they are positive or negative. This is not found in C and C.
6. C introduces an operator overloading mechanism. Java does not support it, but C# can implement its own overloaded operators.
7. Java allows us to convert any basic data type into other basic data types, except for Boolean types. [Recommended related video tutorials: Java Video Tutorial]
8. Java does not have Sizeof(). In C\C, the sizeof() operator can tell us the number of bytes allocated for a data item, because different data types in C may have different sizes on different machines. But all data types in Java are the same size on all machines. (For example, C's int is 16 bits on a 16-bit machine, 32 bits on a 32-bit machine and above, long is 32 bits on a 32-bit machine and below, and 64 bits on a 64-bit machine)
9, C There is goto, Java does not have goto.
Java provides a similar jump mechanism: tags. Usage label:
1 2 3 4 5 6 7 8 9 10 |
|
(1), break interrupts the internal iteration, and returns to the external iteration
(2), continue moves the execution point back to the start of the inner iteration.
(3), continue label1 terminal internal iteration and external iteration at the same time, go directly to label1; then continue iteration, starting from the external iteration.
(4) break label1 will also interrupt all iterations and return to label1, but will not re-enter the iteration. That is to terminate both iterations completely.
10. The destructor in C provides an opportunity to release the resources occupied by the object before it is deleted, but Java does not provide a "destructor" or similar concept. [Recommended related video tutorials: C Video Tutorial]
In C, the object will definitely be destroyed (if the program has no defects);
In Java, Objects are not always garbage collected. (Garbage collection does not equal "destruction")
11. Name masking
If Java's base class has a method name that has been overloaded multiple times, then in the derived class Redefining the method name does not block any version of it in the base class. If C wants to introduce a new overloaded method in a subclass, it needs to shield the base class method.
12. There is multiple inheritance in C, but there is only single inheritance in Java. Java can implement multiple inheritance through interfaces.
13. Java is semi-interpreted and semi-compiled, and runs slower than C.
14. Java does not have Struct or Union
15. Java’s generics do not allow the use of basic data types.
16. Array
In c, when the array is defined, storage space has been allocated and can be used. (Specify the array size when defining)
In Java, when the array is defined, int arr[] only defines the array variable literally, and the array cannot be used. Only after array new will the array be created, storage space allocated, and available for use. (It is illegal to specify the array size when defining, and specify the array size when creating)
17. C has pointers, but Java does not.
18. Attached is a comparison of the main lengths of C in 32-bit and 64-bit systems:
For more programming-related knowledge, please visit: Programming Video! !
The above is the detailed content of What is the difference between java and c++. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The calculation of C35 is essentially combinatorial mathematics, representing the number of combinations selected from 3 of 5 elements. The calculation formula is C53 = 5! / (3! * 2!), which can be directly calculated by loops to improve efficiency and avoid overflow. In addition, understanding the nature of combinations and mastering efficient calculation methods is crucial to solving many problems in the fields of probability statistics, cryptography, algorithm design, etc.

std::unique removes adjacent duplicate elements in the container and moves them to the end, returning an iterator pointing to the first duplicate element. std::distance calculates the distance between two iterators, that is, the number of elements they point to. These two functions are useful for optimizing code and improving efficiency, but there are also some pitfalls to be paid attention to, such as: std::unique only deals with adjacent duplicate elements. std::distance is less efficient when dealing with non-random access iterators. By mastering these features and best practices, you can fully utilize the power of these two functions.

The release_semaphore function in C is used to release the obtained semaphore so that other threads or processes can access shared resources. It increases the semaphore count by 1, allowing the blocking thread to continue execution.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

C is suitable for system programming and hardware interaction because it provides control capabilities close to hardware and powerful features of object-oriented programming. 1)C Through low-level features such as pointer, memory management and bit operation, efficient system-level operation can be achieved. 2) Hardware interaction is implemented through device drivers, and C can write these drivers to handle communication with hardware devices.

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.
