Please enable JavaScript to use CodeHS

Java For Python Users

This tutorial is a very basic overview of Java for users that have Python experience.

By David Burnham

Welcome to Java! This tutorial is an overview of some basic Java commands for people that have previously coded in Python. It is far from an exhaustive list of differences, but if you are familiar with Python, you should get enough to get up and running.

Java vs Python

At a high level, Python is more of a blend between object-oriented programming and functional programming, while Java is really a pure object-oriented programming language. What does that mean? In Java, all of your code is contained inside a class and executed within methods/functions. Python still has classes and functions, but it can also execute code outside of these structures.

Here is a Python example of “Hello World”. In Python, printing is one line of code that can live by itself in a program.

print("Hello World")
Plain text

While the print statement in Java is still one line of code, in order to execute any code, it needs to be in a method(function), and inside a class. Here is the basic Hello World in Java:

public class MyProgram
{
    public static void main(String[] args)
    {
        System.out.println("Hello World");
    }
}
Plain text

Notice that the print statement uses the System.out.println(); syntax and that line is inside a function called main, which is inside a class called MyProgam. All basic programs use this syntax and Java looks for that main function to start executing code. This tutorial is not going to dive too far into things, but remember to put all your lines of code inside the main function for now.

You will also notice two other differences from Python here. First, each statement ends in a semicolon, ;. Python uses things like the end of a line to know that it has reached the end of a statement, but Java ignores whitespace and relies on a semicolon to know it has reached the end of a statement.

Along similar lines, Java uses curly brackets { } to designate a code block, such as the block for the main function. The equivalent in Python would be indentation.

Take a look at the example below. You should notice that Java can do the same things as Python, but uses a different syntax.

Variables

In Python, you saw different variable types like integer, strings, and floats. The same variable types exist in Java (although you will typically use a double instead of a float). The big difference is that Python could imply a variable type, but in Java you need to declare it explicitly.

For example, a Python declaration may look like this:
Python:

num = 10
str = "Hello"
Plain text

In Java, the same statements would look like this:
Java:

int num =  10;
String str = "Hello";
Plain text

The most common data types in Java are as follows:
int - Integer type that holds whole numbers.
double - Double type holds decimal numbers.
boolean - True/false variable type.
String - Holds string values. Note that this is an uppercase S.
char - Character that holds a single character.

Check out the example below to see some things you can do with variables.

Control Statements

Like nearly all programming languages, control statements such as loops and conditional statements form a key part of the language. Loops and conditional statements work the same way in Java, but they have a slightly different syntax. Most notably different is that Java uses curly braces to denote the coding block versus the indentation that Python uses.

Here is an example of Java and Python:

Java:

if (age >= 35){
    System.out.println("Old enough to be president");
    System.out.println("Don't forget the other requirements!")
}
// After if statement
Plain text

Python:

if age >= 35:        
    print("Old enough to be president")
    print("Don't forget the other requirements!")
# After if statement 
Plain text

Notice how the if statement uses the same conditional statement, but it is in parenthesis. Then you can see the coding block inside the curly brackets. The same is true for loops. Any statement in the loop needs to be inside of curly braces.

The basic for loop works the same as in Python. In Python, you have a loop variable such as i and if you want to loop 5 times, your set up would look like this:

for i in range(5)
Plain text

The same can be done in Java:

for (int i = 0; i < 5; i++)
Plain text

This tells Java to start at 0, loop while i is less than 5, and increment i by one each time.

Take a look at the following example to see if/else if statements and loops in action.

Your Turn to Try

Now it is your turn to try. See if you can use the examples above to write a Java program that initializes two integers and prints them out. Next, print out if the first number is divisible by the second number. (Hint: The modulus operator in Java works exactly the same as Python.)