Object-oriented Programming is a type of computer programming in which the programmer creates data structures called objects and the various functions that those objects are required to perform. In this type of programming, the functions that the object can perform, and the ways in which objects interact with each other, are the main points of emphasis. Two very notable languages that are considered Object-oriented are Java and Python. While Java and Python do share some similarities, they also have key differences.
Java is an object-oriented programming language that was first idealized in 1991 by a group of engineers at Sun Microsystems, a company that was later acquired by Oracle. Led by James Gosling, the Java programming language first emerged in 1995 (History of Java Technology). Java’s syntax is very similar to C and C++, it even solves the drawbacks of C and C++ such as automatic and simple memory management. With this, beginner programmers find Java very familiar and easy to learn, given that they have a background in C and/or C++. Furthermore, Java is very forgiving and reliable language. One of Java’s key points of emphasis is early error detection. This is because Java compilers have the ability to reveal errors that would otherwise pop up when first executing a program in a different language, such as C.
Python is another object-oriented programming language that was first derived in the 1980s by Guido van Rossum. Python was developed with easy to read syntax in mind making it a very suitable language for beginner programmers. van Rossum further describes this idea in an interview stating that he “created a basic syntax, used indentation for statement grouping instead of curly braces or begin-end blocks, and developed a small number of powerful data types (Venners, 2003). So with Python, programmers are able to concentrate on logic errors rather than having to worry about complicated syntax errors. Furthermore, Python is a considered a very high level language. This means that programmers do not have to worry about issues like memory allocation like in other low level languages. In addition, Python also features a massive standard library that supports many functions like reading and modifying files.
While Java and Python do have some distinct backgrounds and features, they also have their similarities. Both Java and Python utilize the object-oriented approach. Furthermore, the key similarity between Java and Python is that they automatically allocate memory and collect garbage rather than the programmer having to do both tasks manually like they would in a lower level language.
Python:
def foo(names):
for name in names:
print name
foo(["Eric", "Ernie", "Bert"])
foo(["Guthtrie", "Eddie", "Al"])
Java:
public static void main(String[] args) {
int i=1;
Object obj = new Object
Memory mem = new Memory();
mem.foo(obj);
}
private void foo(Object param) { /
String str = param.toString();
System.out.println(str);
}
As mentioned earlier, Java and Python allow the programmer to create as many objects as they wish. Both languages automatically allocate the memory and get rid of the garbage as soon as it is done. In these examples, all the objects that were created and had memory allocated for them will be automatically freed whenever they are no longer referenced in the program. Should the programmer want to keep the objects, he or she would simply create a reference to that object. Otherwise, both languages will eventually get rid of the objects. So in the Python example, all objects created in the “foo” function will be eventually junked after the function ends and in the Java function all objects will be junked after the main method ends.
Despite their similarities, the major key between Java and Python are their differences. One major difference between Java and Python is their syntax. Java is often viewed as a very “wordy” language while Python was designed with simplicity in mind. This notion can be seen with the beloved beginner “Hello World!” program:
Java:
public class HelloWorld
{
public static void main (String[] args)
{
System.out.println("Hello World!");
}
}
Python:
Print “Hello World”
Both of these programs produce the same intended output, however, Java requires significantly more code to produce “Hello World!” compared to Python’s need for a single line. In addition, Python does not even require braces for functions. All the programmer needs to do is simply indent the following line. Another key difference is that Java is statically typed while Python is dynamically typed. This can be seen in the following example:
Java:
int myCounter = 0;
String myString = String.valueOf(myCounter);
if (myString.equals("0")) …
Python:
myCounter = 0
myString = str(myCounter)
if myString == "0": …
Notice how Python does not require the declaration of int and string while it is required in Java. Simply put, Java requires that all variables and their types explicitly declared. Assigning an object incorrectly will produce an exception. On the other hand, Python does not require anything to be declared and assignment statements bind a name to an object which can be of any type. Furthermore, the same name can be assigned to an object of a different type. With these simple distinctions, it is clear to see the difference between Java and Python.
In conclusion, Python and Java are both powerful object-oriented programming languages. They are similar given that they both seek to make writing code easier, but they are significantly different especially considering their language characteristics. While they have some ideological similarities, Python and Java are very different languages.