2. Difference between Java and Kotlin.
What Java has that Kotlin does not.
- Kotlin doesn’t have checked exceptions: It means that you can’t use the word throws in a function to make sure that when someone use this function the exception will be checked by a try-catch block.
- Primitive types: In Kotlin all are classes, including the primitive types.
- Static members: Kotlin doesn’t have static methods.
- Non-private fields: You can’t declare a field as non-private.
- Wildcard-types: Kotlin doesn’t have wildcard types. The wildcard type (?) in Java is used to set any type as a parameter in a generic type.
- Ternary-operator: Kotlin doesn’t have the ternary operator: a ? b : c
What Kotlin has that Java does not.
Here you have a list of properties that Kotlin has but Java does not. I’ll not explain them yet, but I’ll do when we see the syntax of properties.
- Lambda expressions + Inline functions = performant custom control structures.
- Extension functions.
- Null-safety.
- Smart casts.
- String templates.
- Primary constructors.
- First-class delegation.
- Type inference for variable and property types.
- Declaration-site variance & Type projections.
- Range expressions.
- Operator overloading.
- Companion objects.
- Data classes.
- Separate interfaces for read-only and mutable collections.
Here you have the list which redirect you to the topics:
https://kotlinlang.org/docs/reference/comparison-to-java.html
Issues of Java addressed in Kotlin.
- Null references are controlled by the type system. Kotlin’s type system is aimed to avoid the danger of null references, also known in java like NullPointerException (NPE).
- No raw types: Kotlin is designed to run calling code from Java in a natural way and Java can run calling code from Kotlin as well, using getters and setters in the right way, avoiding the keywords and with the safety of null references.
- Arrays in Kotlin are invariant, that has get() and set() functions.
- Kotlin has proper function types, as opposed to Java’s SAM-conversions: The functions in Kotlin are high levelled than Java.
- Use-site variance without wildcards: It is very convenient to declare a type parameter T as out and avoid trouble with subtyping on the use site.