Object Oriented Programming and C++: C++ Compared To Java
Object Oriented Programming and C++: C++ Compared To Java
2009
Introduction
C++ Compared to Java Roger B. Dannenberg, Ph.D. Associate Research Professor School of Computer Science Carnegie Mellon University
Overview
History of C and C++ Differences between C++ and Java Why Use C++? Plans for the course
History of C
Originally, compilers and operating systems were written in assembler Several efforts to get away from machine code for systems programming Dennis Ritchie wrote C for PDP-11, 1972 Closely connected with creation of Unix C code is
Fast Low-level (access to addresses, bits, raw bytes) Fairly portable -- driven by efforts to port Unix
History of C++
As C was being standardized, Object oriented programming was taking hold Need for systems programming language supporting OOP Several efforts based on macros, precompilers, etc. Bjarne Stroustrup led an effort resulting in C++ C++ became immensely popular Standardization efforts followed
Java
James Gosling and group at Sun Microsystems Came after C++ Adopts much of C++ syntax Aims to simplify C++ Less language evolution, so Java features are more consistent with one another Emphasis on avoiding programming errors and avoiding unsafe programs
2009, Roger B. Dannenberg 6
C++ must be compiled for each OS and ISA (e.g. Linux/PPC, Linux/i386, Win32/i386)
2009, Roger B. Dannenberg 8
Multithreading
A thread is essentially
A runtime stack A (virtual) processor with program counter, registers, condition codes Multiple threads can share memory
C++ programs obtain threads by systemdependent APIs (thread_create()) Java has threads built-in
But Java threads have some problems Semantics unclear, especially with modern multi-core processors
2009, Roger B. Dannenberg 10
APIs
Java is huge
Graphical user interface library Networking Database connectivity
11
Templates
Programming where types can be parameters Useful for generic algorithms/classes
Linked list of T
Compiler generates code as needed Can provide better type checking than Java, although now Java has generics Can provide faster code than Java Warning: Templates can be painful!
2009, Roger B. Dannenberg 14
Operator Overloading
Operators such as + and * can be redefined Is this a good thing? Probably good sometimes
15
Of course, Java has a large library too We will discuss some differences later
16
Generally more work to manage memory, but More explicit management sometimes is an advantage, e.g. real-time systems
2009, Roger B. Dannenberg 17
Conditional Compilation
Compile-time values can enable/disable code Debugging vs Production versions of code Portability issues: compile different code depending on available API and OS Compile for different hardware variations Compile demo version
18
Space Efficiency
C++ gives more control over memory use Data do not necessarily have extra fields for:
Run-time type information Garbage collection Length counts for arrays
19
Private Inheritance
Java supports only public inheritance C++ gives finer control over visibility of methods Programmer can express allowed usage and better express abstractions
20
Bad interactions:
Must follow the rules but no automatic checks
2009, Roger B. Dannenberg 21
22
name
23
Memory Models
C++ Stack Frame Java Stack Frame
Obj1 Obj1 Obj2 Obj3 Obj3 Obj2
24
Therefore,
Java objects are heap-allocated Almost no distinction between pointers and objects
C++ is different!
Pointers are rampant and highly visible Implications for language design and programmers
2009, Roger B. Dannenberg 25
C++ Misgivings
C++ is not a complete success Adherence to design philosophy led to serious problems Few people fully understand C++ (I dont) I believe that if understanding or debugging a C++ program requires detailed knowledge of C++, its best to change the program Many features of C++ should be avoided Nevertheless, Ive written 1000s of LOC in C++
2009, Roger B. Dannenberg 26
Summary
C++ came before Java Widely used, important to know More emphasis on efficiency More machine-based memory model
28