Object-Oriented Python vs Object-Oriented Java Codes Comparison

A Python Class

As is the case with Python in general, the Python OOP paradigm is pretty concise, as the simple class in Listing 1 illustrates.

Listing 1 — A Python class.

class Student:
def __init__(self, name, age, major):
self.name = name
self.age = age
self.major = major
def is_old(self):
return self.age > 100

An Equivalent Java Class

Not to be outdone by our Python coding effort, Listing 2 shows an equivalent Java class.

Listing 2 — A Java student class.

public class Student {        String name;
int age;
String major;
public Student() {
// TODO Auto-generated constructor stub
}
public Student(String name, int age, String major) {
this.name = name;
this.age = age;
this.major = major;
}
}

A Matter of Speed: Python Versus Java Code

Speed is always an issue. Let’s modify Listing 1 so that we can get a feel for the speed of the underlying code.

Listing 3 — A timed program run.

import timeclass Student:
def __init__(self, name, age, major):
self.name = name
self.age = age
self.major = major
def is_old(self):
return self.age > 100
start = time.clock()for x in xrange(500000):
s = Student('John', 23, 'Physics')
print 'Student %s is %s years old and is studying %s' %(s.name, s.age, s.major)
print 'Student is old? %d ' %(s.is_old())
stop = time.clock()
print stop - start
  1. Import the time module.
  2. Create a time snapshot at the beginning of the program.
  3. Instantiate a large number of Student objects.
  4. Access the data inside each object.
  5. Take a time snapshot and subtract the original time.
  6. Display the time required to run the program.
Student John is 23 years old and is studying Physics
Student is old? 0
29.8887370933
Student John is 23 years old and is studying Physics
Student is old? 0
48.2298926572
Student John is 23 years old and is studying Physics
Student is old? 0
97.3272409408

Listing 4 — The Java test program.

public class Student {        String name;
int age;
String major;
public Student() {
// TODO Auto-generated constructor stub
}
public Student(String name, int age, String major) {
this.name = name;
this.age = age;
this.major = major;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
for (int i = 0; i < 500000; i++) {
Student student = new Student("John", 23, "Physics");
System.out.println("Student " + student.getName() + " is "
+ student.getAge() + " years old and is studying " + student.getMajor());
}
long estimatedTime = System.currentTimeMillis() - startTime;
System.out.println("Time estimate: " + estimatedTime/1000);
}
}
Student John is 23 years old and is studying Physics
Student is old: false
Time estimate: 31
Student John is 23 years old and is studying Physics
Student is old: false
Time estimate: 50
Student John is 23 years old and is studying Physics
Student is old: false
Time estimate: 104

Comparative Speed Test

Comparative Speed Test of Similar Python and Java Codes
  • Portability. This simply means that Java bytecode will run on any platform with an appropriate Java virtual machine (JVM).
  • Type safety. As the example illustrates, type safety is closely related to memory safety. This language feature helps to avoid situations in which an attempt is made to copy an invalid bit pattern into a given memory area.
  • Built-in security. The Java security model is based on a sandbox in which code can run safely with minimal negative effects on the underlying platform.

Final Thoughts

OOP languages were the subject of a certain amount of mistrust back in the 1990s. In those days, many organizations preferred to stick with mainstream languages such as C, rather than adopting the new C++. Then along came Java, and I think it’s fair to say that C++ was no longer the de facto OOP language.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Shubham

Shubham

Enthusiast...Inquisitive minded … Ever-Learner...Dreamer…Innovator…Persevering…Programmer…Creator