Sunday, September 25, 2011

java vs c++

Java is different from C++ because

  • They eliminated manual memory allocation and deallocation and added automatic GarbageCollection
  • They eliminated MultipleInheritance, replacing it with a notion of interface derived from ObjectiveCee
  • An object must be dynamically allocated on TheHeap
  • Language support for thread protection with the synchronized keyword
  • Java forces you to deal with exceptions (see JavaExceptionHandling).
  • Write once run anywhere / everywhere
  • Primitive and reference data types always passed by value
  • Automatic garbage collection (can be triggered manually)
  • Supports only class and allocates them on the heap. Java SE 6 optimizes with escape analysis to allocate some objects on the stack.
  • Single inheritance only from classes, multiple from interfaces.
  • Generics are used to achieve an analogous effect to C++ templates, however they do not translate from source code to byte code due to the use of Type Erasure by the compiler.
  • Supports labels with loops and statement blocks.
  • Is compiled into byte code for the JVM. Is dependent on the Java platform but the source code is typically written not to be dependent on operating system specific features.





C++ is different from Java because



  • C++ makes at least an attempt to provide genericity with its "templates", which are, although horribly difficult to use, at least better than casting from "Object" all the time (see UsingTemplates). This has been improved in Java 1.5 with it's generic/parameterized types.
  • On a related vein, C++'s collection classes work at least with all basic types, no need for silly wrappers around "int" as if any half-semi-intelligent compiler couldn't provide them automatically. This has been improved in Java 1.5 with auto-boxing...
  • Write once compile anywhere
  • Pointers, References, and pass by value are supported
  • Explicit memory management
  • Supports class, struct, and union and can allocate them on heap or stack
  • Full multiple inheritance, including virtual inheritance.
  • Compile time Templates
  • Supports the goto statement.
  • Source code can be written to be platform independent (can be compiled for Windows, BSD, Linux, Mac OS X, Solaris etc. without needing modification) and written to take advantage of platform specific features. Is typically compiled into native machine code.
C++ was designed for systems and applications programming, extending the C programming language
To this procedural programming language(C programming language) designed for efficient execution,
C++ has added support for statically-typed object-oriented programming, exception handling, scoped resource management, and generic programming, in particular.
It also added a standard library which includes generic containers and algorithms.

Java relies on a virtual machine to be secure and highly portable.
Java is a statically-typed object-oriented language that uses a syntax similar to C++

C++
Explicit memory management
Java
Automatic garbage collection

Java supports classes, but does not support structures or unions.
All classes in Java ultimately inherit from the Object class. This is significantly different from C++ where it is possible to create inheritance trees that are completely unrelated to one another.
Both C++ and Java support class (static) methods or functions that can be called without the requirement to instantiate an object of the class.
Java does not support operator overloading.

Java, like C++, has primitive types for efficient access.
In Java, these are boolean, char, byte, short, int, long, float, and double.
All the primitive types have specified sizes that are machine independent for portability



References
http://c2.com/cgi/wiki?JavaVsCpp
http://en.wikipedia.org/wiki/Comparison_of_Java_and_C%2B%2B
http://www.dickbaldwin.com/java/Java008.htm
http://www.javacoffeebreak.com/articles/thinkinginjava/comparingc++andjava.html

why there is no pointers in java? (discussion)

Java has references (java.lang.ref.Reference). Anytime you declare an object variable, you are implicitly using a reference to the memory address of that object on the heap. From section 4.3.1 in the JLS: "The reference values (often just references) are pointers to these objects...". According to wikipedia "a pointer is a programming language data type whose value refers directly to (or 'points to') another value stored elsewhere in the computer memory using its address". So Java references are pointers in the general sense, but not in the specific sense of what are called pointers in C/C++.

So now to actually answer the question, "Why are there no pointers?" Assuming you mean pointers in the C/C++ sense, this would be better phrased as "Why can't you reference/manipulate a raw memory address in Java?" And the answer is security: you don't want misbehaving programs (or hackers) being able to manipulate memory addresses directly




Java is not compiled to an platform-specific binary but to bytecode which can only be executed in a JRE (JVM). Each JVM is platform-specific and acts as the intermediary between the Java bytecode and the system, allowing Java to be platform-independent




References
http://www.linkedin.com/groupAnswers?viewQuestionAndAnswers=&discussionID=54050559&gid=70526&commentID=52874399&trk=view_disc&ut=0pse6oSsE1WAU1