01_2_Java_vs_CPP
01_2_Java_vs_CPP
C++ Java
platform-dependent. platform-independent.
C++ only uses a compiler Java uses both a compiler and interpreter
C++ supports both call by value Java supports call by value only. There is no call by
and call by reference. reference in java.
C++ supports virtual keyword so Java has no virtual keyword. We can override all
that we can decide whether or non-static methods by default. In other words, non-
not to override a function. static methods are virtual by default.
C++ doesn't support >>> Java supports unsigned right shift >>> operator
operator. that fills zero at the top for the negative numbers.
For positive numbers, it works same like >>
operator.
C++ always creates a new Java always uses a single inheritance tree because
inheritance tree. all classes are the child of the Object class in Java.
The Object class is the root of the inheritance tree
in java.
Syntactical differences
1. There is no final semi-colon at the end of the class definition.
2. Functions are called as methods.
3. main method is a member of class
& has a fixed form
public static void main(String[] args) – argument is an array of String. This array
contains the command-line arguments.
4. main method must be inside some class (there can be more than one main function
-- there can even be one in every class)
Blah b2 = b1;
Cloning/copying in that instance simply makes no sense in Java because all b1 and b2
are references and not value objects like they are in C++. In C++ that statement
makes a copy of the object's state. In Java it simply copies the reference. The object's
state is not copied so implicitly calling the copy constructor makes no sense.
All stand-alone C++ programs require a function named main and can have
numerous other functions.
Java does not have stand alone functions, all functions (called methods) are
members of a class.
All classes in Java ultimately inherit from the Object class, while it is possible to
create inheritance trees that are completely unrelated to one another in C++.
In this sense , Java is a pure Object oriented language, while C++ is a mixture of
Object oriented and structure language.
The interface keyword in Java is used to create the equivalence of an abstract base
class containing only method declarations and constants. No variable data
members or method definitions are allowed(true till Java 8) . C++ does not support
interface concept. Java does not support multiple inheritance. To some extent, the
interface feature provides the desirable features of multiple inheritance to a Java
program without some of the underlying problems.(death of a diamond)
Java is running on a Virtual Machine, which can recollect unused memory to the
operating system, so Java does not destructor. Unlike C++, Java cannot access
pointers to do memory operation directly. This leads to a whole host of subtle and
extremely important differences between Java and C++.
Furthermore, the C++ compiler does not check whether all local variables are
initialized before they are read. It is quite easy to forget initializing a variable in C+
+. The value of the variable is then the random bit pattern that happened to be in
the memory location that the local variable occupies.
Java does not have global functions and global data. Static in Java is just like global
in C++, can be accessed through class name directly, and shared by all instances
of the class. For C++, static data members must be defined out side of class
definition, because they don't belong to any specific instance of the class.
Generally Java is more robust than C++ because:
Object handles (references) are automatically initialized to null.
Handles are checked before accessing, and exceptions are thrown in the event of
problems.
You cannot access an array out of bounds.
Memory leaks are prevented by automatic garbage collection.
While C++ programmer clearly has more flexibility to create high efficient
program, also more chance to encounter error.