Python and Java are two of the most widely used programming languages in the world today. While Python has been relatively easier to learn, many enterprise-level organizations still prefer Java owing to its speed and efficiency. Python on the other hand has been commonly used for machine learning and data science. 

However, with the availability of high-performance computing hardware, Python is catching up and many tech giants like Google, Facebook, Netflix are using Python as its preferred programming language. To stay ahead in the job market, it is the right time for Java developers to learn Python. This article answers the question of how Java developers can switch to Python. The article focuses on the technical differences between the two languages and what you need to unlearn and learn in order to switch from Java to Python.

Table of Contents

  1. Indentation
  2. Use of Semicolon
  3. Interpreted Language
  4. No Compile Time Errors
  5. Dynamic Data Typing
  6. Functions as First Class Citizens
  7. Returning Multiple Values from Functions

How to Switch to Python for Java Developers

Indentation

The biggest visible difference between Java and Python lies in code indentation. In Java, code blocks are defined via curly brackets. On the other hand in Python, indentations define code blocks. Let’s look at an example.

The following defines an outer and an inner block for an if statement in Java. You can see that the code blocks are defined via curly brackets.

      if(10 > 5)
        {
        System.out.println("10 is greater than 5");
        if(10 > 12){
        System.out.println("10 is greater than 12"); 
        }    
        }

With Python, to start a new block,  you have to go to a new line and then indent the code one tab from the left. You do not need curly or round brackets to start a new code block.
Here is the same script that you saw in Java, written in Python:

if 10 > 5:
    print("10 is greater than 5")
    #new if statement is indented
    if 10 > 12:
        print("10 is greater than 12")

Use of Semicolon

If you have programmed in Java, C++, or C# all your life, you would explicitly expect to end every line of code with a semicolon. However, Python takes an exception here as well. With Python, you don’t need a semicolon to end a statement. Every new statement is simply written on a new line. To write a multi-line statement in Python, you just have to add a forward slash “/” at the end of a line.

Interpreted Language

Java is a compiled language where the code is first converted into Java Bytecode which is converted into machine instructions by the Java compile time. On the other hand, Python is an interpreted language where code is not first compiled. Rather, each line of code is interpreted and executed line by line on the fly.

No Compile Time Errors

Since Java is a compiled language, most of the errors, particularly the ones related to syntax and data typing are caught at runtime. For instance, if you forget to add a bracket, a semicolon, or wrongly assign a value to a variable of a different data type, the compiler will throw an error. On the contrary, since Python is not a compiled language, hence there is no possibility of compile-time errors. Though this feature provides flexibility, it can also lead to increased chances of errors at runtime.

Dynamic Data Typing

In Java, you used to specify data type during variable declaration. A variable declared as an integer type cannot store a string. Here is an example:

int num = 10; // integer 
num = "Hello"; // this line will throw an error

In the above example, you define a variable num of type integer in Java. If you try to store the string “Hello” in the num variable, the code will not compile because num is an integer type variable that cannot store a string.  

On the contrary, Python is a dynamically typed language where you don’t have to specify a data type. You can also store a string in a variable that previously-stored an integer as shown below:

num = 10
num = "Hello"

Functions as First-Class Citizens

In Java, you cannot pass a function directly as a parameter to another function. The only way to do it is to create an interface, define a class that implements that interface, and then pass the function as a parameter using the class object. On the flip side, in Python, functions, and methods are treated as first-class citizens and they can be directly passed as a function parameters. Look at the following Python script to better understand this concept.

class Test:
    def method1(self):
        print ('hello world')
    def method2(self, methodToRun):
        methodToRun
test = Test()
test.method2(test.method1())

 

In the above script, we create a class named Test. This class contains two methods: method1() and method2(). The method2() function accepts 1 parameter which stores a method passed as a parameter value (self is not a parameter here, self here refers to the object itself). Next, an object of the test class is initialized which calls the method2(). In the call to method2(), the method1() is passed as a parameter. 

Furthermore, in Java, a method or function has to be defined inside a class, whereas in Python, you can define a function anywhere.

Returning Multiple Values from Functions

A function in Java can only return a single value. For instance, if you run the following code, you will see a compile-time error since it returns two values val1 and val2. In order to return multiple values, you would normally store values in a list and then return the list as a whole. In addition, you have to define the return type of a function as well.

public static String mymethod()
{
    String val1 = "abc";
    String val2 = "xyz";
    return val1, val2;
}

In Python, a function can return multiple values. For instance, if you look at the following script, you can see that the function mymethod() returns two values. In the same way, you can return any number of values. In addition, you do not have to specify the return types for Python functions.

def mymethod():
    val1 = "abc"
    val2 = "xyz"
    return val1, val2

Overall, Java and Python are very similar languages. Both are high-level languages that support object-oriented programming as well. To switch from Java to Python you need to study the differences between the two languages. This article tried to cover some of the major technical differences and the concepts that you will have to learn in order to switch from Java to Python.