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

C++ vs. Java: Similiarities & Differences: Dr. Jeyakesavan Veerasamy

C++ and Java are both object-oriented programming languages that share similarities but also have key differences. Both support object-oriented programming concepts, but C++ allows for manual memory management using pointers and references while Java uses automatic garbage collection. C++ applications compile to native executables for each target system, while Java uses bytecode that runs on any system with a Java Virtual Machine. The document provides examples of how objects, arrays, and memory are handled differently between the two languages.

Uploaded by

Josh Ch
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

C++ vs. Java: Similiarities & Differences: Dr. Jeyakesavan Veerasamy

C++ and Java are both object-oriented programming languages that share similarities but also have key differences. Both support object-oriented programming concepts, but C++ allows for manual memory management using pointers and references while Java uses automatic garbage collection. C++ applications compile to native executables for each target system, while Java uses bytecode that runs on any system with a Java Virtual Machine. The document provides examples of how objects, arrays, and memory are handled differently between the two languages.

Uploaded by

Josh Ch
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

C++ vs.

Java:
Similiarities & Differences
Dr. Jeyakesavan Veerasamy
Director of CS UTDesign program &
CS Teaching Faculty
[email protected]
University of Texas at Dallas, USA

History
C (1969) C++ (1979) Java
(1995)
Both support OOP. Most OOP library
contents are similar, however Java
continues to grow.
Syntax is very close Java has strong
influence of C/C++. Easy to learn the
other language when you know one
of these.

C++ compiler & Linker


usage
file1.cpp

file2.cpp

Compil
er

Compil
er

file1.o

file2.o

filen.cpp
Compil
er

filen.o

Linker

This
appliction
runs directly
on top of OS.

application
(executable)

C++ compiler
does not care
about filenames.

Java compiler usage


file1.java

file2.java

Compil
er

Compil
er

Compil
er

file1.class

file2.class

filen.class

filen.java

Java Runtime Environment (JRE)


Operating System

C++ vs. Java: differences


C++

Java

Write once, compile


everywhere
unique executable
for each target
No strict relationship
between class names
and filenames.
Typically, a header
file and
implementation file
are used for each
class.

Write once, run


anywhere same class
files will run above all
target-specific JREs.
Strict relationship is
enforced, e.g. source
code for class PayRoll
has to be in PayRoll.java

C++ vs. Java: differences


C++
I/O statements use
cin and cout, e.g.
cin >> x;
cout << y;

Java

I/O input mechanism is


bit more complex, since
default mechanism
reads one byte at a time
(System.in). Output is
easy, e.g.
System.out.println(x);
Pointers, References, Primitive data types
and pass by value
always passed by value.
are supported. No
Objects are passed by
array bound
reference. Array bounds
checking.
are always checked.

C++ vs. Java: differences


C++
Explicit memory
management.
Supports destructors.
Supports operator
overloading.

Java
Automatic Garbage
Collection.
Specifically operator
overloading was thrown
out.

Types of memory used by


executable task
data
(static)
heap
(dynamic)

Code

Stack

Objects

Objects can be created as local


variables just like any basic data types
in C++.
C++:
ComplexType num1;
Java: Nothing equivalent Objects
cannot be in stack.

Objects in Heap
C++:
ComplexType *num1 = new
ComplexType();
Java:
ComplexType num1 = new
ComplexType();

Arrays

Basic data types and classes are


treated the same way in C++, unlike
Java.
C++: ComplexNumber numbers[5];
Java: nothing equivalent.

C++ array version #2


ComplexNumber *numbers;
numbers = new ComplexNumber[5];
Java: nothing equivalent for classes,
but possible for basic data types:
int numbers[];
numbers = new int[5];

C++ array version #3


ComplexNumber **numbers;
numbers = new ComplexNumber*[5];
for( index i = 0 ; i < 5 ; i++)
numbers[i] = new ComplexNumber();
Java:
ComplexNumber numbers[];
numbers = new ComplexNumber [5];
for( index i = 0 ; i < 5 ; i++)
numbers[i] = new ComplexNumber();

C++ vs. Java: Analogy


Working with C++ is like flying a airpline,
while working with Java is like driving a car.
What does it mean?
Too many controls/options in C++: think
before use each one.
Careful use can result in efficiency, poor use
can result in serious inefficiency
Issues like memory leak can crash the
application, out-of-bounds array access can
cause memory corruption but it may not
show up for long time causing lot of
headache!
Java : slow and steady wins the race?

References
C++ tutorials:
http://www.cplusplus.com/files/tutorial.pd
f, http://www.learncpp.com/
C++ reference:
http://en.cppreference.com/w/
Java tutorial:
http://docs.oracle.com/javase/tutorial/
Java API documentation:
http://docs.oracle.com/javase/6/docs/api/

You might also like