Python vs. Java

Python vs. Java

Discuss in your groups: what are the main differences between Java and Python?

Exercise

Translate the Python code below to Java

class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age

  def __str__(self):
    return "{} is {} years old".format(self.name, self.age)
  
  def print_person(self):
    print(self)
    
if __name__ == "__main__":
    p1 = Person("Adriana", 43)
    print(p1.name)
    print(p1.age)
    print(p1)
    p1.print_person()
    p1.age += 1
    print(p1)

Java vs. Python

Any other differences you’ve noticed between Java and Python?

Inheritance

Translate the following Python code into Java

import person

class Student(person.Person):

    def set_id(self, id):
        self.id = id

    def get_id(self):
        return self.id
    
    def __str__(self):
        return "{} is {} years old, id {}".format(self.name, self.age, self.id)

Quiz 8

current time

You have 15 minutes.

Survey

bit.ly/ua-ta-students

Arrays

Translate the following Python code into Java

class People:

    def __init__(self):
        self.people = []

    def add_person(self, person):
        self.people.append(person)

Java vs. Python

What other differences have you noticed between Java and Python?

Summary of differences

  • Python is an interpreted language (executes code line by line)

  • Java is a compiled language that compiles code into bytecode

  • Python is dynamically typed

  • Java is a static-typed programming language

  • Java supports multithreading and concurrent programming

  • Java makes use of encapsulation and inheritance more easily

  • both use automatic garbage collection to manage memory