JavaPython

Allikas: Kursused
Mine navigeerimisribale Mine otsikasti
Java vs Python

LEHEKÜLG ON TÄIENDAMISEL!!!

Sissejuhatus

Selle lehekülje eesmärgiks on võrrelda erinevaid komponente Java ja Pythoni programmeerimiskeelte vahel. Kuigi antud leht on mõeldud Tallinna Tehnikaülikooli informaatikaeriala tudengitele õppevahendiks, sobib seda uurida ka kõigil, kes tahavad võrrelda kahte programmeerimiskeelt. Igal alamleheküljel on toodud võrdlusena komponente nii Java kui ka Pythoni keeles.

Küsimuste korral pöörduda iti0011@cs.ttu.ee

Python-vs-java.jpg

Põhilised erinevused ja sarnasused

Java Python
Trükkimine
In Java, all variable names (along with their types) must be explicitly declared. Attempting to assign an object of the wrong type to a variable name triggers a type exception.That’s what it means to say that Java is a statically typed language.

Java container objects (e.g. Vector and ArrayList) hold objects of the generic type Object, but cannot hold primitives such as int. To store an int in a Vector, you must first convert the int to an Integer. When you retrieve an object from a container, it doesn’t remember its type, and must be explicitly cast to the desired type.

In Python, you never declare anything. An assignment statement binds a name to an object, and the object can be of any type. If a name is assigned to an object of one type, it may later be assigned to an object of a different type. That’s what it means to say that Python is a dynamically typed language.

Python container objects (e.g. lists and dictionaries) can hold objects of any type, including numbers and lists. When you retrieve an object from a container, it remembers its type, so no casting is required.

Koodi pikkus
abounding in words; using or containing more words than are necessary expressing much in a few words. Implies clean-cut brevity, attained by excision of the superfluous
Kompaktsus
Tabulatsioon
Javas ei ole mitte mingit vahet kas joondada koodis iga käsk eri kaugusele, kirjutada nad üksteise järele kõik ühe rea peale või lihtsalt ritta üksteise alla. Tabulatsioon on väga oluline


Näide "Hello World"

Java Python
<syntaxhighlight lang="java" line="1" >

public class HelloWorld {

   public static void main (String[] args)
   {
       System.out.println("Hello world!");
   }

} </syntaxhighlight>

<syntaxhighlight lang="python" line="2" >

print("Hello, world!") # Python version 3 </syntaxhighlight>