Tools
Log in
English

Learn to code

Compare programming languages

Several languages side by side: types, classes, access, exceptions, build tools.

The differences in one table

TopicBased on Java 17Based on Python 3.12Based on PHP 8.3Based on C# 12Based on Swift 6.4
Type checksChecked at compile time. Every variable has a type (int, String, or var for locals).Decided at run time. Variables are name tags; hints such as x: int are for checkers.Decided at run time. Types on parameters, returns and properties are checked, and declare(strict_types=1) stops automatic conversion.Checked at compile time. Locals can use var, and types also say whether null is allowed (string?).Checked at compile time. Types are inferred (let x = 3), and values that may be nil have their own type, such as String?.
Making a variableDeclared with a type: int count = 0;.Created on first assignment: count = 0.Created on first assignment with a $: $count = 0;.int count = 0; or var count = 0;let count = 0 (constant) · var count = 0 (variable)
Code blocksBraces { }, with ; ending each statement.Indentation, usually four spaces.Braces { } and ; at the end of statements.Braces { } and ; · braces usually open on their own line.Braces { } · no ; at line ends and no parentheses around if conditions.
Lists and mapsList, Map and Set interfaces with ArrayList and HashMap; arrays are separate.list, dict, set and tuple are built in.One array type serves as list and map, and it is copied by value.List<T>, Dictionary<K,V>, HashSet<T> and arrays T[] · [1, 2, 3] collection expressions since C# 12.Arrays [Int], dictionaries [String: Int], Set<Int> · all value types, copied when passed.
Defining a classclass Book { } · one public class per file, named like the file.class Plant: · one file may hold many classes.class Plant { } · usually one class per file (PSR-4).class Book { } · file and class names may differ. Use record for value-only types.Reference type class Book { } and value type struct Book { } · structs usually come first.
ConstructorA method named after the class; several allowed with different parameters (overloading).One def __init__(self, ...); extra constructors use @classmethod.One __construct(); public on a parameter declares the property too (8.0).A method named after the class · since C# 12 also a primary constructor, class Book(string title).init(title: String) · structs get a memberwise init for free.
The current objectthis, never written as a parameter.self, written out as the first parameter.$this-> (in static code, self:: or static::).this, and base for the parent.self, and super for the parent.
Hiding datapublic, protected, package-private and private, enforced by the compiler.No keywords. _name is an agreement, __name is renamed.public, protected, private · readonly (8.1) · 8.4 private(set).Six levels including public, private, protected and internal · properties such as { get; private set; }.open, public, internal (default), fileprivate, private · private(set) closes only writing.
Inheritanceclass Dog extends Animal · one parent class only.class Truck(Vehicle): · several parents allowed.class Truck extends Vehicle · one parent · traits add shared code.class Manager : Employee · one parent class · overriding needs virtual and override.class Run: Workout · classes only · override is required, final stops it.
Abstract classes and contractsabstract class and interface (a class may implement many).ABC and @abstractmethod from abc, or typing.Protocol.abstract class and interface (implement many).abstract class and interface (several allowed, default methods possible).protocol plus extension defaults · structs and enums conform too. No abstract classes.
Class-level values and methodsstatic fields and methods.Variables in the class body · @staticmethod · @classmethod.static properties and methods · self::$n · late static binding with static::.static members and static class, which cannot be instantiated.static let and static func · class func when subclasses may override.
No valuenullNonenull · nullable types look like ?string.null, handled with ?. and ??.nil and optionals T? · unwrap with if let, guard let, ?. and ??.
Exceptionstry / catch / finally · checked exceptions must be declared with throws.try / except / finally · no declared exception lists.try / catch / finally · exceptions (Exception) and engine errors (Error) are separate.try / catch / finally · unlike Java, methods do not declare what they throw.throws, try, do / catch · throwing functions are marked throws.
Build and package toolsMaven (pom.xml) or Gradle (build.gradle).pyproject.toml with pip (in a venv) or uv.Composer (composer.json, PSR-4 autoloading, Packagist).The dotnet CLI · .csproj projects in a .sln · packages from NuGet.Swift Package Manager (Package.swift, swift build) · apps in Xcode.
How code runsjavac compiles to bytecode, then the JVM runs java Main.python3 main.py · the interpreter runs the source directly.php main.php, or a web server (PHP-FPM) that runs the script fresh for each request.dotnet build compiles to IL and the .NET runtime runs it (dotnet run).swiftc compiles to machine code (swift run). Xcode builds apps and runs them on a device or simulator.

The table keeps to differences you meet in object-oriented code. The topic pages of each language explain every row with a runnable example.

The same account class in each language

One class that hides the balance and opens only deposits and a read-only balance. Compare how each language hides it and how long the code is.

Java Main.java

class Account {    private final String owner;    private int balance;     Account(String owner) {        this.owner = owner;    }     public void deposit(int amount) {        if (amount > 0) {            balance += amount;        }    }     public String getOwner() {        return owner;    }     public int getBalance() {        return balance;    }} public class Main {    public static void main(String[] args) {        Account acc = new Account("Mina");        acc.deposit(500);        acc.deposit(-90);        System.out.println(acc.getOwner() + " " + acc.getBalance());    }}

Output

Mina 500

Using a private field from another class does not even compile. Reading goes through getter methods.

Python main.py

class Account:    def __init__(self, owner):        self.owner = owner        self._balance = 0     @property    def balance(self):        return self._balance     def deposit(self, amount):        if amount > 0:            self._balance += amount  acc = Account("Mina")acc.deposit(500)acc.deposit(-90)print(acc.owner, acc.balance)

Output

Mina 500

_balance is hidden by agreement and opened read-only through @property. Python does not stop outside code from touching _balance.

PHP main.php

<?phpdeclare(strict_types=1); final class Account{    private int $balance = 0;     public function __construct(public readonly string $owner)    {    }     public function balance(): int    {        return $this->balance;    }     public function deposit(int $amount): void    {        if ($amount > 0) {            $this->balance += $amount;        }    }} $acc = new Account('Mina');$acc->deposit(500);$acc->deposit(-90);echo $acc->owner, ' ', $acc->balance(), "\n";

Output

Mina 500

The balance is locked with private and opened for reading through balance(). The owner is public readonly: readable from outside, never changeable.

C# Program.cs

var acc = new Account("Mina");acc.Deposit(500);acc.Deposit(-90);Console.WriteLine($"{acc.Owner} {acc.Balance}"); class Account{    public string Owner { get; }    public int Balance { get; private set; }     public Account(string owner)    {        Owner = owner;    }     public void Deposit(int amount)    {        if (amount > 0)        {            Balance += amount;        }    }}

Output

Mina 500

Balance is open for reading only; acc.Balance = 0; from outside does not compile. One property replaces a field plus a getter method.

Swift main.swift

let acc = Account(owner: "Mina")acc.deposit(500)acc.deposit(-90)print("\(acc.owner) \(acc.balance)") final class Account {    let owner: String    private(set) var balance = 0     init(owner: String) {        self.owner = owner    }     func deposit(_ amount: Int) {        if amount > 0 {            balance += amount        }    }}

Output

Mina 500

One private(set) keeps reading open and closes writing; acc.balance = 0 from outside does not compile. The owner is a let, fixed after the first value.

Questions people ask

Which language should I learn first?

Python if you want quick results from short code, PHP if you want to build web pages right away, Java or C# if you want to practise strict types and larger program structure, Swift if you want to build iPhone and Mac apps. The object-oriented ideas are shared, so one makes the others easier.

Will more languages join the table?

A column appears automatically when a language gets its pages. Every language with pages is already in the table.

Are the examples the same code in every language?

The account class below does the same job in each. Topic pages use deliberately different stories per language, so reading several still teaches something new.