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 | |
cout << b.x << endl; // outputs 0, because b is a // different object than a | System.out.println(b.x); // outputs 0, because b points to a // different object than a |
Foo *c; // declares c to be a pointer to a // Foo object (initially // undefined; could point anywhere) | Foo c; // declares c to be a reference to a Foo // object (initially null if c is a class member; // it is necessary to initialize c before use // if it is a local variable) |
c = new Foo; // binds c to reference a new Foo object | c = new Foo(); // binds c to reference a new Foo object |
Foo *d = c; // binds d to reference the same object as c | Foo d = c; // binds d to reference the same object as c |
c->x = 5; // modifies the object referenced by c | c.x = 5; // modifies the object referenced by c |
a.bar(5); // invokes Foo::bar() for a c->bar(5); // invokes Foo::bar() for *c | a.bar(5); // invokes Foo.bar() for a c.bar(5); // invokes Foo.bar() for c |
cout << d->x << endl; // outputs 5, because d references the // same object as c | System.out.println(d.x); // outputs 5, because // d references // the same object as c |
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.