java, kotlin, swift,

New Programming Languages Tour

Cui Cui Follow May 01, 2024 · 7 mins read
New Programming Languages Tour
Share this

Let’s learn programming in multi-lang mode. Since all languages are trying to express same things just in different grammars.

中文Python语法基础

Main method

  • ```Java
        public void main(String[] args) {
            System.out.println("Hello, World!");
        }
    ```
    
  • ```Kotlin
        fun main(args: Array<String>) {
            println("Hello, World!")
        }
    ```
    
  • ```Swift
        func main() {
            print("Hello, World!")
        }
    ```
    
  • ```Python
        def method():
            print("Hello, Main!")
        
        if __name__ == "__main__":
            method()
        elif __name__ == "module_name":
            print("Hello, Module!")
    ```
    

Data Types

  • ```Java
        // Primitive Integer Types
        byte byteNumber = 10;           // 1 byte/8-bit signed integer, range from -128 to 127
        short shortNumber = 10;         // 2 bytes/16-bit signed integer, range from -32,768 to 32,767
        int integerNumber = 10;         // 4 bytes/32-bit signed integer, range from -2^31 to 2^31-1
        long longNumber = 1000000000L;  // 8 bytes/64-bit signed integer, range from -2^63 to 2^63-1
        
        // Primitive Floating point Types
        float floatNumber = 10.0f;          // 4 bytes/32-bit floating point number, 6-7 decimal digits
        double doubleNumber = 10.0d;        // 8 bytes/64-bit floating point number, 15 decimal digits
        float scientificNotation = 35e3f;   // 35.0 x 10^3
        double scientificNotation = 12E4;   // 12.0 x 10^4
        
        // Primitive Boolean Types
        boolean isTrue = true;          // 1-bit true/false value
        
        // Primitive Character Types
        char character = 'a';           // 2 bytes/16-bit Unicode character
        char myLetter = 66;             // ASCII value for 'B'
        
        // Reference Types - String 
        String string = "Hello, World!";
    
        // Reference Types - Arrays
        List<String> list = new ArrayList<>();
        Set<String> set = new HashSet<>();
        Map<String, String> map = new HashMap<>();
        Collection<String> collection = Collections.emptyList();
    
        // Reference Types - Classes/Objects
        CustomObject customObject = new CustomObject();
    ```
    
  • ```Kotlin
        // Number Integer Types
        val byteNumber: Byte = 10               // 1 byte/8-bit signed integer, range from -128 to 127
        val shortNumber: Short = 10             // 2 bytes/16-bit signed integer, range from -32,768 to 32,767
        val integerNumber: Int = 10             // 4 bytes/32-bit signed integer, range from -2^31 to 2^31-1
        val longNumber: Long = 1_000_000_000L   // 8 bytes/64-bit signed integer, range from -2^63 to 2^63-1
        // You can miss type declaration and let Kotlin infer the type
        val myMaxInt = 2_147_483_647            // type inferred as Int
        val myLong = 2_147_483_648              // type inferred as Long
        
        // Number Floating point Types
        val floatNumber: Float = 10.0f      // 4 bytes/32-bit floating point number, 6-7 decimal digits
        val doubleNumber: Double = 10.0     // 8 bytes/64-bit floating point number, 15 decimal digits
        val myFloat = 35E3F                 // 35.0 x 10^3
        val myDouble = 12E4                 // 12.0 x 10^4
        
        // Boolean Types
        val isTrue: Boolean = true          // 1-bit true/false value
        
        // Char Types
        val character: Char = 'a'           // 2 bytes/16-bit Unicode character
        //val myLetter: Char = 66           // Error: Kotlin doesn't support ASCII values for char
        
        // Strings Types
        val string: String = "Hello, World!"
        // String length
        val length = string.length
        // Accessing characters
        val firstChar = string[0]
        // Modifying strings
        //string[0] = 'h'                   // Error: Strings are immutable in Kotlin
        
        // Arrays Types
        var intArray = intArrayOf(1, 2, 3, 4, 5)
        var longArray = longArrayOf(1L, 2L, 3L, 4L, 5L)
        var floatArray = floatArrayOf(1.1f, 2.2f, 3.3f, 4.4f, 5.5f)
        var doubleArray = doubleArrayOf(1.1, 2.2, 3.3, 4.4, 5.5)
        var booleanArray = booleanArrayOf(true, false, true)
        var charArray = charArrayOf('a', 'b', 'c')
        var stringArray = arrayOf("Hello", "World")
        // Array size
        var size = stringArray.size
        // Accessing elements
        var element = stringArray[0]
        // Modifying elements
        stringArray[0] = "Hi"
    
        // Collections Types
        val list: List<String> = listOf()
        var set: Set<String> = mutableSetOf()
        var map: Map<String, String> = mapOf("key" to "value")
    ```
    
  • ```Swift
        pputs 'hello'
    ```
    
  • ```Python
        print('hello')
    ```
    

Operators & Maths

  • ```Java
        var_dump('hello');
    ```
    
  • ```Kotlin
        console.log('hello');
    ```
    
  • ```Swift
        pputs 'hello'
    ```
    
  • ```Python
        print('hello')
    ```
    

If-Else

  • ```Java
        var_dump('hello');
    ```
    
  • ```Kotlin
        console.log('hello');
    ```
    
  • ```Swift
        pputs 'hello'
    ```
    
  • ```Python
        print('hello')
    ```
    

Switch

  • ```Java
        var_dump('hello');
    ```
    
  • ```Kotlin
        console.log('hello');
    ```
    
  • ```Swift
        pputs 'hello'
    ```
    
  • ```Python
        print('hello')
    ```
    

For Loop

  • ```Java
        var_dump('hello');
    ```
    
  • ```Kotlin
        console.log('hello');
    ```
    
  • ```Swift
        pputs 'hello'
    ```
    
  • ```Python
        print('hello')
    ```
    

While Loop

  • ```Java
        var_dump('hello');
    ```
    
  • ```Kotlin
        console.log('hello');
    ```
    
  • ```Swift
        pputs 'hello'
    ```
    
  • ```Python
        print('hello')
    ```
    

Functions/Methods

  • ```Java
        var_dump('hello');
    ```
    
  • ```Kotlin
        console.log('hello');
    ```
    
  • ```Swift
        pputs 'hello'
    ```
    
  • ```Python
        print('hello')
    ```
    

Lambda

  • ```Java
        var_dump('hello');
    ```
    
  • ```Kotlin
        console.log('hello');
    ```
    
  • ```Swift
        pputs 'hello'
    ```
    
  • ```Python
        print('hello')
    ```
    

Classes/Objects

  • ```Java
        var_dump('hello');
    ```
    
  • ```Kotlin
        console.log('hello');
    ```
    
  • ```Swift
        pputs 'hello'
    ```
    
  • ```Python
        print('hello')
    ```
    
Join Newsletter
Get the latest news right in your inbox. We never spam!
Cui
Written by Cui Follow
Hi, I am Z, the coder for cuizhanming.com!