Kotlin No Primitives



I am currently enjoying learning kotlin. I currently love that everything is an object. Nothing is a primitive. I never liked having a primitive type and a reference type for the same concept in the language(e.g. int and Integer). I also really dislike the confusion that == versus the equals operator causes. I understand the difference, as well as the need for them. I don’t think this was a well thought out part of the language. The number of bugs over(== v. equals) has likely caused is too damn high.

Equality in programming is such a commonly used operator, why allow it to be complicated.

This got me thinking. Why are there primitive types in the first place. A quick google didn’t yield a direct answer, but a similar question on stack overflow gave me the probable reason. The question was “Why do people still use primitive types in Java?”

The proposed answer from Effective Java (2nd Edition) was for efficiency reasons.

public static void main(String[] args) {
    Long sum = 0L; // uses Long, not long
    for(long i = 0; i <= Integer.MAX_VALUE; i++) {
        sum += i;
    }
    System.out.println(sum);
}

public static void main(String[] args) {
    long sum = 0L; // uses long
    for(long i = 0; i <= Integer.MAX_VALUE; i++) {
        sum += i;
    }
    System.out.println(sum);
}

In these two code samples the first one reportedly takes 43 seconds to run, and the second takes 8.

Leave a Reply

Your email address will not be published. Required fields are marked *