This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SomeClass { | |
// Kotlin allows this, as you can specify the intended type here | |
var property: String | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Classic declaration | |
ArrayList<Integer> list = new ArrayList<Integer>(); | |
// But honestly, since Java 7 Java we can use the diamond operator <> (or omit it entirely) | |
ArrayList<Integer> list = new ArrayList(); | |
// Var declaration | |
var list = new ArrayList<Integer>(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Classic declaration, we are repeating ourselves | |
Integer ten = new Integer(10); | |
// New declaration, using 'var' | |
var ten = new Integer(10); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// We can explicitly work against interfaces | |
List<String> list = new ArrayList(); // type: List<String> | |
// Using var, we can't do this without casting | |
var list = new ArrayList<String>(); // Inferred type ArrayList<String> | |
var list = (List)new ArrayList<String>(); // Inferred type List<String> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Can't use the local variable type inference with fields | |
public class SomeClass { | |
// Not allowed, var is only available for local variables | |
var property = ""; | |
} | |
// Can't leave values unitialzed | |
var uninitialized; // No way to infer the type | |
// Can't just assign null to var to reassign later (but workaround available) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var variable = returnValue(); // Type is inferred from the return type of the method | |
// Method declaration | |
public String returnValue() { | |
return "w00p"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var strings = Arrays.asList("a", "b", "c"); | |
for(var str: strings) { | |
System.out.println(str); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data class User(val name: String?, val email: String?) { | |
fun isValid() = !name.isNullOrBlank() && name.length > 3 | |
&& !email.isNullOrBlank() && email.length > 3 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ExperimentalContracts | |
fun User?.isComplete(): Boolean { | |
contract { returns(true) implies (this@isComplete != null) } | |
return this != null && this.isValid() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
contract { | |
returns(true) implies (this@isComplete != null) | |
} |
OlderNewer