HELLO WORLD IN JAVA: HELLO WORLD IN PYTHON

[Pages:6]HELLO WORLD IN PYTHON:

print("Hello world!")

HELLO WORLD IN JAVA:

public class HelloWorld { public static void main(String[] args) { System.out.println("Hello world!"); }

}

1 / 6

DIFFERENCE #1: EVERY JAVA PROGRAM NEEDS TO BE INSIDE A CLASS. (OBJECT-ORIENTED PROGRAMMING!)

1 public class HelloWorld {

2

3

public static void main(String[] args) {

4

System.out.println("Hello world!");

5

}

6

7}

The rst line in the Java program public class HelloWorld de nes the start of a class named HelloWorld The public class must have the same name as the le we are in (HelloWorld.java), otherwise we'll get an error when compiling The program starts executing at the "main" method of the class that you are running de ned using public static void main(String[] args) (more on that later)

2 / 6

DIFFERENCE #2: WHITESPACE DOESN'T MATTER IN JAVA; USE BRACES INSTEAD

1 public class HelloWorld {

2

3

public static void main(String[] args) {

4

System.out.println("Hello world!");

5

}

6

7}

In Python, whitespace was a Big Deal In Java, whitespace is irrelevant to the computer! Use { .. } braces to group things instead (Although using proper indentation is still highly recommended for readability's sake. In Eclipse, you can use Source -> Format to clean up your code indentation.)

Remember, Eclipse is just an IDE

3 / 6

DIFFERENCE #3: NOW, SEMICOLONS ARE A BIG DEAL

1 public class HelloWorld {

2

3

public static void main(String[] args) {

4

System.out.println("Hello world!");

5

}

6

7}

In Java, every single-line statement must end with a semi-colon

4 / 6

DIFFERENCE #4: STRING QUOTATIONS MUST BE DOUBLE QUOTES IN JAVA

1 public class HelloWorld {

2

3

public static void main(String[] args) {

4

System.out.println("Hello world!");

5

}

6

7}

In Python, the single and double quotes could both be used for strings In Java, it must be the double quotes ".." (Try replacing the quotes in the HelloWorld.java program with single quotes to see how much Java complains)

5 / 6

DIFFERENCE #5: PRINT VS. SYSTEM.OUT.PRINTLN

System.out.println is Java's version of Python's print

6 / 6

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download