标签:
Some developers have claimed that Python is more productive than Java. It is dangerous to make such a claim, because it may take several days to prove that thoroughly. From a high level view, Java is statically typed, which means all variable names have to be explicitly declared. In contrast, Python is dynamically typed, which means declaration is not required. There is a huge debate between dynamic typing and static typing in programming languages. This post does not talk about that. However, one point should be agreed - Python is an interpreted language with elegant syntax and that makes it a very good option for scripting and rapid application development in many areas.
In this comparison, I will try to cover some basic language components, such as string, control flow, class, inheritance, file i/o, etc. All of them will be compared by using side-by-side examples. I hope this can provide java programmers a general idea of how Python and Java do the same thing differently. By a glance of the code below, we can easily realize that Python code is much shorter, even though some Java "class shell" (In Java everything starts with a class definition) is not listed. This might be one reason why Python can be more productive.
You may also check out the most popular python libraries and code examples.
1. Hello World
Start with the simplest program. Java needs a lot of words for printing just a string. This is the first example showing Python is more concise.
Java | Python | ||
|
|
Fist of all, whatever we do in Java, we need start with writing a class, and then put our desired method(s) inside. This is sometimes very annoying and it does waste time. In Python, you can simply start writing your code, and then run it.
2. String Operations
|
|
There are a lot of string related functions in Python which is as good as or better than Java, for example, lstrip(), rstrip(), etc.
3. Control Flow
|
|
4. Class and Inheritance
|
|
When you extend a base class, there is no requirement such as defining an explicit constructor for implicit super constructor.
5. File I/O
|
|
As we can see that there are a lot of classes we need to import to simply read a file, and in addition, we have to handle the exception thrown by some methods. In Python, it is two lines.
6. Collections
|
|
These comparisons only stand on the surface of Python, for real programming, the Python doc is still the best place to go for reference.
转自: http://www.programcreek.com/2012/04/java-vs-python-why-python-can-be-more-productive/
Java vs. Python (1): Simple Code Examples
标签:
原文地址:http://www.cnblogs.com/labc/p/4856750.html