1. Difference Between Python vs Java
The last session was on pros and cons of Python. In this
Python Vs Java Tutorial, we will study a basic difference between java and
python. The comparison between Java and Python on the basis of syntax,
simplicity, speed, interpreted, database accessibility etc.
So, let’s start Python Vs Java.
2. Hello World in Java and Python
To rigorously compare Python and java, we first compare the
first program in any programming language-
to print “Hello World”.
Java
public class HelloWorld
{
public static void
main(String[] args)
{
System.out.println("Hello World");
}
}
Python
Now, let’s try printing the same thing in Python.
print(“Hello World”)
As you can see, what we could do with 7 lines of code in
Java, we can do with 1 line in Python.
Let’s further discuss parts of this program, and other
things.
3. Python vs Java – Syntax
A striking characteristic of Python is simple python syntax.
Let’s see the subtle differences.
a. Semicolon
Python statements do not need a semicolon to end, thanks to
its syntax.
>>> x=7
>>> x=7;
But it is possible to append it. However, if you miss a
semicolon in Java, it throws an error.
class one
{
public static void
main (String[] args)
{
int x=7;
System.out.println(x)
}
}
Compilation error
#stdin compilation error #stdout 0.09s 27828KB
Main.java:10: error: ‘;’ expected
System.out.println(x)
^
1 error
b. Curly Braces and Indentation
In Java, you must define blocks using semicolons. Without
that, your code won’t work. But Python has never seen a sight of curly braces.
So, to define blocks, it mandates indentation. Some lazy programmers don’t go
the extra mile to indent their code, but with Python, they have no other
choice. This indentation also aids readability. But you must compliment the
indentation with a colon ending the line before it.
>>> if 2>1:
print("Greater")
Greater
This code would break if we added curly braces.
>>> if 2>1:
{
SyntaxError: expected an indented block
Now, let’s see if we can skip the curly braces and
indentation in Java.
class one
{
public static
void main (String[] args)
{
if(2>1)
System.out.println("2");
}
}
Success
#stdin #stdout 0.07s 27792KB
2
Here, we could skip the braces because it’s a single-line
if-statement. Indentation isn’t an issue here. This is because when we have
only one statement, we don’t need to define a block. And if we have a block, we
define it using curly braces. Hence, whether we indent the code or not, it
makes no difference. Let’s try that with a block of code for the if-statement.
class one
{
public static void main
(String[] args)
{
if(2<1)
System.out.println("2");
System.out.println("Lesser");
}
}
Success
#stdin #stdout 0.09s 28020KB
Lesser
As you can see here, 2 isn’t less than 1, so the if
statement’s body isn’t executed. Since we don’t have curly braces here, only
the first print statement is considered to be its body. This is why here, only
the second statement is executed; it isn’t in the if’s body.
c. Parentheses
Starting Python 3.x, a set of parentheses is a must only for
the print statement. All other statements will run with or without it.
>>> print("Hello")
Hello
>>> print "Hello"
SyntaxError: Missing parentheses in call to ‘print’
This isn’t the same with Java, where you must use
parentheses.
d. Comments
Comments are lines that are ignored by the interpreter. Java
supports multiline comments, but Python does not. The following are comments in
Java.
//This is a single-line comment
/*This is
a multiline comment
Yes it
is*/
Now, let’s see what a comment looks like in Python.
>>> #This is a comment
Here, documentation comments can be used in the beginning of
a function’s body to explain what
it does. These are declared using triple quotes (“””).
>>> """
This is a
docstring
"""
'\n\tThis is a docstring\n'
These were the Python vs Java Syntax comparison
4. Python vs Java – Dynamically Typed
One of the major differences is that Python is
dynamically-typed. This means that we don’t need to declare the type of the
variable, it is assumed at run-time. This is called Duck Typing. If it looks
like a duck, it must be a duck, mustn’t it?
>>> age=22
You could reassign it to hold a string, and it wouldn’t
bother.
>>> age='testing'
In Java, however, you must declare the type of data, and you
need to explicitly cast it to a different type when needed. A type like int can
be casted into a float, though, because int has a shallower range.
class one
{
public static void
main (String[] args)
{
int x=10;
float z;
z=(float)x;
System.out.println(z);
}
}
Success
#stdin #stdout 0.09s 27788KB
10.0
However then, at runtime, the Python interpreter must find
out the types of variables used. Thus, it must work harder at runtime.
Java, as we see it, is statically-typed. Now if you declare
an int an assign a string to it, it throws a type exception.
class one
{
public static void
main (String[] args)
{
int x=10;
x="Hello";
}
}
Compilation error
#stdin compilation error #stdout 0.09s 27920KB
Main.java:12: error: incompatible types: String cannot be
converted to int
x="Hello";
^
1 error
5. Python vs Java – Verbosity/ Simplicity
Attributed to its simple syntax, a Python program is
typically 3-5 times shorter than its counterpart in Java. As we have seen
earlier, to print “Hello World” to the screen, you need to write a lot of code
in Java. We do the same thing in Python in just one statement. Hence, coding in
Python raises programmers’ productivity because they need to write only so much
code needed. It is concise.
To prove this, we’ll try to swap two variables, without
using a third, in these two languages. Let’s begin with Java.
class one
{
public static void
main (String[] args)
{
int x=10,y=20;
x=x+y;
y=x-y;
x=x-y;
System.out.println(x+" "+y);
}
}
Success
#stdin #stdout 0.1s 27660KB
20 10
Now, let’s do the same in Python.
>>> a,b=2,3
>>> a,b=b,a
>>> a,b
(3, 2)
As you can see here, we only needed one statement for
swapping variables a and b. The statement before it is for assigning their
values, and the one after is for printing them out to verify that swapping has
been performed. This is the main point in Python vs Java.
6. Python vs Java – Speed
When it comes to speed, Java is the winner. Since Python is
interpreted, we expect them to run slower than their counterparts in Java. They
are also slower because the types are assumed at run time. This is extra work
for the interpreter at runtime. The interpreter follows REPL (Read Evaluate
Print Loop). Also, the IDLE has built-in syntax highlighting, and to get the
previous and next commands, we press Alt+p and Alt+n respectively.
However, they also are quicker to develop, thanks to
Python’s brevity. Therefore, in situations where speed is not an issue, you may
go with Python, for the benefits it offers more than nullify its speed
limitations. However, in projects where speed is the main component, you should
go for Java. An example of such a project is where you may need to retrieve
data from a database. So if you ask Python or Java as far as speed is
concerned, Java wins.
7. Python vs Java – Portability
Both Python and Java are highly portable languages. But due
to the extreme popularity of Java, it wins this battle. The JVM (Java Virtual
Machine) can be found almost everywhere. In the Python versus Java war of
Portability, Java wins.
8. Python vs Java – Database Access
Like we’ve always said, Python’s database access layers are
weaker than Java’s JDBC (Java DataBase Connectivity). This is why it isn’t used
in enterprises rarely use it in critical database applications.
9. Python vs Java – Interpreted
With tools like IDLE, you can also interpret Python instead
of compiling it. While this reduces the program length, and boosts
productivity, it also results in slower overall execution.
10. Python vs Java – Easy to Use
Now because of its simplicity and shorter code, and because
it is dynamically-typed, Python is easy to pick up. If you’re just stepping
into the world of programming, beginning with Python is a good choice. Not only
is it easy to code, it is also easy to understand. Readability is another
advantage. However, this isn’t the same with Java. Because it is so verbose, it
takes some time to really get used to it.
11. Python Vs Java – Conclusion
So, after all that we’ve discussed here in Python vs Java
Tutorial, we come to conclude that both languages have their own benefits. It
really is up to you to choose one for your project. While Python is simple and
concise, Java is fast and more portable. While Python is dynamically-typed,
Java is statically-typed. Both are powerful in their own realms, but we want to
know which one you prefer. Furthermore, if you have any query/question, feel
free to share with us!
No comments:
Post a Comment