Compare C++ Syntax and Features with Java
Here is a brief summary that compares C++ syntax and features with Java
C++
Java
cout << "something" << endl;
int x;
cin >> x;
System.out.println("something");
Scanner input = new Scanner(System.in);
int x = input.nextInt();
main function (not in a class, return int)
main method in a class (void)
namespace
using namespace
pacakge
import a package
short, int, long, unsigned short, unsigned
int, unsigned long, float, double, long
double, char, bool
byte, short, int, long, float, double, char,
boolean
Casting: (int)x or static_cast<int>x
operand evaluation order a + b
(int)x
a + b (strictly from left to right)
||, &&, !
if, switch, conditional operator
||, &&, !, ^, |, &
if, switch, conditional operator
for, while, do-while
break, continue
for, while, do-while
break, continue
function
Pass-by-value
Pass-by-reference for any type
overloading
Ambiguous overloading
Global variables
Static local variables
method
Pass-by-value
No pass-by-reference
overloading
Ambiguous overloading
int counts[4]
counts = counts1; (not allowed)
int[] counts, int counts[]
new int[4];
Arrays are objects
counts = counts1; (OK)
Static allocation and heap allocation (new)
Bound checking (no)
Bound checking (checked at runtime)
Cannot get the length from the array
f(counts, SIZE)
counts.length returns the length of an array
f(counts)
pointers
C-string
Circle class
Create an object Circle c; // Create an
object, c is the name of the object. The
object is created using the no-arg
constructor.
Circle c(5.5);
c.getArea();
Circle class
Circle c; // Declare a reference variable, c
is just a reference to the object
Circle* p = new Circle(); // Dynamic
creation
p->getArea(); or (*p).getArea()
delete p;
Circle c = new Circle(); // All objects in
Java are created dynamically
c.getArea();
c = null; Automatically removed
Pass-by-value function f(Circle c)
Invoke f(c1)
Pass-by-reference function(Circle& c)
Invoke f(c1)
Pass-by-reference via pointer
function(Circle* p)
Pass-by-value in Java
method
m(Circle c)
invoke f(&c1)
The string class
static members (data fields and functions)
Header files
Define class in a .h file and implement it in
a .cpp file
Include header file
inclusion guard (to avoid multiple
inclusion) ifndef then define,
invoke m(c1)
The String class
static members (data fields and methods)
Define class and implement all methods in
the same class .java file
Default constructor: same
Default constructor: same
Copy constructor
Circle c1; Circle c2(5.5);
c1 = c2; // Still two different objects
clone() method
Circle c1 = new Circle(); Circle c2 = new
Circle(5.5);
c1 = c2; // c1 and c2 refer to the same
Anonymous object
Circle().getArea();
destructor
dynamic_cast<type*>(pointer)
Constructor chaining: same
Destructor chaining
The functions invoked from a constructor
in C++ are not polymorphic, i.e., the
functions are statically bound.
Immutable objects: (memberwise copy)
object
new Circle().getArea()
The Object class has the finalize() method.
(type)objectReference
Constructor chaining: same
Invoking finalize() methods from the
current class on to the Object class
The methods invoked from a constructor in
Java are polymorphic, i.e., the methods are
dynamically bound.
Immutable objects: String, Integer,
String s1;
String s2;
s1 = s2;
Inheritance (Derived class, base class)
Constructor chaining
class A : public B
{
Inheritance (subclass, superclass)
Constructor chaining
class A extends B
{
};
private, protected for the members of the
class
public class A (no such thing in C++)
Encapsulation, inheritance, and
polymorphism
Redefine functions
}
private, protected for the members of the
class
public class,
Encapsulation, inheritance, and
polymorphism
No redefine functions and only override
functions
override
Abstract class
public abstract class A {
public abstract void m();
}
Override (virtual function)
Abstract class
class A
{
public:
virtual void m() = 0;
}
No interface
Multiple inheritance
class A : public B, public C
{
}
interface
No multiple inheritance in Java
(wrong)
public class A extends B, C {
}
Text I/O
Text I/O
ifstream
Scanner
ofstream
Binary I/O
PrintWriter
Binary I/O
istream opened with binary mode
reinterpret_cast
InputStream
OutputStream
FileInputStream
FileOutputStream
BufferedInputStream
BufferedOutputStream
ObjectInputStream
ObjectOutputStream
Operator overloading
s[0]
cout << s1
cin >> s1
s1 + s2
s1 < s2
Not in Java
Exception handling
Exception
try
{
try
{
}
catch (type e)
{
}
catch (ObjectType e)
{
}
finally
{
}
finally
{
}
throw an object of Throwable instance
throw an object or primitive type value
Templates
Generics
You can substitute a generic type with a
primitive type or object type
You can substitute a generic type with only
object type
STL
Collections framework
list, vector, deque
Collection, List, Set, Map
set, multiset, map, multimap
HashSet, LinkedHashSet, TreeSet,
LinkedList, ArrayList,
HashMap, LinkedHashMap, TreeMap
Stack, Queue, Priority_Queue
queue, stack, priority_queue
STL algorithms
Uses iterators extensively
foreach loop in C++11 (Visual C++ 2012)
for (type& e: collection)
{
}
auto type
No counterpart of algorithms and
algorithms are built in the API
foreach loop in Java
for (type e: collection)
{
}
No auto type