0% found this document useful (0 votes)
3 views

01_2_Java_vs_CPP

C++ is a platform-dependent language primarily used for system programming, while Java is platform-independent and focuses on application programming. Key differences include C++ supporting multiple inheritance and operator overloading, whereas Java uses interfaces for multiple inheritance and does not support operator overloading. Additionally, Java has built-in thread support and automatic garbage collection, making it generally more robust than C++.

Uploaded by

ASK 011
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

01_2_Java_vs_CPP

C++ is a platform-dependent language primarily used for system programming, while Java is platform-independent and focuses on application programming. Key differences include C++ supporting multiple inheritance and operator overloading, whereas Java uses interfaces for multiple inheritance and does not support operator overloading. Additionally, Java has built-in thread support and automatic garbage collection, making it generally more robust than C++.

Uploaded by

ASK 011
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

C++ vs Java

C++ Java

platform-dependent. platform-independent.

used for system programming. used for application programming.

C++ uses compiler only. Source code(.java) converts to bytecode(.class) at


compilation time,
converts source code into
machine code The interpreter executes bytecode at runtime and
produces output.

C++ only uses a compiler Java uses both a compiler and interpreter

supports multiple inheritance. doesn't support multiple inheritance through class.


It can be achieved by using interfaces in java

supports operator overloading not

supports pointers not

supports goto not

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 structures and not


unions.

doesn't have built-in support. Java has built-in thread support.


Depends on 3rd party support.

not Java supports documentation comment (/** ... */)

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.

C++ is an object-oriented Java is also an object-oriented language. However,


language. However, in the C everything (except fundamental types) is an object
language, a single root hierarchy in Java. It is a single root hierarchy as everything
is not possible. gets derived from java.lang.Object.

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)

File: main.cpp File: Simple.java

#include <iostream> class Simple


using namespace std; {
int main() public static void main(String arg
{ s[])
cout << "Hello C++ Programming"; {
System.out.println("Hello Java");
return 0; }
} }

 Java does not support pointer. It supports Restricted pointers.


 Java references (Restricted pointers) can't be arithmatically modified.
 C++ is nearer to hardware than Java.
 Java (Single root hierarchy as everything gets derived from java.lang.Object).
 Java doesn't have pointers, templates, unions, operator overloading, structures,
etc.
 Java also does not support conditional compilation (#ifdef/#ifndef type).
 Internet support is built into Java, but not in C++.
 Java does not support default arguments
 There is no scope resolution operator :: in Java.
 It has . using which we can qualify classes with the namespace they came from.
 There is no goto statement in Java.
 Java has method overloading, but no operator overloading
 The String class does use the + and += operators to concatenate strings and
String expressions use automatic type conversion,
 Java is pass-by-value.
 Java does not support unsigned integers.

Why java doesn't support c++ copy constructor?


Java does. They're just not called implicitly like they are in C++ .

Firstly, a copy constructor is nothing more than:


public class Blah {
private int foo;
public Blah() { } // public no-args constructor
public Blah(Blah b) { foo = b.foo; } // copy constructor
}
Now C++ will implicitly call the copy constructor with a statement like this

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.

You might also like