Skip to content

Instantly share code, notes, and snippets.

Avatar

Jarno Walgemoed jwalgemoed

View GitHub Profile
View basic.java
// Classic declaration, we are repeating ourselves
Integer ten = new Integer(10);
// New declaration, using 'var'
var ten = new Integer(10);
View examplekotlin.kt
class SomeClass {
// Kotlin allows this, as you can specify the intended type here
var property: String
}
View example2.java
// 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)
View example-collection.java
// 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>();