Listen to this article

A couple of years back, Java was considered one of the most popular programming languages, but recently Python has also gained popularity as well. As a result, both of these languages are often compared and contrasted with one another. Let’s have a look at some of the differences:

  1. Processing of Source Code

Java uses a compiler to translate the source code into machine language, which is then run by the Java Virtual Machine (JVM). Here the source code is already in a “translated” form at runtime.

However Python makes use of an interpreter to directly translate the source code, line by line, at runtime. Thus it is somewhat slower than Java.

2. Termination of Statements

In Java, statements must end with a semicolon. This means that you can write any number of statements on the same line, provided you terminate each of them with a semicolon. In contrast, Python requires that you end each statement with… a line break! Going to a new line is the only way of ending a statement in Python, so no two statements can be on the same line. (However if you want to continue a statement on the next line in Python, end the current line with a backslash (\).)

Java Python
int a = 5; int b = a + 1; a = 5
b = a + 1

3. Demarcation of Statement Blocks

A block refers to a group of statements that can be used anywhere a single statement is allowed. In Java the statements that are part of a block are enclosed within curly braces (‘{}’). Indentation is also often done to make the code easier to read, but is not a requirement for the code to be syntactically correct.

In Python, however, indentation is what decides which block a particular statement is in. If your Python code isn’t indented properly, it won’t even run properly.

Java Python
int a = 5; if (a > 0){ System.out.println(“Positive”); }
System.out.println(“Outside the if”);
a = 5
if a >0:
    print(“Positive”)
print(“Outside the if”)

4. Types of Variables

Java demands that the type of a variable should be mentioned when it is declared (statically typed). This helps allocate enough memory for the variable when it gets a value.

However, in Python, the type of the variable is not mentioned when one is created. In addition, the value of the variable must be mentioned at declaration, which in turn decides the type of the variable (dynamically typed). This also means that in Python, the variable can change its type, if necessary, to accommodate different values.

Java Python
int a1; a1 = 5;
double a2 = 10.0;
String a3 = “Hello”;
a = 5
a = 10.0
a = “Hello”

We have seen some differences between Python and Java in this post. Join me as I compare and contrast more topics…

This post is a guest post for alphabet ‘J’ for the #BlogchatterA2Z challenge 2020. Stay tuned for next week’s posts…

(Visited 117 times, 1 visits today)

Related Posts

3 thoughts on “Java vs Python

Leave a Reply