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:
- 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:
- 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)
[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]
[java]
if (x !is String){
print(x.length) //there is no need to cast it
}
[/java]
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.
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.
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
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 … เว็บ คาสิโน ตรง ไม่ผ่าน เอ เย่ น
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
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
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
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
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; เว็บคาสิโน ไม่ผ่านเอเย่นต์
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
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: เว็บคาสิโนตรงไม่ผ่านเอเย่นต์
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
Your point of view caught my eye and was very interesting. Thanks. I have a question for you.
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
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
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
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
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
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
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; คาสิโนออนไลน์เว็บตรงต่างประเทศ
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
Fabulous, what a weblog it is! This blog provides helpful data to us, keep it
up.
Feel free to surf to my webpage – คาสิโนเว็บตรงไม่ผ่านเอเย่นต์
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
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
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
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: เว็บ คาสิโน ตรง ไม่ผ่าน เอ เย่ น
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
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 :: คาสิโนเว็บตรงไม่ผ่านเอเย่นต์
Great information. Lucky me I recently found your blog by accident (stumbleupon).
I’ve saved as a favorite for later!
My site :: ทางเข้า ut9win
Great post. I’m dealing with many of these issues as well..
Here is my blog … คาสิโนออนไลน์เว็บตรงต่างประเทศ
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.
I couldn’t refrain from commenting. Perfectly written!
Also visit my web site: บาคาร่าออนไลน์