Kotlin's improvements to Java


Kotlin has summarized the results of investigating what Java has improved, focusing on I'm not good at Java, including why Java has become such a language specification.


・ I like Java, but I haven't used Kotlin at this time. (So the content may be thin) ・ What kind of language Kotlin is, grammar, etc. are mentioned a lot elsewhere, so I will not mention it here.


Isn't Java the most believer language among server-side programmers? Thought is sublime and does not accept beginners. Unlike other languages that have become object-oriented in the middle, they do not allow strange loopholes. Designing in this language is irresistible for engineers who like to think logically, right? (Lol)

[Background of investigation]

However, there are many people who don't like Java because it has its drawbacks. And, after all, very few people want to use it privately (crying) So, at the study session I'm doing, I checked if there were any people using Java privately, and as expected, even one out of 15 people didn't use Java (crying). However, there were three people using Kotlin. From there, I became interested in Kotlin and decided to investigate.

Main subject

First of all, Java is a nice place for me, but it looks like this.

  1. Culture to write all Getter / Setter
  2. Inspection exception
  1. NullPointerException
  2. Mixed primitive and object types

How have these been improved! ??

1. Culture to write all Getters / Setters

[why? ]

First, you need to know why Java writes all getters / setters. I didn't understand it very clearly, but when I looked it up, there were various opinions. ・ To hide member variables and make them safe -To make it possible to monitor changes to member variables (make room for processing) I wasn't convinced by these, and it seemed that I could define it in public, but this is the correct answer that I finally arrived at. -Member variables cannot be overridden in subclasses! So wrap it in a method and override that method if you want the subclass to change its behavior.


This is now a property in C #, ruby, swift. So you don't have to write Getter / Setter. Properties can be overridden in subclasses. You can also abstract and force implementation in subclasses.

2. checked exception

[what? ]

It seems that it is the only method adopted only in Java. Exceptions thrown by a method must be explicit as part of the method's syntax, and the caller must handle it or throw it above.

public FileInputStream(String name)
                throws FileNotFoundException

This has the following benefits:

Many languages and mechanisms treat exceptions as run-time, and you don't know what exceptions will occur until they actually occur, and whether they can occur from this process in the first place. Especially in the case of Java, since you can understand the types of exceptions, you can expect a lot of exception recovery. [Source: Rethinking inspection exceptions](http://qiita.com/Kokudori/items/0fe9181d8eec8d933c98#java%E3%81%AE%E6%A4%9C%E6%9F%BB%E4%BE%8B%E5 % A4% 96)

However, this is now said to be a mistake and has not been incorporated into other object-oriented languages that represent C #.

The meaningless catch clause is mass-produced and actually deteriorates the quality of the program. Source: The Scalability of Checked Exceptions


Checked exceptions are gone and caller handling of exceptions is no longer enforced.

  1. NullPointerException

[what? ]

Occurs if you use variables before the instance is created.

//Run-time error for Java
String s = null;
s.length(); // java.lang.NullPointerException


This can't be helped, right? I thought, but modern languages have a mechanism to secure this. Reference: null unsafe languages are no longer legacy languages

In Kotlin, if it can be Null, it will be specified at the time of declaration, and if it can be Null, it will result in a compile error unless it is checked first.

//Variable?Null can be set in the attached declaration.?Null setting is not possible without it.
val s: String? = null  
s.length //Compile error: only safe (?.) or non-null

//OK writing
val s:String? = null
if (s != null){
    s.length // OK

4. Mixed primitive type and object type

As you know, Java has eight primitive types (boolean, char, byte, short, int, long, float, double), which have wrapper classes for object types.

[why? ]

Why are there variables of primitive type? Java has taken many of its ideas from C ++. Primitive type variables are one of them. And, by incorporating this, I was happy that I aimed at the influx of existing C ++ programmers. In addition, primitive types seem to have the advantage of high processing speed. Reference: Primitive Types Considered Harmful

Reference source: Unknown defect of Java


-It is necessary to properly use the processing while being aware of whether the variable being handled is a primitive type or an object type. ⇒ [Example] Primitive types can be compared with ==, object types use .equals (), etc. -In order to pass a variable of primitive type to a method that takes an object type as an argument, it is necessary to convert it to an object type once, which wastes memory.


There are no primitive types in Kotlin. (However, it can be said that some of the convenience of primitive types has been lost.)

That's all, but for the time being, I had the impression that the parts that I personally couldn't say were almost improved.

If I have Java, I would love to use Kotlin.

Recommended Posts

Kotlin's improvements to Java
[Java] Introduction to Java
Introduction to java
Kotlin's Null safety to send to Java developers
Changes from Java 8 to Java 11
Sum from Java_1 to 100
[Java] Connect to MySQL
From Java to Ruby !!
Introduction to java command
[Java] How to use Map
How to lower java version
Migration from Cobol to JAVA
How to uninstall Java 8 (Mac)
Java --How to make JTable
How to use java Optional
New features from Java7 to Java8
How to minimize Java images
How to write java comments
How to use java class
[Java] How to use Optional ②
Connect from Java to PostgreSQL
[Java] How to use removeAll ()
[Java] How to display Wingdings
[Java] Introduction to lambda expressions
[Java] How to use string.format
Shell to kill Java process
How to use Java Map
How to set Java constants
Connect to DB with Java
Connect to MySQL 8 with Java
[java] Reasons to use static
How to use Java variables
[Java] Introduction to Stream API
Java8 to start now ~ Optional ~
How to convert Java radix
[Java] Convert ArrayList to array
Java thread to understand loosely
[Java] How to implement multithreading
[Java] How to use Optional ①
From Ineffective Java to Effective Java
How to initialize Java array
[Introduction to rock-paper-scissors games] Java
Input to the Java console
Add multi-letter watermark to Java Word
How to study Java Silver SE 8
How to use Java HttpClient (Get)
Call Kotlin's sealed class from Java
Operation to connect multiple Streams @Java
Java to learn with ramen [Part 1]
Studying Java # 6 (How to write blocks)
[java8] To understand the Stream API
Multithreaded to fit in [Java] template
[Java] Points to note with Arrays.asList ()
[Introduction to Java] About Stream API
Java to extract PDF text content
How to make a Java container
Road to Java SE 11 Silver acquisition
How to disassemble Java class files
Java adds SmartArt graphics to PowerPoint
Kotlin Class to send to Java developers
How to use Java HttpClient (Post)