Ultimate Syntax CheatSheet for Kotlin, Python, Java and C++
Disclaimer: This is a work in progress. But it is being updated regularly.
This is a cheatsheet to make things easy for people who are coding in multiple languages in different projects at the same time, for whatever reasons.
1. Printing Stuff
Python
print("some stuff")
Java
public static void main(String[] args){
System.out.println("Some Stuff");
}
Kotlin
fun main(){
println("Some Stuff")
}
C++
#include <iostream>
using namespace std;
int main(){
cout << "Some Stuff";
return 0;
}
From this point on I’ll be skipping the formalities of these programming languages like importing iostream in C++ or declaring main function in Kotlin.
2. Taking Input (and variables)
Python
x = input("Enter a value: ")
Java
import java.util.Scanner
.
.
.
Scanner sc = new Scanner(System.in);
int a = sc.nextInt() // inputing integers
String str = sc.nextLine(); // inputing string values
.
.
.
Kotlin
import java.util.Scanner
.
.
val text_input = Scanner(System.'in')
var age = read.nextInt()
val
is different from var
in Kotlin. The difference is that val
is a constant variable and can’t be assigned multiple values, it is immutable. var
is mutable and can be assigned multiple values.
As you might have noticed, Kotlin borrows the Scanner
function from it’s parent programming language Java.
C++
int x;
cin >> x;
3. Arrays and the For loop
Python
list1 = [1, 3, 4, 5]
for i in list:
print(i)
One dimensional Arrays are called lists in Python.
Java
int first_array[] = new int[6] // declaring a 1-D array with 6 elements
// initializing the array using a for loop
for (int i=0; i<6; i++){
a[i] = 0; // this assigns every element in the array a zero value
}
Kotlin
val integers = intArrayOf(1,2,3,4)
for (i in integers){
println(i)
}
C++
string fang[4] = {"Facebook", "Amazon", "Netflix", "Google"}
for (int i = 0; i<4; i++){
cout << fang[i] << "\n";
}
4. Multidimensional Arrays
Python
arr = [ [0,9,7,6],
[1,2,3,4],
[3,5,7,9]
]
for i in range(len(a)):
for j in range(len(a[i])):
print(a[i][j], end = " ")
print() // line break
Java
int[][] marr = new int[4][3];
We can take the input for the values of the array using Scaner. Or do something like this:
int [][] marr = {{1,2}, {3,4}, {5,6}};
Kotlin
val marr = arrayOf(
arrayOf(1,2),
arrayOf(3,4),
arrayOf(5,6)
)
Here’s another way to declare and intialise multidimensional arrays in Kotlin:
var m = Array(6,{i -> Array(5, {j -> 0})}) // a 6x5 Int array intialise all to 0
C++
int x[3][4] = {{0,1,2,3}, {4,5,6,7}, {8,9,10,11}};