What is diffrence between c++ and java?
- C++ supports pointers whereas Java does not.
- C++ supports operator overloading,multiple inheritance but java does not.
- Java is platform independent language but c++ is depends upon operating system, machine etc.
- Java uses compiler and interpreter both and in c++ their is only compiler.
- Java has premitive data type like boolean which are not available in c++.
- C++ does not allow persistence because it does not support database connection while Java allows persistence connection because it supports database connection.
C
++ is optional automated bounds checking.(e.g. the at () method in vector & string container) but Java is normally performs bounds checking. HotSpot can remove bounds checking.
c++ supports multiple inheritance but Java provides interfaces in case of multiple inheritance.
- C++ standard libraries provide containers and associative arrays but Java libraries is inside the so-called Java Collections Framework.
- C++ is normally compiled directly to machine code which is then executed directly by the operating system. Java is normally compiled to byte-code which the Java virtual machine (JVM) then either interprets or JIT compiles to machine code and then executes.
Syntax between c++ and java:-
C++ | Java |
---|---|
class Foo { // Declares class Foo public: int x; // Member variable Foo(): x(0) {} // Constructor for Foo, initializes x int bar(int i) { // Member function bar() return 3*i + x; } }; | class Foo { // Defines class Foo public int x; // Member variable, //initialized to 0 by default public Foo() { // Constructor for Foo } public int bar(int i) {// Member method bar() return 3*i + x; } } |
Foo a; // declares a to be a Foo object value, // initialized using the default constructor. // Another constructor can be used as "Foo a(args);" | Foo a; // declares a to be a reference to a Foo object a = new Foo(); // initializes using the default constructor // Another constructor can be used as "Foo a = new Foo(args);" |
Foo b = a; // copies the contents of a to a new Foo object b; // alternative syntax is "Foo b(a)" | Foo b = a.clone(); // copies the values of all members // of this instance if, and only if, // Foo implements a public method called // clone() which returns a new copy of the object |
a.x = 5; // modifies the object a | a.x = 5; // modifies the object a |