Presentation is loading. Please wait.

Presentation is loading. Please wait.

Going from C++ to Java Jayden Navarro.

Similar presentations


Presentation on theme: "Going from C++ to Java Jayden Navarro."— Presentation transcript:

1 Going from C++ to Java Jayden Navarro

2 Key Differences Harder to do easier things Easier to do harder things
Printing to the screen (System.out.println();) Getting user input (BufferedReader or Scanner) Easier to do harder things Creating classes (No header files, prototyping) Compiling and running programs (javac mainfile.java)

3 Biggest General Differences
No header files No namespaces No makefiles No pointers No need to reference other user’s class files (#include sample.h)

4 Biggest General Differences, cont.
Automatic memory management No destructor Everything is contained in a class, including main Filename must be class name

5 Biggest Syntax Differences
cout << “hello\n”; bool string cin >> var; Arrays (covered later) Java: System.out.println(“hello”); boolean String No short equivalent

6 Virtual Machine vs. Machine Specific Executable
.class (bytecode) vs .exe (machine code)

7 How to Compile No Header files! No make file!
Compile the main file and the rest of the files compile .class files are created for every .java file Use the command “javac ‘filename.java’”

8 How to Run Use the command “java ‘filename’”
Don’t use .java at the end of the filename!

9 How to Create Objects ClassName objectName = new ClassName(Param1, Param2, etc.); CANNOT DO: ClassName objectName(Param1, Param2, etc.);

10 Arrays Size is permanent, just like in C++!
Zero based, just like in C++! Can only use one data type per array, just like in C++! By default, arrays are filled with zeros or null values, in C++ they are undefined

11 int[] a = new int[5]; int test = a[3]; a[3] = 54;
size == 5 Indices: 0 to 4 test == 0;

12 int[] b = {34, 4, 7}; int num = b[1];
size == 3 Indices: 0 to 2 num == 4

13 int[] c; c = new int[8]; size == 8 Indices: 0 to 7

14 Note placement of first brackets!
C++: int a[5]; Java: int[] a = new int[5];

15 ArrayLists instead of Vectors
Creating ArrayLists ArrayList<String> IA = new ArrayList<String>(); Inserting data SA.add(“hi”); Accessing data String s = SA.get(INDEX);

16 ArrayLists<Integer> example
Must use ‘Integer’ or ‘Character’, etc. Creating ArrayLists ArrayList<Integer> IA = new ArrayList<Integer>(); Inserting data IA.add(54); Accessing data int num = IA.get(INDEX);

17 How to get user input import java.io.BufferedReader;
import java.io.InputStreamReader; BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); String theString = input.readLine();

18 Hello World: C++ #include <iostream> using namespace std;
int main() { cout << "Hello, world!\n"; }

19 Hello World: Java public class HelloWorld {
public static void main(String[] args) { System.out.println(“Hello World!”); } Note bracket styling differences!

20 Helpful Links Google it! Wikipedia: Java vs. C++ Java API overview
Stack Overflow: Yahoo Answers for Programmers Google it!

21 Thank You!


Download ppt "Going from C++ to Java Jayden Navarro."

Similar presentations


Ads by Google