Python is an interpreted language, often described as a ‘scripting language’. Python evolved origins of improving the teaching and prototyping language ABC and being a language ideal for the programmers own projects, to now being a language used in large scale web servers, AI and other substantial projects.
Kotlin is a compiled language, targeted from the outset as Java replacement for robust development of complex applications. Kotlin now has become a leading language for mobile development and with strong native code support, a viable alternative to C or Rust for native code projects.
While both languages now have features that allow them to expand beyond their origins, it is understanding these origins and the thinking behind them that give best understanding of their basic differences. The two languages start from almost opposite sides of the ‘static compiler vs dynamic interpreter’ worlds, but each have adopted concepts from the other side of the debate, and both to some degree straddle middle ground. But the bias of their respective starting points still shows.
The topics covered here are:
- language origin
- installing
- Run kotlin with Java
- kotlin reads your code
- Hello World
- comments and quotes
- variables and types
Language Origin.
The origins of python date back to 1989, but the early versions circulated between very few people. Python 2 was releases in 2000 and as you may gather from the fact that no one ever speaks today of ‘Python1’ that before python 2 distribution was quite limited. This allow Python to evolve prior to being locked into a large user base and is generally seen as a good thing. Python started very much as an interpreted ‘scripting language’, but clearly has become a general purpose programming language. Python was invented by Guido van Rostrum who is still the BDFL (benevolent dictator for life) but is developed by a strong open source community. Guido named ‘Python’ after the comedy troop ‘monty python’ which suggest the language has a fun aspect.
The origins of kotlin are that it started life with a ‘we need something better than Java’ brief back in 2010 and was influenced by the need for the language to produce code that runs on the JVM (java virtual machine). Version 1.0 was released in February 2016. Jetbrains, the team behind kotlin, had a huge codebase that could really benefit from a better language. After trying Scala, they then “cherry picked’ the best language features they could find. Java was named after a large tropical island half way around the world from the Java developers, a tropical island famous for growing coffee. To follow that, the kotlin team decided to also choose an island, and they chose a cold island in the Baltic sea, famous for a class of warship being named after it, and the closest island to their offices. They may lack humour and imagination, or perhaps they love irony. Which, only time will tell. Perhaps it is a good time to review the page ‘why kotlin?‘
Installing
To run python programs, the first step is to install python from python.org. This also gives you a very basic development tool in the form of ‘idle’. You can run programs directly from that tool, but once the program is developed, it can be run on any computer where python is installed. Just copy the source file to the other computer. In reality, if continuing to develop with python, a better ide is advised.
For kotlin, best to first download a version of JDK. Then download Intelij from jetbrains.
See here for getting a first app running.
Run Kotlin with the Java JVM.
There are tutorials here and other places to get your first kotlin app running. With the first app, you will normally first run it inside Intelij, but how to run that program without the IDE? With python, while sharing the program as anything but a sources file is more difficult, python installations are common or even standard on mac and linus, and to install python and then run the app from command line is relatively simple. This means to share the python.py file(s) to another computer is often all that is needed. To run the kotlin program from the command line, you need to first use kotlin to produce a ‘class’ or ‘jar’ file, then run that file using the java command which runs the program using the JVM. I will add more instructions here later…if you need such instructions, please leave a comment.
Kolin only reads code, Python runs Code.
Python, as follows from the scripting orgin, runs each line of you program as it reads that line. Your program runs as if it was in ‘idle’ the repl. From top to bottom of the file, each line is executed immediately as it is read. Note, if there is a ‘def’, the instruction is to define the function, not run it, and the same applies with class. Statements outside a ‘def’ or ‘class’ happen immediately. This is the same even if you import a second file….. any code in that second file outside of ‘def’ statements, will be run immediately.
If a file has the line “a = 5” in the file, even if you just import the file, ‘a’ will be set to ‘5’ immediately when python reaches that line.
Kotlin executes none of the code, but instead reads the code and produces and equivalent JVM/JavaScript or binary program. You could consider that “all the code is inside ‘fun’ statements so it is like python with ‘def’ statements, but it goes further than that. With kotlin there is a complete ‘compile all code’ phase, and then a separate ‘run’ at the end.
A different hello world.
The first difference from the python ‘hello world’ is that the code for kotlin must be inside a ‘fun’ called ‘main. Think of this as the equivalent of:
if __name__ == "__main__": print("hello world") #// equivalent to fun main(args : Array) { println("hello world") }
In python, the ‘if’ is needed to stop the code running if not main, in kotlin the ‘fun main’ is needed to say the code is ‘main’.
Quite obvious the comment in kotlin is /* to start and */ to end, with ‘//’ the equivalent of ‘#’
The equivalent to the docstring is a comment, not a string, and the comment starts with /** …for more see here
In python you can use ‘single’ quote strings, or “double” quote strings, and “””triple quote”” for longer strings or r”raw strings” and from python 3.6) f”format strings”.
In kotlin, single quotes are not strings they are single character literals, which have no python equivalents. All “regular” double quote strings are format strings, “””triple quotes””” are both the equivalent of the python “”” and the python r”raw” string, but also still allow templates. see here for more
next : Variables and Types
This is most informative and also this post most user friendly and super navigation to all posts… Thank you so much for giving this information to me..
LikeLike