Home  >  Article  >  Java  >  What is the difference between java and c++

What is the difference between java and c++

青灯夜游
青灯夜游Original
2021-03-24 15:25:1746385browse

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.

What is the difference between java and c++

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.

{
    int x = 12;
    {
        int x = 96;
    }
}

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:

if(1){
}

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:

label1:
outer-iteration{
        inner-iteration{
    break;(1)
    continue;(2)
    continue label1;(3)
break label1;(4)

}
}
  • (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:

What is the difference between java and c++

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn