The main difference between Java & C++ [closed]

Before my exam in the following week on advanced programming , I’ve tried to think of the main differences between Java and C++ .

From my experience with both languages :

  • C++ uses pointers and have memory leaks , where java doesn’t have pointers and there are no memory leaks (although there are logic memory leaks,I think) ..
  • C++ compiles to machine language , when Java compiles to byte code .
  • Java has no virtual , since everything in java is virtual (please correct me if I’m wrong) .
  • In C++ the programmer needs to worry about freeing the allocated memory , where in Java the Garbage Collector (with all its algorithms , I can think of at least 3 algorithms) takes care of the the unneeded / unused variables .

Did I forget anything ? Please feel free to correct me if I’m wrong , I’d appreciate it

Regards,Ron

Difference between & and && in Java? [duplicate]

Possible Duplicates: What’s the difference between | and || in Java? Difference in & and && I was just wondering what the difference between & and && is? A few days I wrote a

What is the difference between main in C & C++

What is the difference between main in a C program and in a C++ program? Other than return statement ( default 1 in C,`0 in C++) syntax: int main() { /* … */ } int main(int argc, char* argv[]) { /*

Difference between init() and main() method in java

I want to know the difference between init and main method of java

Difference between Java SE & Java EE [duplicate]

This question already has an answer here: Difference between Java SE/EE/ME? 7 answers What is the difference between Java SE & Java EE?

What is the main difference in object creation between Java and C++?

I’m preparing for an exam in Java and one of the questions which was on a previous exam was:What is the main difference in object creation between Java and C++? I think I know the basics of object c

In C: Difference between main() and int main () [duplicate]

Possible Duplicate: difference between main(void) and main() in c I know this is super basic and some other threads already talked about similar questions. But I have a book (Absolute Beginners Guid

C main vs Java main

What is the difference between the C main function and the Java main function? int main( int argc, const char* argv[] ) vs public static void main(String [] args) How do these main functions relate

Main Diffirences between c++ and Java [closed]

What are the most important differences between C++ and Java from a point of view of someone coming from Java to C++?

difference between main(void) and main() in c

Can anyone tell me the difference between int main() and int main(void). Why both of them work and what is the default argument to int main().

Difference between static void main(string[] args) and static void main() in c#?

can u pls anyone tel me, Difference between static void main(string[] args) and static void main() in c#?

Answers

One of the main difference for me: Destructors in C++ and the use of the RAII idiom

Java Enums are objects instead of int values in C++.

C++ mutliple inheritance vs single in java.

  1. C++ supports pointers whereas Java does not pointers. But when many programmers questioned how you can work without pointers, the promoters began saying “Restricted pointers.” So we can say java supports Restricted pointers.

  2. At compilation time Java Source code converts into byte code .The interpreter execute this byte code at run time and gives output. Java is interpreted for the most part and hence platform independent (Write once, run anywhere). C++ run and compile using compiler which converts source code into machine level languages so c++ is platform dependent (Write once, compile anywhere).

  3. Java is platform independent language but c++ is dependent upon operating system machine etc. C++ source can be platform independent (and can work on a lot more, especially embedded, platforms), although the generated objects are generally platform dependent but there is clang for llvm which doesn’t have this restriction.

  4. Java uses both a compiler and interpreter, while C++ only uses a compiler

  5. C++ supports operator overloading multiple inheritance but java does not.

  6. C++ is nearer to hardware then Java

  7. Everything (except fundamental types) is an object in Java (Single root hierarchy as everything gets derived from java.lang.Object).

  8. Java does is similar to C++ but it doesn’t have the complicated aspects of C++, such as pointers, templates, unions, operator overloading, structures, etc. Java also does not support conditional compilation (#ifdef/#ifndef type).

  9. Thread support is built into Java but not in C++. C++11, the most recent iteration of the C++ programming language, does have Thread support though.

  10. Internet support is built into Java, but not in C++. On the other hand, C++ has support for socket programming which can be used.

  11. Java does not support header files and library files. Java uses import to include different classes and methods.

  12. Java does not support default arguments.

  13. There is no scope resolution operator :: in Java. It has . using which we can qualify classes with the namespace they came from.

  14. There is no goto statement in Java.

  15. Because of the lack of destructors in Java, exception and auto garbage collector handling is different than C++.

  16. Java has method overloading, but no operator overloading unlike C++.

  17. The String class does use the + and += operators to concatenate strings and String expressions use automatic type conversion,

  18. Java is pass-by-value.

  19. Java does not support unsigned integers.

Java does have a widely used idea of a memory leak, but it doesn’t mean the same thing as it does in C++. In Java a “memory leak” is any unwanted growth in memory usage. In C++ it means memory which can never be re-used (which never happens in Java)

In C++ all the stages of compilation occur in the C++ compiler which is run before the application starts. The CPU will still perform pre-fetching, decoding and branch prediction so there is some dynamic optimisation of code.

In Java most of the optimising compilation is performed by the JIT at runtime and its output is native code.

I Java everything is notionally virtual, except static methods. “virtual” method which have only one implementation do not need to be called virtually (via a lookup table) and Java can inline up to two “virtual” methods even if they come from other libraries. i.e. “virtual” methods many not result in a virtual call.

C++ has shared pointers (standard in C++v11) and allocation of objects on the stack, which can handle automatic memory de-allocation in many cases. For objects which cannot be either, you have to manage these yourself. As shared pointers use reference counts, they do not handle circular references for you.

Java has references which are managed by a garbage collector. There is no simple way to explicitly free an object or allocate objects on the stack. Escape analysis can eliminate the need to allocate some objects, by placing their fields on the stack (but the Sun/Oracle JVM doesn’t do this in many simple cases)

Java 7 now has ARM (Automatic Resource Management) which can be used for closing resources, but not freeing objects.

Java is described as platform independent. What this means is its independent of the OS, but has to run on a JVM (which is also described as a platform)

C++ is platform dependant, but with some effort, can be compiled for more systems than Java currently supports.

Java has a long list of libraries which are built in. C++ has boost which provides much of this functionality to some degree. Some of the boost libraries are more mature than others. 😉

Java has the goto and const keyword, you just can’t use it anywhere in the syntax. You can still perform a “goto” with break and continue e.g. you can use a break without a loop.

Java doesn’t have destructors, but it has finalize()rs The main difference is that you have not idea when, if ever, a finalizer will be called. It is also called in a background thread.

Java’s only unsigned type is char which is 16-bit. In C++ its 8-bit. The width of the primitive types is always the same in Java. In C++, there are macros/typedef you can use to get fixed width primitive types.

  1. Wrong. Java can have memory leaks as well, essentially in the form of objects registered to some framework classes which don’t tell about these object but also don’t end up using them. Worse, yet, Java can have resource leaks which are generally cleaned up by C++’s destructors.
  2. Wrong. C++ can compile to a byte code (see e.g. clang/llvm) and Java can be compiled to machine code (see e.g. gcc).
  3. The defaults for virtual functions are different in Java and C++: in Java members are virtual by default and need to be made final if this isn’t desired. In C++ members are non-virtual by default and need to be virtual if this is desired.
  4. Wrong. See 1.

Multiple inheritance is the ability to inherit from multiple classes. Java does not have this capability.However, C++ does support multiple inheritance.

What a Java class does have is the ability to implement multiple interfaces – which is considered a reasonable substitute for multiple inheritance.

In C++ parameters to functions are either passed by value or passed by reference or passed by address but in Java parameters are always passed by value.

As a major difference I’d add portability.

Java is intrinsecally “write once run everywhere“, while most C++ programs (especially mature software that was not born with cross-platformness in mind) can be worksome to port to other platforms.

And in any case, you have to compile C++ for a specific target platform, while Java compiled bytecode (be it classes, jars, wars, etc) is ready to go on any OS and bitness.

  1. Java program can inspect java classes during runtime, for example it can print out the members of a class, it can dynamically load a class, dynamically instantiate one or even call one of its member functions. To know more, REFLECTION is the thing I’m talking about.
  2. Its purely object oriented, you can no longer write a function that takes parameters of type “function/method” because such an entity does not exist on its own, this means you cannot pass a callback “as such” to other functions, you need to wrap them in a class that implements a pre-defined interface and then you need to pass the object of that class as a callback.
  3. There is only “pass by value”!

One of the main defference is Java does not support multiple inheritance. 🙂