Python and JS infer types at runtime; Go, Java, C and C++ are statically typed (written explicitly or inferred by the compiler).
Python
n = 42
name = "Ola"
print(n)
print(name)
JavaScript
const n = 42;
const name = "Ola";
console.log(n);
console.log(name);
Go
package main
import "fmt"
func main() {
n := 42
name := "Ola"
fmt.Println(n)
fmt.Println(name)
}
Java
public class Main {
public static void main(String[] args) {
int n = 42;
String name = "Ola";
System.out.println(n);
System.out.println(name);
}
}
C
#include <stdio.h>
int main(void) {
int n = 42;
const char *name = "Ola";
printf("%d\n", n);
printf("%s\n", name);
return 0;
}
C++
#include <iostream>
#include <string>
int main() {
int n = 42;
std::string name = "Ola";
std::cout << n << "\n";
std::cout << name << "\n";
return 0;
}
Python marks blocks with indentation, the others use braces; PHP variables start with $.
Python
x = 7
if x % 2 == 0:
print("even")
else:
print("odd")
JavaScript
const x = 7;
if (x % 2 === 0) {
console.log("even");
} else {
console.log("odd");
}
Go
package main
import "fmt"
func main() {
x := 7
if x%2 == 0 {
fmt.Println("even")
} else {
fmt.Println("odd")
}
}
Java
public class Main {
public static void main(String[] args) {
int x = 7;
if (x % 2 == 0) {
System.out.println("even");
} else {
System.out.println("odd");
}
}
}
C
#include <stdio.h>
int main(void) {
int x = 7;
if (x % 2 == 0) {
printf("even\n");
} else {
printf("odd\n");
}
return 0;
}
C++
#include <iostream>
int main() {
int x = 7;
if (x % 2 == 0) {
std::cout << "even\n";
} else {
std::cout << "odd\n";
}
return 0;
}
Go, Java, C and C++ sum with a loop; Python, JS (reduce) and PHP have a built-in way.
Python
nums = [1, 2, 3]
print(sum(nums))
JavaScript
const nums = [1, 2, 3];
console.log(nums.reduce((a, b) => a + b, 0));
Go
package main
import "fmt"
func main() {
nums := []int{1, 2, 3}
total := 0
for _, v := range nums {
total += v
}
fmt.Println(total)
}
Java
public class Main {
public static void main(String[] args) {
int[] nums = {1, 2, 3};
int total = 0;
for (int v : nums) {
total += v;
}
System.out.println(total);
}
}
C
#include <stdio.h>
int main(void) {
int nums[3] = {1, 2, 3};
int total = 0;
for (int i = 0; i < 3; i++) {
total += nums[i];
}
printf("%d\n", total);
return 0;
}
C and C++ uppercase character by character; the other languages have a ready method or function.
Python
s = "Iteris"
print(len(s))
print(s.upper())
JavaScript
const s = "Iteris";
console.log(s.length);
console.log(s.toUpperCase());
Go
package main
import (
"fmt"
"strings"
)
func main() {
s := "Iteris"
fmt.Println(len(s))
fmt.Println(strings.ToUpper(s))
}
Java
public class Main {
public static void main(String[] args) {
String s = "Iteris";
System.out.println(s.length());
System.out.println(s.toUpperCase());
}
}
C has no built-in dictionary: this is the closest honest equivalent, searching an array of key/value pairs with strcmp.
Python
d = {"city": "Kyiv"}
print(d["city"])
JavaScript
const d = { city: "Kyiv" };
console.log(d["city"]);
Go
package main
import "fmt"
func main() {
d := map[string]string{"city": "Kyiv"}
fmt.Println(d["city"])
}
Java
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Map<String, String> d = new HashMap<>();
d.put("city", "Kyiv");
System.out.println(d.get("city"));
}
}
C
#include <stdio.h>
#include <string.h>
struct Pair {
const char *key;
const char *value;
};
int main(void) {
struct Pair d[] = {{"city", "Kyiv"}};
for (int i = 0; i < 1; i++) {
if (strcmp(d[i].key, "city") == 0) {
printf("%s\n", d[i].value);
}
}
return 0;
}
<?php
class Person {
public function __construct(public string $name) {}
public function greet(): string {
return "Hello, " . $this->name;
}
}
echo (new Person("Ola"))->greet() . "\n";
Output:Hello, Ola
try / catch: division by zero
Go and C have no try/catch (Go returns an error, C checks a condition); in JS and C++ division by zero does not throw, so the exception is thrown by hand.
try {
const b = 0;
if (b === 0) throw new Error("division by zero");
console.log(1 / b);
} catch (e) {
console.log("error");
}
Go
package main
import (
"errors"
"fmt"
)
func div(a, b int) (int, error) {
if b == 0 {
return 0, errors.New("division by zero")
}
return a / b, nil
}
func main() {
if _, err := div(1, 0); err != nil {
fmt.Println("error")
}
}
Java
public class Main {
public static void main(String[] args) {
try {
int b = 0;
System.out.println(1 / b);
} catch (ArithmeticException e) {
System.out.println("error");
}
}
}
C
#include <stdio.h>
int main(void) {
int b = 0;
if (b == 0) {
printf("error\n");
} else {
printf("%d\n", 1 / b);
}
return 0;
}
C++
#include <iostream>
#include <stdexcept>
int main() {
try {
int b = 0;
if (b == 0) throw std::runtime_error("division by zero");
std::cout << 1 / b << "\n";
} catch (const std::exception &e) {
std::cout << "error\n";
}
return 0;
}