What is the Difference Between Java and Python?

There has always been a need for a better high level language since the first high level language appeared. Programmers and scientists have been trying to identify mistakes in older programming languages and have long been trying to develop languages that rectify those mistakes. But rectifying one mistake may have certain tradeoffs. When a new language is devised, we try to understand the compromises that need to be made between reliability, performance, readability and portability. The purpose of creating new languages is so that we can make better use of the features and techniques that are already known to us. Whether a feature is just an overhead and not likely to be used, or is it a feature that needs to be retained. Programming languages like C and C++ changed the way we program for better for the most part. With increased computational power, these languages were able to make use of all the available resources in modern computer programming. A comparison between many high level languages yields results based on the perspective of the person who is analyzing those languages because if a feature is well received by some people maybe a total waste for someone else. So to declare that a language is better than another is dependent on various factors.

Python is one of the most widely used high level programming language. Python came only a few years before Java although many significant changes have been made in both the languages. Python’s latest version came out in late 2015.  Its design philosophy is conciseness and readability. Python focusing on trying to do exactly what a programmer intends to due by writing as few lines as possible. Rather than writing several lines to do something very trivial. Python allows the programmer the ease that caters to most of the problems of readability shown in other languages. Python focuses on eradicating verbose, so that an operation takes lesser lines of code to implement in python than other languages. Python not only tries to use as less words as possible as verbosity is not just a matter of using fewer words but also to ensure that there are less places where they is a chance to make a mistake. Python’s aim is to let programmer to spend his time programming the actual application rather than satisfying the compiler’s needs.

Sample statement: print “Hello World!”;

Java according to spectrum.ieee.org is the most popular language in the world. Java is a strictly object oriented language. Java’s best trait is its portability, it is a multi-platform language that runs on all the popular operating systems.  Java’s Virtual Machine (JVM) is responsible to convert java code to byte code depending on the architecture it’s running on, making it one of the most versatile languages of the world. Programmers using java can develop applications for web, desktop and mobile platforms. Java’s syntax is quite identical to C and C++ from which it was derived but it did not inherit all of their features, hence it has very little low level capabilities. Although it is widely used for embedded systems. Java was created under Sun Microsystems by James Gosling who developed java. Java was originally named oak after an oak tree outside James’ office before finally being renamed Java. James Gosling made java similar to C/C++ so that programmers could find it easy to adapt to Java without having to go through documentation all the time. Although C++ and C can work without strictly following object oriented guidelines. According to Oracle their five primary goals in the creation of Java language were to make it “simple and familiar”, “Robust and secure” and making it portable so that it is not limited to only a single specific environment.

Sample statement: System.out.println (“Hello World!”);

As the sample statements suggests, python uses less words in a statement to do exactly what this aforementioned statement does in Java. i.e. – print Hello World. As previously described, it’s not easier to make claims stating that one language is better than the other by examining just one feature, it’s a debatable topic. As far as Python and Java are concerned, many of their attributes vary significantly from one another. A few claim that a programmer can be significantly more productive in Python than he can be in Java, although this claim isn’t totally wrong, but it doesn’t mean that python can replace java everywhere. The write once run anywhere philosophy of java makes it very powerful tool for cross platform development.

There are some characteristics where Python comes off stronger. Firstly Java is statically typed whereas python is dynamically typed. Statically typed languages are those where the variable name is bound to an object or a type at compile time, which means that once bound, the name is bound to a specific type and Although through assignment operations, it can be bound to other objects of the same time but it can never be bound to an object of a different type. Java doesn’t allow that and it throws a type exception. Whereas programming languages that are dynamically bound like Python, a variable name is object to objects on execution time and hence it is possible to bind a name to object of a different type even after it was bound to another object. When declaring or initializing a variable in Java, like C and C++, it is required to provide the the data type to be associated with that name. But case is very much different in python like php or JavaScript where it is not required at compile time to provide a data type to be associated to a name. The value that is stored in that variable provides the type information at runtime. There are many that are against static typing and same is the case with dynamic typing, the debate is on-going as to which should be preferred. Python suggests that rather than having the programmer waste time on casting types, the programmer should be focused on what needs to be done and not worry about these chores.

Python has many other advantages over Java, because it is focused more on the programmer and python has been developed trying to ensure that it lets program use small statements and short codes to achieve what needs to be achieved. Python syntax is very concise and provides very clean cut brevity, this helps a programmer a lot and makes it marginally easy for a programmer to organize his code, for example, in order to write a simple program that prints “Hello!” on console in java is as follows,

public class MySimpleProgram

{

public static void main (String [] args)

{

System.out.println (“Hello!”);

}

}

 

But the same can be achieved in python by writing a single line, ie: print “Hello!”  . That shows how verbose java is whereas python is very simple, some do suggest that Java being verbose allows the program to be more understandable if the programmer in not going to comment the code.

There are many advantages to java programming and one of them is java being strictly object oriented. Java doesn’t allow a function or any statement outside a class, everything that a java program contains is divided into classes and all the code is written inside, and only a single class can reside inside a single file, unlike C or C++, where multiple structs and classes can be present inside a class. This means that if a program has 10 classes then it must have 10 files as well, one for each class, and the name of the file must be the same as the class that resides in it. These limitations are helpful in organizing the code but it makes it more complicated to keep track of files, and every tiny class needs to be in a separate file which can create complications for the programmer. Python in this regard allows freedom to programmer to put his classes wherever it pleases him. This makes sense as programmers tend to keep similar classes together and so 10 classes will not necessarily be needing 10 files, rather the programmer can maybe have 3 files for 10 classes. Although this limitation doesn’t worry everyone, it’s always better to have options rather than restrictions.

Java although has all these restrictions in place in order to ensure that the written code is robust and reliable, some of these limitations really get on your nerves because some of these might seem redundant and useless. One other example of such case is Java’s exception handling, As java uses checked exceptions, exceptions that are caught or thrown by every method in which they appear, it becomes a very tedious process, if for example there is a method named drive that calls engine which in turn calls fuel, in this case if the method fuel throws an exception, then it must be caught by all the methods that were invoked in the process. Which means that if fuel throws exception, then engine will also throw an exception and it should be caught by the method drive or else the code will not compile. Imagine having to add throws Exception signature to all the methods just to satisfy the compiler. If method a calls B calls C calls D. all methods must have throws Exception added to their signature and finally caught in method A. This is fast becoming one of the most annoying characteristics of Java. This is not the case with python, where if a method throws exception it automatically propagates upwards to the first method that was invoked and the exception can be caught there rather than having to write throws declarations to every method called in the way. Some developers do prefer checked Exceptions because they this it helps them keep track of the vulnerabilities in their code but some find it a very tedious task to keep track of an exception all the way to the first method.

Another Big example of ease of development in python is that when a method needs to be overloaded, python provides optional parameters in method signatures which decreases the number of lines written in the code significantly.

For example:

———————————————-

def myFun (name, age =18, dob = 1990):

self.name = name

self.age = age

self.dob = dob

return;

———————————————–

The code above is effectively equivalent to 3 overloaded methods.

In java, we would have to write the following 3 methods.

———————————————–

public myFun(String name)

{

this.name  = name;

this.age = 18;

this.dob = 1990;

}

public myFun(String name, int age)

{

this.name = name;

this.age = age;

this.dob = 1990;

}

public myFun (String name, int age, int dob)

{

this.name = name;

this.age = age;

this.dob = dob;

}

————————————————-

All these capabilities of python suggest that python was introduced to make it easy for the programmer to remember the syntax and most of the basic operations so that he doesn’t have to look up operations as simple as reading a file or printing a string on console. Java due to verbose, demands writing a lot of extra code that needs to be written every time some simple operations needs to be performed. These extra lines although required by the Java are just a waste of space and time for the developer. Firstly the programmer has to import a certain library that contains those operations in order for them to run. Although modern IDEs have made it easier to import those libraries, it’s still a wasteful task that must be performed. For example, the code below performs a file read operation, although the basic code consists of a few lines, if the code is squeezed to a few lines, it still is very complicated. Many java developers won’t even understand everything that’s going on in this statement.

Scanner in = new Scanner (new FileReader (“filename.txt”));

Now you can call methods of Scanner class on in object to proceed. While python’s dynamic typing yet again allows the programmer to not worry about the exact name of the class and open the file as simply as it can possibly be.

File = open (‘filename.txt’);

The above syntax is easy to understand and use. Without having to worry about what a Scanner is and what is a FileReader class.

Java is one of the most used languages of the world even when it’s probably not the best language of the world. Java’s power across several platforms, makes its reach greater, it’s one of the first few languages taught to people who start application development. Although there are things in Java that could’ve been handled better, Java still is one of the most popular languages. Most of the development done in Android OS which is based on Linux kernel is done on Java including all App development. Java Virtual Machine dalvik is one of the core components of Android OS, newer version ART, Android Run time is now slowly replacing Dalvik. Android OS is the most widely used and most popular Mobile operating System nowadays and its one of the reasons why Java is so widely used.

Python is becoming more and more popular, albeit slowly. Many of the most popular websites including but not limited to Google, YouTube, Pinterest, Drop Box, Reddit and Instagram are developed using Python.

Both languages have their advantages and disadvantages but python focuses more on the Programmer’s ease that’s why many developers are going to want to shift to python. Java’ vast documentation and capabilities will ensure that while many would prefer Python over Java, many would still love to hold on to Java.

References

[1] Bradley Efron and Robert Tibshirani, “An introduction to the Bootstrap Monographs on statistics and applied probability,” Chapmanand Hall, NewYork, London, 1993.
[2] Lutz Prechelt, “An empirical comparison of C, C++, Java, Perl, Python, Rexx, and Tcl,” Fakultat fur Informatik, Universitat Karlsruhe, Karlsruhe, 2000.
[3] Sebastian Nanz, CarloA.Furia, “A Comparative Study of Programming Languages in Rosetta Code,” arXiv: 1409.0252 v4, Zurich, Switzerland, 2014.
[4] Barbara Unger, Lutz Prechelt, “A controlled experiment on the effects of PSP training: Detailed description and evaluation,” Fakultatfur Informatik Universitat Karlsruhe, Karlsruhe,Germany, 1999.
[5] Bradley Efron, R.J. Tibshirani, An Introduction to the Bootstrap, CRC Press, 1994, p. 456.
[6] P. Lutz, “An empirical comparison of seven programming languages,” Computer 33, 2000.
[7] Cunningham, Douglas, Eswaran Subrahmanian, and Arthur Westerberg, “User-Centered Evolutionary Software Development Using Python and Java.,” in 6th International Python Conference, 2010.
[8] Jim Hugunin, “Python and Java: The Best of Both Worlds,” in 6th international Python conference, 1997.
[9] Sanner, Michel F, “Python: a programming language for software integration and development,” J Mol Graph Model, p. 57-61, 1999.
[10] Enrique Flores, Alberto Barr´on-Cedeno, Paolo Rosso,and Lidia Moreno, “Towards the Detection of Cross-Language Source Code Reuse,” in Natural Language Processing and Information Systems, Berlin Heidelberg, 2011.
[11] Bishop, Judith, and Riaan Hurter, “Competitors to Java: Scripting languages,” in South African Computer Lecturers Association-SACLA-conference, Golden Gate, South Africa, 1999.
[12] Necaise, Rance D., “Transitioning from Java to Python in CS2,” Journal of Computing Sciences in Colleges 24, pp. 92-97, 2008.
[13] T OGRAPH, B., and Y. RICHARD MORGENS, “Cloud computing,” Communications of the ACM 51, p. 7, 2008.
[14] A. Radenski, “Python First: A lab-based digital introduction to computer science,” ACM SIGCSE Bulletin, vol. 38, no. 3, pp. 197-201, 2006.
[15] Vinoski, Steve, “Advanced message queuing protocol,” IEEE Internet Computing 6, pp. 87-89, 2008.
[16] Juneau, Josh, Jim Baker, Frank Wierzbicki, Leo Soto Muoz, Victor Ng, and Donna L. Baker, The definitive guide to Jython: Python for the Java platform, Apress, 2010.
[17] Blackburn, Stephen M., Robin Garner, Chris Hoffmann, Asjad M. Khang, Kathryn S. McKinley, Rotem Bentzur, Amer Diwan, “The DaCapo benchmarks: Java benchmarking development and analysis.,” ACM Sigplan Notices, vol. 41, no. 10, pp. 169-190, 2006.
[18] Stubblebine, Tony, Regular Expression Pocket Reference: Regular Expressions for Perl, Ruby, PHP, Python, C, Java and. NET., O’Riley Media Inc., 2007.
[19] Cattuto, Ciro, Dominik Benz, Andreas Hotho, and Gerd Stumme, “Semantic analysis of tag similarity measures in collaborative tagging systems,” arXiv preprint arXiv:0805.2045 , 2008.
[20] Fourment, Mathieu, Michael R. Gillings, “A comparison of common programming languages used in bioinformatics,” BMC bioinformatics 9, 2008.
[21] Agarwal, Krishna K., Achla Agarwal, M. Emre Celebi, “Python puts a squeeze on java for CS0 and beyond,” Journal of Computing Sciences in Colleges, vol. 23, no. 6, pp. 49-57, 2008.
[22] Lo Chieh-An, Yu-Tzu Lin, and Cheng-Chih Wu, “Which Programming Language Should Students Learn First? A Comparison of Java and Python,” Learning and Teaching in Computing and Engineering (LaTiCE), pp. 225-226, 2015.
[23] Mason, Raina, and Graham Cooper, “Introductory Programming Courses in Australia and New Zealand in 2013-trends and reasons.,” in Sixteenth Australasian Computing Education Conference, V. 148, 2014.
[24] O’Brien, Casey, Max Goldman, and Robert C. Miller, “Java tutor: bootstrapping with python to learn Java.,” in 1st ACM conference on Learning@ scale conference, 2014.
[25] Lin, Jimmy, and Miles Efron, “Overview of the TREC-2013 microblog track,” in TREC vol. 2013, 2013.
[26] Turner, Stephen, “Security vulnerabilities of the top ten programming languages: C, Java, C++, Objective-C, C#, PHP, Visual Basic, Python, Perl, and Ruby,” Journal of Technology Research, vol. 5, no. 1, 2014.

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here