C vs Java Similiarities Differences Dr Jeyakesavan Veerasamy

  • Slides: 15
Download presentation
C++ vs. Java: Similiarities & Differences Dr. Jeyakesavan Veerasamy Director of CS UTDesign program

C++ vs. Java: Similiarities & Differences Dr. Jeyakesavan Veerasamy Director of CS UTDesign program & CS Teaching Faculty jeyv@utdallas. edu University of Texas at Dallas, USA

History • C (1969) C++ (1979) Java (1995) • Both support OOP. Most OOP

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 file 1. cpp file 2. cpp Compiler file 1.

C++ compiler & Linker usage file 1. cpp file 2. cpp Compiler file 1. o file 2. o …. filen. cpp Compiler …. filen. o Linker This appliction runs directly on top of OS. application (executable) C++ compiler does not care about filenames.

Java compiler usage file 1. java file 2. java Compiler file 1. class file

Java compiler usage file 1. java file 2. java Compiler file 1. class file 2. class filen. class …. Java Runtime Environment (JRE) Operating System filen. java

C++ vs. Java: differences C++ Java Write once, compile everywhere unique executable for each

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 Pay. Roll has to be in Pay. Roll. java

C++ vs. Java: differences … C++ I/O statements use cin and cout, e. g.

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, and Primitive data types always pass by value are passed by value. Objects are supported. No array passed by reference. Array bound checking. bounds are always checked.

C++ vs. Java: differences … C++ Explicit memory management. Supports destructors. Supports operator overloading.

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

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

Objects • Objects can be created as local variables just like any basic data types in C++: Complex. Type num 1; Java: Nothing equivalent – Objects cannot be in stack.

Objects in Heap C++: Complex. Type *num 1 = new Complex. Type(…); Java: Complex.

Objects in Heap C++: Complex. Type *num 1 = new Complex. Type(…); Java: Complex. Type num 1 = new Complex. Type(…);

Arrays • Basic data types and classes are treated the same way in C++,

Arrays • Basic data types and classes are treated the same way in C++, unlike Java. C++: Complex. Number numbers[5]; Java: nothing equivalent.

C++ array version #2 Complex. Number *numbers; numbers = new Complex. Number[5]; Java: nothing

C++ array version #2 Complex. Number *numbers; numbers = new Complex. Number[5]; Java: nothing equivalent for classes, but possible for basic data types: int numbers[]; numbers = new int[5];

C++ array version #3 Complex. Number **numbers; numbers = new Complex. Number*[5]; for( index

C++ array version #3 Complex. Number **numbers; numbers = new Complex. Number*[5]; for( index i = 0 ; i < 5 ; i++) numbers[i] = new Complex. Number(…); Java: Complex. Number numbers[]; numbers = new Complex. Number [5]; for( index i = 0 ; i < 5 ; i++) numbers[i] = new Complex. Number(…);

C++ vs. Java: Analogy • Working with C++ is like flying a airpline, while

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. cplus. com/files/tutorial. pdf, http: //www. learncpp. com/ •

References • C++ tutorials: http: //www. cplus. com/files/tutorial. pdf, 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/