I’m now learing about TypeScript, and I thought it would be useful to compare it with Java and Go. Below is a table that summarises some of the key features and syntax differences between these three languages.
Feature |
TypeScript |
Java |
Go |
Variable Declarations |
let count: number = 10;
const name: string = "Alice";
var isActive: boolean = true;
|
int count = 10;
String name = "Alice";
boolean isActive = true;
|
var count int = 10
name := "Alice"
isActive := true
|
Conditional Branching |
if (x > 10) {
console.log("Greater than 10");
} else if (x === 10) {
console.log("Equal to 10");
} else {
console.log("Less than 10");
}
|
if (x > 10) {
System.out.println("Greater than 10");
} else if (x == 10) {
System.out.println("Equal to 10");
} else {
System.out.println("Less than 10");
}
|
if x > 10 {
fmt.Println("Greater than 10")
} else if x == 10 {
fmt.Println("Equal to 10")
} else {
fmt.Println("Less than 10")
}
|
Loops |
for (let i = 0; i < 5; i++) {
console.log(i);
}
const items = [1, 2, 3];
for (const item of items) {
console.log(item);
}
|
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
int[] items = {1, 2, 3};
for (int item : items) {
System.out.println(item);
}
|
for i := 0; i < 5; i++ {
fmt.Println(i)
}
items := []int{1, 2, 3}
for _, item := range items {
fmt.Println(item)
}
|
Classes |
class Person {
name: string;
constructor(name: string) {
this.name = name;
}
greet(): string {
return `Hello, ${this.name}`;
}
}
|
public class Person {
private String name;
public Person(String name) {
this.name = name;
}
public String greet() {
return "Hello, " + name;
}
}
|
// Go does not have classes, but we can simulate them with structs and methods.
type Person struct {
Name string
}
func (p Person) Greet() string {
return "Hello, " + p.Name
}
|
Function Definitions |
function add(a: number, b: number): number {
return a + b;
}
// Arrow function
const multiply = (a: number, b: number): number => a * b;
|
public int add(int a, int b) {
return a + b;
}
|
func add(a int, b int) int {
return a + b
}
|
Structs |
// TypeScript uses interfaces or type aliases to define object shapes.
interface Point {
x: number;
y: number;
}
const pt: Point = { x: 10, y: 20 };
|
// Java does not have structs; a simple class can serve a similar purpose.
public class Point {
public int x;
public int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
|
type Point struct {
X int
Y int
}
|
Enums |
enum Color {
Red,
Green,
Blue
}
let c: Color = Color.Green;
|
public enum Color {
RED, GREEN, BLUE;
}
Color c = Color.GREEN;
|
// Go does not have enums in the same way; use constants with iota.
type Color int
const (
Red Color = iota
Green
Blue
)
|