Kotlin vs Java

In this post, I compare Kotlin and Java and explain why in my opinion Kotlin is the way forward.

Feature Java Kotlin
Checked exceptions Yes No
Primitive types Yes No
Static members Yes No
Package-private access level modifiers Yes No
Big community Yes No
Resources (Books, posts..) Yes No
Functions with default and named parameters No Yes
Extension functions No Yes
Operator overloading No Yes
Primary constructors No Yes
Singletons No Yes
Range expressions No Yes
Null-safety No Yes
Smart casts No Yes
String-handling functions No Yes
Data classes No Yes
Destructuring Declarations No Yes
Statically typed language Yes Yes

Things that are not in Kotlin that I don’t miss from Java

Since I started using Kotlin, I’ve found that most of the time, you can use Kotlin to do anything that you used to do with Java, but in a slightly different way. However, Java is getting old so some of its features are only there because it is backward compatible. In practice, what that means on the positive side is that you can still run a program done in Java 1 with Java 8, but the negative is that you still needs to include dated things like “;” at the end of each statement.

Here a few examples of features that you cannot find in Kotlin but which I personally don’t miss anyway:

  • Checked Exceptions: In my opinion checked exceptions are not useful, they make the code more verbose and less readable. This results in multiple throws clause declarations, and more generally they aren’t compatible with functional coding.
  • Primitive types: Kotlin has primitives too (similar to Java) but unlike Java, they are not a special case. In Kotlin, everything is an object, including basic types.
  • Static members: Kotlin has replaced these with package-level (also known as top-level) functions and companion objects. The official Kotlin website recommends using package-level functions in most instances. These are functions declared outside of any class, so there is no need for an extra level of nesting.

Features that are available in Kotlin and should have been available in Java

  • Functions with default and named parameters: In Kotlin you can define a function with default parameters. This way you can call a function and only have to specify the parameters that are different to the default ones:
  • [java]
    fun defaultSum(p1: Int, p2: Int = 10, p3: Int = 0): Int {
    return p1 + p2 + p3
    }

    assert(3 == defaultSum(1, 2))
    assert(13 == defaultSum(3))
    assert(15 == defaultSum(3, p3 = 2))

    [/java]

  • Extension functions: these are functions that can be called as a member of a class but are defined outside of it. This feature makes it easier to integrate with existing code. For instance, in Kotlin you can add a new
    function to the Java String class and use it in your code.
  • Operator overloading: similar to other languages, Kotlin allows you to overload operators. For instance, you can define a special method plus to overload the + operator.
  • Null-safety: in Kotlin, the compiler will be in charge of checking any possible NullPointerException.
  • Smart casts: when you check if an object conforms to a given type, then the compiler already knows the type of the object. Therefore you don’t need to cast it. Let’s have a look to an example:
  • [java]
    if (x !is String){
    print(x.length) //there is no need to cast it
    }
    [/java]

  • Singletons: it is possible to build singletons with Java, but Kotlin provides first class support for creating singletons. You just need to use the word “object”.
  • Range expressions: Kotlin makes it easier to define a range by using the .. operator (ex: for (i in 1..4) ). Other keywords such as downTo (for reversing the order), step and until could also be very useful.
  • Data classes: If you mark a class as “data”, then the compiler automatically generates the following methods from all properties declared in the primary constructor: toString, equals, hashCode and copy.
  • String-handling functions: Kotlin provides an extra number of functions and templates that makes it easier to work with Strings.
  • Destructuring Declarations: this feature allows you to unpack a single composite value and use it to initialize several separate variables. For instance, you can ‘destructure’ an object and assign it to 2 variables: val (x,y) = Point(10, 20)

In summary

In my opinion, although Kotlin doesn’t have one single feature which will single-handedly blow your mind, when put altogether, the collection of new features that Kotlin offers really make a difference. Kotlin removes a lot of the boilerplate code that you have in Java and allows you to spend more time on the interesting parts of the code and less on routine matters.

Kotlin is not just object-oriented but also functional too. A functional style provides extra conciseness, safer multi-threading and is easier to test. Most of the most popular features of functional languages are available in Kotlin, such as first-class functions and immutability. Although since Java 8 came out, Java also has had the functional style available, Kotlin provides syntactic and library support, meaning that you can use the functional style effortlessly.

The fact that Kotlin is fully compatible with Java is also one of its biggest assets. Existing Java code can be called from Kotlin in a natural way, and Kotlin code can be used in Java too. This gives you the flexibility of mixing Java and Kotlin anywhere in your project. However, I personally wouldn’t recommend mixing them, at least not in production code, because it can overcomplicate things. Nevertheless, you could introduce it for testing purposes. Currently, there are very good frameworks which offer a more expressive DSL for writing tests in Kotlin like KotlinTest and Spek.

One field in which Java is definitely much better than Kotlin is in the quantity of available helpful resources. Java is more than 20 years old and therefore has an innumerable amount of books, articles, posts, etc written on the topic. Kotlin it is growing very fast, but still has a long way to go before it reaches the same size of community and resources as Java.

In conclusion, Kotlin is a great language with lots of fantastic features that make coding much more pleasant and improves the quality and readability of the code. Having said that, my advice is that if you are already working on a Java project, I wouldn’t attempt to mix both languages within it. On the other hand, if you are planning to start a new project or a new microservice then I would definitely recommend using Kotlin from the very start.

You may also like...

57 Responses

  1. Niels S says:

    I just read this article and thought to myself: “How could he forget about properties?”

    Seriously – I think this is the most valuable feature (next to null-safety) of the Kotlin language.

  2. Brenda says:

    Today, I went to the beach with my kids. I found a sea shell and gave it to my 4 year old daughter and said “You can hear the ocean if you put this to your ear.” She put the shell to her ear and screamed.
    There was a hermit crab inside and it pinched her
    ear. She never wants to go back! LoL I know this is entirely
    off topic but I had to tell someone!

    Look at my web blog สล็อต pg

  3. Deena says:

    Excellent website you have here but I was curious
    about if you knew of any community forums that cover the same topics discussed here?
    I’d really love to be a part of group where I can get feedback from other knowledgeable people that share the same
    interest. If you have any recommendations, please let me know.
    Cheers!

    Here is my blog … เว็บ คาสิโน ตรง ไม่ผ่าน เอ เย่ น

  4. Romaine says:

    I was curious if you ever thought of changing the page layout of your
    blog? Its very well written; I love what youve got to say.
    But maybe you could a little more in the way of content so people could connect
    with it better. Youve got an awful lot of text for only having 1 or 2 pictures.
    Maybe you could space it out better?

    Also visit my blog golden nugget online casino review

  5. Cornell says:

    Hey I know this is off topic but I was wondering
    if you knew of any widgets I could add to my blog that
    automatically tweet my newest twitter updates. I’ve been looking for a plug-in like this for
    quite some time and was hoping maybe you would have some experience with something like this.
    Please let me know if you run into anything. I truly enjoy reading your blog and I look forward to your new updates.

    Also visit my site – what is the safest online casino

  6. Georgina says:

    After I initially commented I appear to have clicked
    on the -Notify me when new comments are added- checkbox and from now on each time a comment is
    added I recieve 4 emails with the same comment. There has to be a means you are able to remove me from that service?

    Thanks a lot!

    Take a look at my blog post stardust online casino reviews

  7. Fernando says:

    I’ve learn a few excellent stuff here. Certainly value bookmarking for revisiting.
    I wonder how much effort you set to make this sort of magnificent informative website.

    Feel free to visit my site: holland casino online review

  8. Deena says:

    Hmm is anyone else encountering problems with the images on this blog
    loading? I’m trying to determine if its a problem on my end or if it’s the blog.
    Any suggestions would be greatly appreciated.

    Here is my blog; เว็บคาสิโน ไม่ผ่านเอเย่นต์

  9. Casimira says:

    Ahaa, its fastidious discussion concerning this piece of writing here at
    this webpage, I have read all that, so at this time me also commenting at this place.

    Take a look at my web page: alberta online casino review

  10. Rocco says:

    Hi, I do think this is a great website. I stumbledupon it 😉 I am going to
    come back once again since I saved as a favorite it. Money
    and freedom is the greatest way to change, may you be rich and continue to guide others.

    Here is my blog: เว็บคาสิโนตรงไม่ผ่านเอเย่นต์

  11. Joshua says:

    I’m curious to find out what blog system you are using?
    I’m having some minor security issues with my latest website and I’d like to find something more secure.
    Do you have any solutions?

    Here is my webpage; online casino review

  12. Your point of view caught my eye and was very interesting. Thanks. I have a question for you.

  13. Shanna says:

    Wonderful beat ! I wish to apprentice while
    you amend your web site, how could i subscribe for a blog site?
    The account aided me a acceptable deal. I had been a little bit acquainted of this your broadcast provided bright clear
    concept

    Here is my web page … winport online casino review

  14. Thank you for your sharing. I am worried that I lack creative ideas. It is your article that makes me full of hope. Thank you. But, I have a question, can you help me? https://accounts.binance.info/register-person?ref=JW3W4Y3A

  15. Sherri says:

    If some one needs expert view on the topic of blogging
    and site-building afterward i suggest him/her to pay a visit this webpage, Keep up the good job.

    Here is my web site skycity online casino review

  16. Sommer says:

    Everything is very open with a clear description of the issues.

    It was definitely informative. Your site is extremely helpful.

    Many thanks for sharing!

    my website … สมัคร 77bet

  17. Allan says:

    Hi there to every one, the contents present at this website are in fact amazing for people experience,
    well, keep up the nice work fellows.

    My web page; ทางเข้า bk8

  18. Charlie says:

    Hi there colleagues, how is the whole thing, and what
    you desire to say about this article, in my view its in fact amazing in favor of me.

    Here is my webpage :: สมัคร live casino house

  19. Rafaela says:

    Your method of explaining everything in this piece of writing is in fact nice,
    every one be able to simply know it, Thanks a lot.

    Feel free to visit my site; คาสิโนออนไลน์เว็บตรงต่างประเทศ

  20. Darrel says:

    Magnificent beat ! I would like to apprentice while you amend your
    website, how can i subscribe for a blog site? The account aided me a
    acceptable deal. I had been tiny bit acquainted of this your
    broadcast offered bright clear concept

    My web page ทางเข้า happyluke

  21. Bruce says:

    Fabulous, what a weblog it is! This blog provides helpful data to us, keep it
    up.

    Feel free to surf to my webpage – คาสิโนเว็บตรงไม่ผ่านเอเย่นต์

  22. Kenneth says:

    Generally I don’t read post on blogs, however I would like to say
    that this write-up very compelled me to try
    and do it! Your writing style has been surprised
    me. Thanks, quite great post.

    Take a look at my web page :: honest review on online casino tivit bet game strategy

  23. Monte says:

    Hi, Neat post. There’s an issue together with your site in web explorer, might check this?
    IE still is the marketplace chief and a good component of people will miss
    your great writing due to this problem.

    Here is my page – leo vegas online casino review

  24. Angelo says:

    Simply desire to say your article is as astonishing.
    The clearness on your post is simply spectacular and i could suppose you’re knowledgeable
    on this subject. Well along with your permission allow me to
    clutch your feed to keep up to date with coming near near post.
    Thank you one million and please carry on the gratifying work.

    My blog post; ทางเข้า fun88

  25. Arletha says:

    Today, I went to the beach with my kids. I found a sea shell and gave it to my 4 year
    old daughter and said “You can hear the ocean if you put this to your ear.” She
    put the shell to her ear and screamed. There was a hermit crab inside and it pinched her ear.
    She never wants to go back! LoL I know this is entirely off topic but I had to tell someone!

    My web blog: เว็บ คาสิโน ตรง ไม่ผ่าน เอ เย่ น

  26. binance code says:

    Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me. https://www.binance.com/bn/register?ref=WTOZ531Y

  27. Rueben says:

    Hi, this weekend is good designed for me, for the reason that this moment i am reading this great educational post here at my house.

    my web page :: คาสิโนเว็บตรงไม่ผ่านเอเย่นต์

  28. Heike says:

    Great information. Lucky me I recently found your blog by accident (stumbleupon).
    I’ve saved as a favorite for later!

    My site :: ทางเข้า ut9win

  29. Nikole says:

    Great post. I’m dealing with many of these issues as well..

    Here is my blog … คาสิโนออนไลน์เว็บตรงต่างประเทศ

  30. binance says:

    I don’t think the title of your article matches the content lol. Just kidding, mainly because I had some doubts after reading the article.

  31. Emery says:

    I couldn’t refrain from commenting. Perfectly written!

    Also visit my web site: บาคาร่าออนไลน์

  32. Your article helped me a lot, is there any more related content? Thanks! https://www.binance.info/uk-UA/register?ref=XZNNWTW7

  33. Jasmin says:

    Wow, this piece of writing is fastidious, my younger sister is
    analyzing these things, so I am going to tell her.

    my web blog :: พนันบอลออนไลน์ เล่นยังไง

  34. Trey says:

    Howdy I am so thrilled I found your site, I really found you by mistake, while I was researching on Digg
    for something else, Regardless I am here now and would just like to
    say thanks a lot for a fantastic post and a all round interesting
    blog (I also love the theme/design), I don’t have time to read through
    it all at the moment but I have book-marked it and also added in your RSS
    feeds, so when I have time I will be back to read much more, Please do
    keep up the superb jo.

    Have a look at my web-site: macau casino online review

  35. Kerstin says:

    Please let me know if you’re looking for a author for your site.
    You have some really good posts and I believe I would be a good asset.
    If you ever want to take some of the load off, I’d love to
    write some articles for your blog in exchange for a link back to mine.
    Please blast me an e-mail if interested. Cheers!

    Take a look at my web site gal sports betting online south sudan

  36. Shelby says:

    Hurrah, that’s what I was seeking for, what a material!

    present here at this webpage, thanks admin of this web page.

    my site เกมสล็อตที่คาสิโนลาสเวกัส

  37. Fred says:

    After checking out a handful of the blog posts on your web site, I honestly like your technique of blogging.
    I book-marked it to my bookmark site list and will be checking back soon. Please visit my website as well and
    let me know what you think.

    My web-site :: เครดิตฟรี superslot 50 บาท

  38. Dick says:

    Hello there! This is my first comment here so I just wanted to give a quick shout
    out and say I genuinely enjoy reading your blog posts. Can you suggest any other blogs/websites/forums that go over the same
    subjects? Thanks for your time!

    my web-site; joker123 test id

  39. Reina says:

    Hello, i believe that i saw you visited my website thus i got here to return the favor?.I’m attempting to in finding issues to enhance my
    web site!I suppose its good enough to make use of
    some of your ideas!!

    Feel free to visit my webpage … คาสิโนออนไลน์ได้เงินจริง มือถือ

  40. Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me.

  41. Alvin says:

    Thank you for the good writeup. It in fact was a amusement account it.

    Look advanced to far added agreeable from you! By the way, how could we communicate?

    Here is my web page เกมสล็อตที่คาสิโนลาสเวกัส

  42. Elvis says:

    Do you mind if I quote a few of your articles as long as I provide credit and
    sources back to your weblog? My blog is in the exact same area of interest as yours and my visitors would genuinely benefit from a
    lot of the information you provide here. Please let me know if
    this ok with you. Thanks a lot!

    Feel free to surf to my webpage: mgm online casino review

  43. Holly says:

    Right now it seems like WordPress is the preferred blogging platform available right now.
    (from what I’ve read) Is that what you’re using on your blog?

    my web blog – คาสิโนไม่ผ่านเอเย่นต์

  44. Register says:

    Your point of view caught my eye and was very interesting. Thanks. I have a question for you.

  45. Sherlyn says:

    excellent points altogether, you just received a new reader.
    What would you recommend about your put up that
    you just made a few days ago? Any positive?

    Also visit my homepage :: คาสิโนออนไลน์เว็บตรงต่างประเทศ

  46. Damaris says:

    Simply wish to say your article is as amazing. The clearness in your post
    is just excellent and i can assume you are an expert on this subject.

    Well with your permission let me to grab your feed to keep updated with
    forthcoming post. Thanks a million and please carry on the gratifying
    work.

    Also visit my web site; best canadian online casino review

  47. Thanks for sharing. I read many of your blog posts, cool, your blog is very good. https://www.binance.info/register?ref=JW3W4Y3A

  48. Micah says:

    Hello Dear, are you truly visiting this website on a
    regular basis, if so after that you will definitely obtain nice know-how.

    My website … คาสิโนเว็บตรงไม่ผ่านเอเย่นต์

  49. Ervin says:

    I do trust all the ideas you’ve presented on your post.

    They’re really convincing and will definitely work.
    Nonetheless, the posts are too brief for beginners.
    Could you please lengthen them a bit from next time? Thank you for the
    post.

    Also visit my blog :: the online casino sports review

  50. Your point of view caught my eye and was very interesting. Thanks. I have a question for you.

  51. Rex says:

    Good post. I learn something new and challenging
    on blogs I stumbleupon everyday. It will always be helpful to read content from other writers and use something from their sites.

    Here is my web site บาคาร่าออนไลน์ทดลองเล่น

  52. Clarita says:

    Appreciating the dedication you put into your blog and in depth information you
    offer. It’s awesome to come across a blog every once in a while that isn’t the same
    outdated rehashed information. Wonderful read!
    I’ve saved your site and I’m including your RSS feeds to my Google account.

    my webpage: this is vegas online casino review

  53. Dian says:

    Greetings, There’s no doubt that your site may be having internet browser compatibility problems.

    Whenever I take a look at your site in Safari, it looks fine but
    when opening in Internet Explorer, it’s got some overlapping issues.
    I simply wanted to provide you with a quick heads up!
    Other than that, great blog!

    Have a look at my web site – พนันบอลออนไลน์ ฟรี

  54. Shannan says:

    Good article! We will be linking to this particularly
    great article on our website. Keep up the good writing.

    Look into my page :: zodiac online casino login

  55. Gilberto says:

    It’s a pity you don’t have a donate button! I’d most
    certainly donate to this outstanding blog! I suppose for now i’ll settle for book-marking
    and adding your RSS feed to my Google account. I look forward
    to brand new updates and will share this blog with my Facebook group.
    Chat soon!

    my web site: คาสิโนเว็บตรงไม่ผ่านเอเย่นต์

  56. Sheila says:

    I’m curious to find out what blog system you are using? I’m having some small security issues with my latest blog and I’d like to find
    something more secure. Do you have any solutions?

    my webpage … สมัคร Huc99

  57. Ashli says:

    Greetings! I know this is somewhat off topic but I was wondering
    if you knew where I could locate a captcha plugin for my comment form?
    I’m using the same blog platform as yours and I’m having
    problems finding one? Thanks a lot!

    Also visit my web-site – สมัคร fun88

Leave a Reply to Alvin Cancel reply

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