Compare java and python
Compare java and python
April 18, 2011
1. In terms of difficulty. python is far simpler than java.
2. Development speed. Python is far better than java
3. Running speed. Java is far superior to standard Python, and pypy and cython can catch up with Java, but neither is mature enough to do projects.
4.Available resources. There are a lot of Java resources, but very few Python resources, especially Chinese resources.
5. Stability. Python 3 and 2 are not compatible, causing a certain degree of confusion and the failure of a large number of class libraries. Java is much more stable because it has corporate support behind it.
6. Is it open source? Python has been completely open source from the beginning. Java is developed by sun, but now there is Openjdk for GUN available, so don't worry.
7. Compile or interpret. Both are interpretive.
I understand that C is like a manual transmission car (compiled language), and Java and Python (interpreted language) are like automatic transmission cars. The fastest cars are all manual transmissions, but for people who are not good at driving, it is faster to drive an automatic transmission.
Kno has an article that talks about choosing a programming language, "Determine your needs first", don't decide based on the simplicity or complexity of the language. Only by being able to write programs that you really think is useful can you gain satisfaction and continue learning.
So what kind of environments are java and python suitable for? It can be seen from sourceforge.net:
The most famous and time-tested common applications are basically written in C++. For example emule, 7-zip, WinSCP, FileZilla, etc.
Part of it is developed by java, such as the most famous OpenOffice.
There are very few python writers, such as Pidgin and FireBird.
The ranking of development languages (how many programs are developed in this language) is as follows:
#Java46,202
#C++36,895
#php30,048
#C28,075
#C#13,476
#Python13,379
# Javascript11,285
# Perl9,216
Fairer.
It can be seen that java is the most widely used language in both GNU and commercial fields. C is mainly used to build the bottom layer of the system. C++ and Java are used to build the intermediate application layer. If the resources are sufficient, then C++ development will be chosen for running speed, otherwise Java development will be used for development speed. Python is better than Java in all aspects and can be described as a next-generation language. But the most controversial thing is its speed. Pure Python is much slower than Java, and there is no commercial support behind it. Its stability has been criticized. So far, python is mainly used as a glue language at the commercial level to bind class libraries of other languages (mainly c/c++). In the GNU field, it is mainly limited to small-scale applications and personalized applications. and reverse engineering (hacking) applications.
Why is Java widely used on the server side but less used on the client side? Could it be that the amount of calculation used on the server side is less? I think this shows that compared to c++, the speed of java is still acceptable. What is unacceptable is the JRE platform and the moment when the JRE platform is stuck when it starts. I once thought that the performance of programs written in Java was low.
One thing that Python users often say is: Python is not slow because Python calls a large number of C libraries when running, and C is very fast. Thinking about it on the other hand, this reflects the fact that it is a glue language. Any language can call the C library. Is this more valuable? If a library is entirely made of python, then its operating efficiency... doesn't matter. You can't always use other people's libraries for programming.
----
The current use of the Python programming language requires continuous learning. Below we will take a detailed look at how to better learn relevant knowledge. Recently I have been looking at the code of a GUI application based on wxPython, which is about 45.5KLOC, and this does not include the libraries it uses (such as Twisted).
The code is written by Java developers who are new to Python, so it has serious performance issues (such as thirty seconds of startup time). When checking the code, I found that they wrote a lot of things that make sense in Java but are difficult to accept in the Python programming language. It's not because "Python is slower than Java", but because there are more convenient ways to accomplish the same goals in Python, even things that are impossible in Java.
So, the sad thing is that these guys are getting half the result with half the effort, and the code they write is much slower than what should be implemented in the Python programming language. Below, let’s look at some examples:
◆Static methods in Java cannot be translated into Python class methods. Oh, sure, it more or less produces the same effect, but the purpose of class methods is actually to do things that are usually not even possible in Java (like inheriting a non-default default function). The idiomatic translation of Java static methods usually translates into a module-level function, rather than a class method or static method. (And static constants should be translated into module-level constants.)
This is not a performance issue, but if a Python programming language programmer wants to call Foo.someMethod, he is forced to use a method like Foo.Foo.someMethod in Java. If he does, he will be driven crazy. One thing must be noted: calling a class method requires an additional storage space, but calling a static method or function does not require this.
By the way, the property chain of these Foo.Bar.Baz is not itself You can count them. In Java, these dotted names are searched by the compiler, and the total number is not considered when running. In Python, the search process is performed at runtime. So include every point. (In Python, one thing to remember is that "tiled structures are better than nested structures", although it may be more about "readability" than performance. and "simple is better than complex".)
◆ Want to use a switch statement? The Python programming language will be a hash table, not a bunch of if-then statements. Want to use a bunch of if-then statements that are not switch statements in Java but also have strings involved? It will still be a hash table. CPython dictionaries are implemented using hash tables which are considered to be one of the best performers in the field that we know of. The code you write yourself won't be any better than this, unless you are the illegitimate son of Guido, Tim Peters, and Raymond Hettinger, and you are genetically enhanced.
◆xml is not the answer. It's not a problem either. Now paraphrasing Jamie Zawinski with regular expressions, "Some people, when they encounter a problem, think 'I know, I'm going to use XML.' Then they have two problems."
vs. This is a different situation in Java because XML is flexible and flexible compared to Java code. But compared to Python code, XML is an anchor and a burden. In Python, XML is used to work together, not your core functionality, because you don't need to do that. In Java, XML may be your savior because it lets you implement domain-specific languages and improve the adaptability of your applications "without coding". In Java, avoiding encoding is a big advantage because encoding means recompiling. But in Python, it's usually easier to write code than XML. Also, Python processes code much faster than XML. (Not only that, you have to write the XML processing code, and Python has already written it for you.)
If you are a Java programmer, you don't have the gut instinct to think about whether you want to do it in your Python core XML is used as part of the application. If you are not implementing an existing XML standard or establishing some kind of input or output format or building some kind of XML editor or processing tool for information exchange reasons, then don't do it. Don't do it at all. Don't even think about it. Now, throw away that XML schema and get your hands free! If your application or platform is going to be used by Python programming language developers, they will just thank you for not adding the burden of using XML to their work.
(The only exception here is if your target audience actually needs to use XML for some reason. Like, they refuse to learn Python but they will pay you if you use XML, or you The plan is to give them a great GUI for editing XML, and the XML GUI is written by another person, and you get the right to use it for free. There are also some rare architectural reasons that need to use XML. Trust me, they won't apply to your program. If in doubt, explain your use case to a senior Python developer. Or, if you're thick-skinned and don't mind being laughed at, try asking one. Lisp program explains why your program should use XML!)
◆Getters and setters are evil. I should say it's the devil, it's the devil! Python programming language objects are not Java Beans. Don't write getters and setters, but build them into "properties". It doesn't make sense until you can prove that you need functionality that's a little more complex than a simple access, otherwise, don't write getters and setters. They are a waste of CPU time, and more importantly, they are a waste of valuable programmer time. Not just for the people who write the code and tests, but also for the people who are going to read and understand them.
In Java, you must use getters and setters, because public fields do not allow you to change your mind and use getters and setters later. So, in Java you'd better avoid these "chores" beforehand. In Python, this is silly, because you can start with a normal feature and change your mind at any time without affecting the class any customer. So don't write getter and setter methods.
◆Code duplication is usually an unavoidable disaster in Java, you often have to write the same method over and over again with only a little change (usually this is because of static type constraints). It is neither necessary nor worthwhile to do this in Python (except for a few specific occasions where you need to inline some functions that require performance). If you find yourself writing the same code over and over with little variation, you need to learn about closures. They're not actually that scary.
A summary of Python programming skills
A brief introduction to the flexible Python programming language
Master the Python programming language in a short time
Introduction to the history of the Python programming language
Description of the Python programming language
This is it What you have to do. You write a function that contains a function. The internal function here is the template of the function you want to write over and over again, but in it you add functions that use variables for different situations. The outer function takes the variable just raised as a parameter, and returns the inner function as a result. Then, every time you want to write a slightly different function, you just call the outer function and assign the return value to the name you want the "duplicate" function to appear. Now, if you need to change the way this works, you only need to change one place: the template.
Among the applications/platforms I have seen, there is only one very trivial program that uses this technique, which removes hundreds of lines of heavy-duty code. In fact, because developers use special boilerplate files to develop plug-ins for this platform, this saves a lot of code for third-party developers and also makes things simpler for those programmers to learn.
This is just the tip of the iceberg of the change in the way of thinking about Java->Python programming language. Now I can make the change correctly without delving into the details of the program. Essentially, if you have used Java for a while and are new to Python, don't trust your instincts too much. Your instincts have been conditioned by Java, not Python. Taking a step back, the most important thing is to stop writing so much code.
In order to do this, make yourself feel that you need Python even more. Pretend as if Python is a magic wand that can do anything you want without any effort on your part. Ask, "How does Python solve my problem?" and "What feature of the Python language is most similar to my problem?" You'd be surprised if what you need already exists in some form. . In fact, this phenomenon is so common, even among experienced Python programmers, that the Python community has a name for it. We call it "GUIDO's Time Machine" because before we master it ourselves, it often seems like that's the only way to get what we need.
So, if you can't feel at least 10 times more productive when using Python programming language than using Java, you'd better make some changes. Have you forgotten to use time machine! (chances are good that you' ve been forgetting to use the time machine) (At the same time, if you still miss your Java IDE, you can think of it this way: because the Python program you wrote is much more complicated than it needs.)
The above is the comparison between java and python For more related articles, please pay attention to the PHP Chinese website (www.php.cn)!