[JAVA] Why Kotlin is so useful

Background of this post

https://qiita.com/ngsmvn/items/b74328291d0b5342e407

For those who are too long and don't want to read, each section has a summary, so you can understand the purpose by itself.

Why Kotlin is so useful

To summarize why Kotlin is useful, I think it's because it's ** good at other languages **.

A book written by the Kotlin developer called Kotlin in Action says:

We aren’t trying to advance the state of the art in programming language design and explore innovative ideas in computer science. Instead, whenever possible, we’re relying on features and solutions that have already appeared in other programming languages and have proven to be successful.

(My translation)

We are not trying to raise the bar in programming languages or explore innovative ideas in computer science. Instead, we rely as much as possible on features and solutions that are already in other programming languages and that we know will work.

You can see that it is practical and that it is actively trying to incorporate the good points of existing programming languages.

Specific useful points

I think there are many other things, but I will focus on the following three points.

  1. "Flexible" and "concise" despite being statically typed
  2. Referential transparency taken from functional languages
  3. Since it runs on the JVM (described later), "write once, run everywhere"

"Conciseness" and "flexibility" of dynamically typed languages

Summary of this section: Statically typed languages give a lot of information about your program to your computer, so you can quickly find mistakes in your program. It's annoying to write instead. Kotlin is a statically typed language that is simple and flexible to write

Have you ever made a typo if you thought a program written in JavaScript or PHP wouldn't work? Simple mistakes would be easier if your computer could find them.

A language that finds mistakes such as spelling mistakes and different types of data before execution is called a statically typed language. Conversely, JavaScript and PHP are called dynamically typed languages. The two differ in when the type (data type) is determined.

--Dynamic → Determined when the program is executed --Data inconsistencies cannot be found before execution --Static → Determined before running the program --Data inconsistencies can be found before execution

If you find a mistake, you should always use a statically typed language, right? But that's not the case. Let's compare the programs that output character strings.

--JavaScript (dynamic typing)

alert("JavaScript");

--PHP (dynamic typing)

echo "PHP";

--Java (static typing)

public class Main {
    public static void main(String[] args){
        System.out.println("Java");
    }
}

You just need to write this much, even though you just want to display the string. Each statically typed language and dynamically typed language has its advantages and disadvantages.

コメント 2019-12-03 154004.png

It would be great if there was a statically typed language that could be written concisely and flexibly. That means Kotlin is the strongest.

fun main() = println("Kotlin") //It is statically typed but can be written concisely!!

As a result of learning from the mistakes of other programming languages and learning the good points, I am able to write as concisely as existing dynamically typed languages.

Kotlin has a lot of features to write concisely and flexibly. ** I can't show you all, so please google **

There is a good article on Qiita. (Throwing)

--Type inference --Extension function --Local function --Scope function --Infix function (infix) --Local return --Default argument --Named arguments --Data class --Processing on lists (sumBy, groupBy, fold, first, drop ...) --Higher-order function

Functional language "referential transparency"

Summary of this section: When various values change, it is hard to think more. Kotlin, which imitates functional languages and their mechanisms, can be written in a way that values cannot be rewritten.

Referential transparency is the property that if the inputs are the same, the result of executing the function will be the same. Did you take it for granted? Now consider the following example.

Suppose you put a 1000 yen bill in a vending machine and press a 100 yen coffee button. Are the products and changes that come out always the same? 自販機

If it's sold out, coffee won't come out, and the change will be 500-yen coins and 100-yen coins, or all 100-yen coins. If you replace this example with a program

--Insert a 1000 yen bill → enter --Press the button → Call the function --Changes and products that come out → Output -** Products and coins in the vending machine → Condition **

What I wanted to convey in this example is that ** if the state changes, the output will change **. It is troublesome to program while thinking about each state. In the first place, if the state does not change, there is no need to think about this.

So, in a pure functional language, referential transparency is achieved by not rewriting the state of the object.

For example, Haskell can't do this. (It's a pseudo code, so it's not a proper Haskell.)

--List of programming languages available
let skillSet = ["JavaScript", "PHP"]
--Since I participated in the study session, I can use Kotlin.
skillSet push "Kotlin" --← Cannot be added to the list
for (let i = 0; i < skillSet size; i++){ --← Loop variable cannot be incremented by 1
    --Proud of a programming language you can use
    putStrLn skillSet !! i ++ "Can be used!!"
}
--I didn't write the code for a while, so I forgot everything
skillSet = [] --← Cannot be reassigned. Or rather, it's called binding instead of substitution
--

Kotlin partially adopts this idea to distinguish between rewritable objects (mutable objects) and non-rewritable objects (immutable objects).

--Move

//List of programming languages available
var skillSet = mutableListOf("JavaScript", "PHP") //Assign a mutable list to a variable that can be reassigned
//Since I participated in the study session, I can use Kotlin.
skillSet.add("Kotlin")
for (i in 0 until skillSet.size){
    //Proud of a programming language you can use
    println("${skillSet[i]}Can be used")
}
//I didn't write the code for a while, so I forgot everything
skillSet = mutableListOf()

--It doesn't work

//List of programming languages available
val skillSet = listOf("JavaScript", "PHP") //Assign an immutable list to a variable that cannot be reassigned
//Since I participated in the study session, I can use Kotlin.
skillSet.add("Kotlin") //← This cannot be done
for (i in 0 until skillSet.size){
    //Proud of a programming language you can use
    println("${skillSet[i]}Can be used")
}
//I didn't write the code for a while, so I forgot everything
skillSet = listOf() //← I can't do this either

In addition to referential transparency, functional languages seem to be influenced by higher-order functions and operations on lists.

Java's "write once, run everywhere"

Summary of this section: Java can be run on any OS, as does Kotlin, which mimics how Java works.

Compiler and interpreter languages

Kotlin is strongly influenced by the language Java. To explain why Kotlin is so great, we need to explain why Java is so great. I'll take a little detour, but in the end it will lead to the story of Kotlin, so please follow me.

There are two main types of programming languages. Compiler language and interpreter language.

I don't remember where I heard the metaphor, but programmers are like writing letters on a computer.

"Please show me the web page" "Please show me the value of the database" "Please send an email to that person" I think there are various requests, but I will write the request in a letter and send it to the computer. The letter is the program, and the language used to write the program is the programming language.

The computer doesn't know the programming language. The computer only knows the current ON / OFF. A computer-readable word that represents ON / OFF with the numbers 0 and 1 is called a machine language.

There are two main ways to get a computer to read a letter (= run a program).

  1. Translate all letters into machine language in advance before sending
  2. Next to the computer reading, explain the meaning line by line

The former is the compiler language for writing letters, and the latter is the interpreter language.

The compiler language has advantages such as fast execution speed and less concern that letters will be imitated by other people (because humans do not understand machine language), but there are some problems.

Your letter depends on the OS

In pre-Java compiler languages, programs were OS-dependent. Dependence on the OS means that it was necessary to develop and test each OS. What is the OS for in the first place?

In order for the computer to do the work, it must (of course) use its own hardware. Suppose you need to program your hardware in detail, like "Show files saved in this location on your disk." What if you want to buy a new computer or run your program on a friend's computer?

Of course, the computer design and parts are different, so I have to rewrite the program. It's annoying, isn't it?

The OS takes over the access to the computer hardware so that the programmer does not have to touch it directly. Thanks to the OS, there is no need to program directly to the hardware.

The contact point where the software asks "OS-kun, please access the hardware with a good feeling" is called API. Since the API is different for each OS, the program will change if the OS is different. It's easier than accessing the hardware directly, but it still has the annoyance of dealing with differences between operating systems.

The Java Virtual Machine (JVM) has solved that annoyance.

JVM

A JVM is a Java virtual machine. A virtual machine that runs as a Java execution environment. A virtual machine is software that pretends to be a computer using the hardware of a real computer. There are many types other than the JVM.

The JVM provides APIs for Java programs. This API is intentionally limited to the functions common to various OSs.

In the analogy of the above letter, writing a letter in Java replaces it with a language that understands the JVM, which is an intermediate language instead of machine language. It will be translated into machine language in a good way depending on the OS on which the JVM is installed.

Instead of the software touching the hardware directly, I was able to absorb the difference in hardware by going through the OS. In the same way, you can absorb the difference between OSs by going through the JVM instead of accessing the OS directly.

This is "write once, run everywhere" advocated by Sun Microsystems, the creator of Java. Once written, you can use the program without having to be aware of the differences in OS. This isn't annoying anymore ... it wasn't.

Java as an execution environment is excellent, but Java as a programming language has a long syntax and is very annoying.

A company in the Czech Republic, who was disgusted with "Can I make an IDE like this!", Created a modern programming language that runs on the JVM. That is Kotlin. That's the good thing about the JVM that realizes "write once, run everywhere".


That's it! I've summarized in my own words why Kotlin is so useful.

Reference material

Kotlin In Action [Why make it with Java](https://www.amazon.co.jp/Java%E3%81%A7%E3%81%AA%E3%81%9C%E3%81%A4%E3%81% 8F% E3% 82% 8B% E3% 81% AE% E3% 81% 8B-% E7% 9F% A5% E3% 81% A3% E3% 81% A6% E3% 81% 8A% E3% 81% 8D % E3% 81% 9F% E3% 81% 84Java% E3% 83% 97% E3% 83% AD% E3% 82% B0% E3% 83% A9% E3% 83% 9F% E3% 83% B3% E3 % 82% B0% E3% 81% AE% E5% 9F% BA% E7% A4% 8E% E7% 9F% A5% E8% AD% 98-% E7% B1% B3% E6% 8C% 81-% E5 % B9% B8% E5% AF% BF / dp / 4822281965)

Recommended Posts

Why Kotlin is so useful
Why preventDefault is needed
[Ruby on Rails] Understand why Set # include? Is so fast
Divide by Ruby! Why is it 0?
Why Java Vector is not used