[#]: collector: (lujun9972) [#]: translator: ( ) [#]: reviewer: ( ) [#]: publisher: ( ) [#]: url: ( ) [#]: subject: (Java vs. Python: Which should you choose?) [#]: via: (https://opensource.com/article/19/12/java-vs-python) [#]: author: (Archit Modi https://opensource.com/users/architmodi) Java vs. Python: Which should you choose? ====== Compare the two most popular programming languages in the world, and let us know which one you prefer in our poll. ![Developing code.][1] Let's compare the two most popular and powerful programming languages in the world: Java and Python! Both languages have huge community support and libraries to perform almost any programming task, although selecting a programming language usually depends on the developer's use case. After you compare and contrast, please make sure to answer our poll to [share your opinion][2] on which is best. ### What is it? * **Java** is a general-purpose object-oriented programming language used mostly for developing a wide range of applications from mobile to web to enterprise apps. * **Python** is a high-level object-oriented programming language used mostly for web development, artificial intelligence, machine learning, automation, and other data science applications. ### Creator * **Java** was created by James Gosling (Sun Microsystems). * **Python** was created by Guido van Rossum. ### Open source status * **Java** is free and (mostly) open source except for corporate use. * **Python** is free and open source for all use cases. ### Platform dependencies * **Java** is platform-independent (although JVM isn't) per its WORA ("write once, run anywhere") philosophy. * **Python** is platform-dependent. ### Compiled or interpreted * **Java** is a compiled language. Java programs are translated to byte code at compile time and not runtime. * **Python** is an interpreted language. Python programs are translated at runtime. ### File creation * **Java**: After compilation, **<filename>.class** is generated. * **Python**: During runtime, **<filename>.pyc** is created. ### Errors types * **Java** has ****2 ****types of errors: compile and runtime errors. * **Python** has 1 error type: traceback (or runtime) error. ### Statically or dynamically typed * **Java** is statically typed. When initiating variables, their types need to be specified in the program because type checking is done at compile time. * **Python** is dynamically typed. Variables don't need to have a type specified when initiated because type checking is done at runtime. ### Syntax * **Java**: Every statement needs to end with a semicolon ( **;** ), and blocks of code are separated by curly braces ( **{}** ). * **Python**: Blocks of code are separated by indentation (the user can choose how many white spaces to use, but it should be consistent throughout the block). ### Number of classes * **Java**: Only one public top-level class can exist in a single file in Java. * **Python**: Any number of classes can exist in a single file in Python. ### More or less code? * **Java** generally involves writing more lines of code compared to Python. * **Python** involves writing fewer lines of code compared to Java. ### Multiple inheritance * **Java** does not support multiple inheritance (inheriting from two or more base classes) * **Python** supports multiple inheritance although it is rarely implemented due to various issues like inheritance complexity, hierarchy, dependency issues, etc. ### Multi-threading * **Java** multi-threading can support two or more concurrent threads running at the same time. * **Python** uses a global interpreter lock (GIL), allowing only a single thread (CPU core) to run at a time. ### Execution speed * **Java** is usually faster in execution time than Python. * **Python** is usually slower in execution time than Java. ### Hello world in Java ``` public class Hello {    public static void main([String][3][] args) {       [System][4].out.println("Hello Opensource.com from Java!");    } } ``` ### Hello world in Python ``` `print("Hello Opensource.com from Java!")` ``` ### Run the programs ![Java vs. Python][5] To run the java program "Hello.java" you need to compile it first which creates a "Hello.class" file. To run just the class name, use "java Hello." For Python, you would just run the file "python3 helloworld.py." -------------------------------------------------------------------------------- via: https://opensource.com/article/19/12/java-vs-python 作者:[Archit Modi][a] 选题:[lujun9972][b] 译者:[译者ID](https://github.com/译者ID) 校对:[校对者ID](https://github.com/校对者ID) 本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 [a]: https://opensource.com/users/architmodi [b]: https://github.com/lujun9972 [1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/code_development_programming.png?itok=M_QDcgz5 (Developing code.) [2]: tmp.Bpi8QYfp8j#poll [3]: http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+string [4]: http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+system [5]: https://opensource.com/sites/default/files/uploads/python-java-hello-world_0.png (Java vs. Python)