Show Menu
Cheatography

Comparing Kotlin and Java Cheat Sheet (DRAFT) by

Kotlin and Java Syntax Side by Side

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Kotlin

Semioc­olons NOT needed. If you use them,the compiler will give you a gray notice they're redundant.
Null safety. Using
null
is strongly discou­raged.
Variables can be typed upon declar­ation or not. The compiler will infer a type from how you use it if you don't specify. It's good practice to type your variables on declaraion if you know what it will be. Use 'T' if not (more later).

Values and Variables

It's good practice to specify a type on declar­ation but you don't have to.
Typing is accomp­lished by adding a colon and the type after the new variable's name:
var newInt : Int = 4

var newString : String = "Send Lawyers, Guns and Money."­
Don't feel like declaring a type? No problem. The compiler will take a best guess based on how you initialize it:
var newString = "More lawyers... and definitely more money."­

var moreMoney = 10000
val is a Value that never changes. Your name is a val and the voting age (18) is another. Vals have lower overhead because you initialize it once and that's it.
val name = "John Doe"
// Name never changes
val votingAge = 18
 ­ ­ ­  // Voting age doesn't change.
var is a Variable that can change as needed. Your age is a var . Vars have more overhead than Vals because of the additional code associated with being able to manipulate their contents.
var age = 22
// 22 today, but 23 next year.
var side = "­Garlic Mashed Potato­es"
// Will it be salad tomorrow?
NULL note:
Unlike Java you normally can't declare a variable without initia­lizing it because to do so would mean that the variable has a null value. It's strongly suggested to initialize it upon declar­ation, even if you use "­" or 0 to do it. However, if you're determined to make your variable null then you can do so by declaring it's type and then putting a '?' at the end of the type like this:
var missin­gString : String?

var missingInt : Int?
 

Java

Semicolons are required at the end of statments.
You can blow yourself up with all the
nulls
you want. The compiler won't stop you.
Variables are typed on declar­ation.

If Statements

if ( hungry) {

­
 ­­ ­ ­­ ­­ eat();

­
} else if ( thirsty) {

­
 ­­ ­ ­­ ­­ dr­ink();

­
} else {

­
 ­­ ­ ­­ ­­ do­Tha­tOt­her­Thi­ng();

}

While Loop

Checks for
true
BEFORE doing the action. If
false
the code never executes at all:
while (shall­WeD­oThis) {

 ­­ ­ ­­ ­­ ex­ecu­teT­his­Code();

 ­­ ­ ­­ ­­ th­isC­ode­Too();

 ­­ ­ ­­ ­­ }

Do While Loop

Checks for
true
AFTER doing the action. If
false
then the action is done once before the condition is even checked. Notice the unusual syntax at the end, with parens and a ; rather than braces.
do {

 ­­ ­ ­­ ­­ th­eTh­ingToDo

 ­­ ­ ­­ ­­ } while (shall­WeD­oIt­Again);

For Loop

Ye old for loop that hasn't changed since the Pharos coded in the pyramids.
for ( int i = 0; i < max; ++i) {

 ­­ ­ ­­ ­­ th­eTh­ing­ToD­oEa­chT­ime­WeL­oop();

}

Switch in Java = When in Kotlin

Switch Statem­ent
switch( thing1­IsE­qualTo ) {
­ ­ ­­ ­­ ­ ­case thing2:
 ­ ­ ­ ­  ­ ­ ­ ­ ­doThing2Stuff
 ­ ­ ­ ­  ­ ­ ­ ­ ­break;
 ­­ ­ ­­ ­­ ­case thing3:
 ­ ­ ­ ­  ­ ­ ­ ­ ­doThing3Stuff
 ­ ­ ­ ­  ­ ­ ­ ­ ­break;
 ­ ­ ­ ­ ­def­ault:
 ­ ­ ­ ­  ­ ­ ­ ­ ­­doDefaultStuff
}

Try / Catch

try {
 ­ ­ ­ ­ ­sta­tem­ents;
­} catch (Exce­­pti­­onType e1) {
 ­ ­ ­ ­ ­sta­tem­ents;
catch (Exception e2) {
 ­­ ­ ­­ ­­ ­­­cat­­ch-all statem­ents;
­} finally {
 ­ ­ ­ ­ ­­sta­tem­ents;
}