Java vs C++ 1 1 Department of Computer Science Poznan University of Technology 2012.10.07 / OOP Laboratory
Outline 1 2 3
Outline 1 2 3
Outline 1 2 3
Tabular comparizon C++ Java Paradigm Procedural/Object-oriented Object-oriented is highly recommended Compilation/Running Compiled to machine code that can run on specific system Compiled to byte code that can be run on system with JVM Standard libraries Limited to the very basic functionality (e.g. Grows with each release STL supports regular expression only since C++11) Multiple inheritance Full support, including virtual inheritance One can achieve similar result using interfaces Run-time object information Only type names are available Full meta-information support, including dynamic code invocation Type instances passing by value/by reference/by pointer instances of primitive types by value, rest by reference (that can be perceived as pointers) Memory management explicit one has to force system to free disposing memory is managed by, so memory called, garbage collector Overloading operators most of operators can be overloaded does not allow to overload operators Logic program hierarchy namespaces package = path to the source file, filename = inner type name Inline abstract classes/interfaces not possible allowed imple- mentation
Hello world in Java Java source code 1 package hw; 2 3 import java.lang.system; 4 5 public class Hello { 6 public static void main(string args[]) { 7 System.out.println("Hello world"); 8 } 9 } C++ source code 1 #include <iostream> 2 3 using std::cout; 4 using std::endl; 5 6 int main(int argc, char **argv) { 7 cout << "Hello world" << endl; 8 } Compiling 1 javac path/to/file/filename.java Running 1 java path.to.file.filename
Creating jar file Compiled classes by javac are stored as files with extension class in a file system. While application grows there are plenty of such class files. To make the application transferable one have to use some other mechanisms that merge the number of files in a single executable application. To handle this problem Java applications make use of jar files. Command creating jar 1 jar cfe path/to/jarfile/jarfilename.jar path.to.mainclass.mainclass classfile[,...] Running jar 1 java -jar jarfile.jar
Tasks Complete the tasks, answer questions 1 Create hello world application in Java and create executable executable jar file.
A few guidelines Access modifier (without colon) before every class member declaration Inheritance of classes using extends keyword Implementing interface using implements keyword To declare constant values use final keyword To use some class from outside of the package use import keyword Virtual methods don t have to be additionally specified every method is virtual and can be overridden To declare abstract method use abstract keyword Use dot to access members of the object instance Use dot to access the package elements Use dot to access a static class members
Example car/car.java 1 package cars; 2 3 import vehicles.vehicle; 4 import vehicles.istartenginable; 5 6 public class Car extends Vehicle implements IStartEnginable { 7 public int cylinders = 10; 8 9 public void startengine() { 10 System.out.print("B"); 11 for (int i = 0; i < this.cylinders; i++) { 12 System.out.print("R"); 13 } 14 System.out.println(); 15 } 16 17 public static void main(string[] args) { 18 Car car = new Car(); 19 car.startengine(); 20 } 21 }
Tasks Complete the tasks, answer questions 1 Use a class hierarchy from the first presentation (car.h/cpp, volkswagen.h/cpp, hyundai.h/cpp, main2.cpp) and rewrite its functionality to Java language. Remember to create a jar file.