Java and C++ Transitioning
A simple example public class HelloWorldApp { public static void main(String[] args) { //Display the string. System.out.println("Hello World!"); } } #include void main (int argc, char* argv[]) { cout << “Hello World”); } JAVA C++
Everything is a class in java public class HelloWorldApp { public static void main(String[] args) { //Display the string. System.out.println("Hello World!"); } } #include void main (int argc, char* argv[]) { cout << “Hello World”); } JAVA C++
static declarations public class HelloWorldApp { public static void main(String[] args) { //Display the string. System.out.println("Hello World!"); } } #include void main (int argc, char* argv[]) { cout << “Hello World”); } JAVA C++
static Declarations Used in class specifications Also used in other areas (ambiguity) –Must consider context One instance for ALL objects public class A { int x; static int y; void B(){…} static void C(){…} } A1 = new A(); A2 = new A(); A3 = new A(); 3 x values 1 y value Must declare an instance Of A to call B e.g. A1.B(); Use class to call C e.g. A.C(); System.out.println…
Different I/O procedures public class HelloWorldApp { public static void main(String[] args) { //Display the string. System.out.println("Hello World!"); } } #include void main (int argc, char* argv[]) { cout << “Hello World”); } JAVA C++
Command line parameters public class HelloWorldApp { public static void main(String[] args) { //Display the string. System.out.println("Hello World!"); } } #include void main (int argc, char* argv[]) { cout << “Hello World”); } JAVA C++
No include statements in java public class HelloWorldApp { public static void main(String[] args) { //Display the string. System.out.println("Hello World!"); } } #include void main (int argc, char* argv[]) { cout << “Hello World”); } JAVA C++
File naming public class HelloWorldApp { public static void main(String[] args) { //Display the string. System.out.println("Hello World!"); } } #include void main (int argc, char* argv[]) { cout << “Hello World”); } JAVA C++ HelloWorldApp.java Must be named: Anything.cpp Named ANYTHING:
File Organization JAVA C++ MainFile.java ClassA.javaClassB.java Main routine ClassA ClassB OR MainFile.cpp ClassA.cpp ClassB.java
No explicit pointers public class A { int x,y; A(){x=0;y=0;} public void printit(){…} } public class HelloWorldApp { public static void main(String[] args) { A A1, A2; A1 = new A(); A2 = new A(); A1.printit() } JAVA
Pointers in c++ class A { int x,y; A(){x=0;y=0;} public void printit(){…} } void main() { A* A1, A2; A1 = new A(); A2 = new A(); A1->printit(); } C++
Accessing Libraries C++ –Uses include –Requires compilation and explicit listing of files to be compiled/linked Java –No include/headers –Libraries grouped in packages –List libraries to be used in import –Automatically incorporates files from packages without explicit listing other than import –More notes on packages and import later