Java Tutorial for Python Programmers

Java Statements and Printing

Statements

Java statements (non-compound statements, anyways) all end in a semicolon. Python uses whitespace (line breaks) as a delimiter between statements, but Java doesn't, so semicolons are necessary. If I wanted to assign the variable simian to the string value "monkey", that statement looks exactly the same as it does in Python, except with the semicolon added:

        simian = "monkey";
        

Printing

In Java, a print statement looks like the following:

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

This tells Java to print a string to the standard output: System.out. Let's see what happens when we run this in the terminal:

$ javac *.java
$ java CodeToRun
Hello, World!

Change the print statement to concatenate two strings:

        System.out.println("Hello, World!" + "  I like bananas!");
        

Run it again:

$ javac *.java
$ java CodeToRun
Hello, World!  I like bananas!

Test Your Understanding

Update your Monkey class so that it prints Sweet Monkeys! when you run it.