by cryptos · 9y ago · 0 comments
The hello world example is symptomatic:
public class Example {
public static void main(String ...args) {
System.out.println("Hell, World!");
}
}
vs fun main(args: Array<String>) {
println("Hello, World!")
}
Some more examples: // java
for (Element element : collection)
// kotlin
for (element in collection)
// java
(OtherType) somehting;
// kotlin
somehting as OtherType
// java
a.equals(b);
// kotlin
a == b
// java
Color color = x < 5 ? Color.RED : Color.GREEN;
// kotlin
val color = if (x < 5) Color.RED else Color.GREEN
And some more like constructors just named "constructor", init blocks are name "init", varargs are prefixed with "vararg", type checks with "is", ranges with 1..9, strings wicht embedded variables ...I TAed an intro to CS class in Python for a number of years. I am rather ambivalent toward Python as a developer, but as a first language to be taught it worked pretty well. There are others, but having the minimum amount of noise for some basic coding examples I think is incredibly helpful (though I admit, I think I'm basically just saying "I think dynamically/optionally typed scripting languages > statically, explicitly typed compiled languages for this task").
program HelloWorld;
begin
end.
I was puzzled: why would you need to write something for it to do nothing? But it becomes clear very easily: all programs need to have a special structure to be correctly defined. This structure happens to be a block of code with a name. And that explanation was enough to satisfy me for the moment. I turned a few pages, and sure enough all examples had that structure with something else inside. I was already feeling good with myself for being able to recognize well structured programs.Just the way it allows you to create an inmutable class in a single line already makes it way easier to read, while also saving time.
Compare Java:
public class Person {
private final String name;
private final int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
With Kotlin: data class Person(val name: String, val age: Int)