Subscribe to DSC Newsletter

Comparative study note of Java and Python (I)

One usually asked question is:  with which programming language to start when someone just begins studying computer science? Then a lot of suggestions will come up.

1      1.       You should start with one static typed language say C/C++, java etc. and then move easily to dynamic typed language like Perl, Python etc. Because it is harder to move from dynamic typed language to static typed language.

     But then an argument comes up: static typed language is much harder than dynamic one, and less productive.  Moreover, the way to write code in dynamic typed language is very different from that of static one. Once you get used to the static typed language, you may not feel comfortable with the dynamic one. So:

2      2.       You should start with dynamic typed language.

    But there are also arguments that disagree.  Then why not:

3      3.        We start with both.

This note is a try to study both from the beginning. But this is not a text book, I am not going to write down all the details here, for those who need the details, you could just find enough on Google. I will just do a comparative study on Java and Python.

1 Declare and initialize the variables

As a practical study note, we don’t need” Hello World”. We go straight forward to the point.   

The first thing to do when writing code would always be declaring the variables you will use in your program (Let’s assume that we already have designed the algorithm). The way you declare a variable in Java is like this:

[type name]  [variable name] = [value];

Int   example_Variable = 0;

Assigning a value to the variable is not necessary. You can just declare the variable and initialize it when needed. And in Python, the statement is like this:

[variable name] = [value]

example_Variable = 0

You may notice the difference between them. In Java you have to declare the type explicitly. If not you cannot compile you program. Also, the value assigned to the variable must be of the right type, and you can’t change the data type of the variable.

But in Python, the data type is determined by the value assigned. In the example above, the type of the variable is int. If you write the code like this:


A= “ hello”

Then A is first an integer of value 0, then becomes a string of value “hello”. In Python you can change the data type whenever you want. This is so called dynamic typed. In Java, when you declare the data type for a variable, then it is fixed there. This is so called static typed. 

One easily-made mistake when coding in dynamic typed language is:





You want to do a true division and obtain a result 0.5. But by this code you will get a result of 0, a round integer of the true result.  Because by assigning 10 to a, a is declare to be an integer(b also), thus the result is of  course an integer. You have to assign 10.0 to a( 20.0 to b)  so as to obtain a floating point number result. In Java you don’t need to worry about this, because you will always be very clear of the data type. After all, the naming convention and carefully assigning value would be helpful. This link gives you a brief introduction to naming convention.

The second difference is that in Java you need a semicolon ; to indicate that the statement is finish. But in Python you don’t need any delimiter to indicate a finish. 

Up till now, we see only disadvantages of Python, and now here comes the advantage.  In both Java and Python, there are some built-in fundamental data types:

In Java : boolean, char, byte, short, int, long, float, double;

In Python: Boolean, int,  long, float, complex.

The data types are more or less the same between two languages, except one thing: there is a complex data type in Python, and Java no. So mathematicians and those who work with complex number would be very happy using Python. And if you want to use complex number in Java, you have to write your own class for this, including the mathematical operation functions.

Here’s the link for an example of the complex number Java class code:

Views: 1730

Tags: java, python


You need to be a member of AnalyticBridge to add comments!

Join AnalyticBridge

On Data Science Central

© 2021   TechTarget, Inc.   Powered by

Badges  |  Report an Issue  |  Privacy Policy  |  Terms of Service