Leoš Přikryl
@leos_prikryl
commity.cz, GDG Jihlava
val a: Int = 2 //immutable
var b: String = "Hello" //mutable
val a = 2
var b = "Hello"
val user = User()
val list = listOf(1, 2, 3) //immutable val list = mutableListOf(1, 2, 3) //mutable val map = mapOf(1 to "one", 2 to "two") //immutable val map = mutableMapOf(1 to "one", 2 to "two") //mutable
var max: Int
if (a > b) {
max = a
} else {
max = b
}
val max = if (a > b) a else b
var text: String
when (x) {
1 -> text = "jedna"
2 -> text = "dvě"
else -> {
text = "mnoho"
}
}
val text = when (x) {
1 -> "jedna"
2 -> "dvě"
else -> {
"mnoho"
}
}
val text = when (x) {
1, 2 -> "jedna nebo dvě"
3..10 -> "tři až deset"
else -> "mnoho"
}
when { x.isOdd() -> println("x je liché") x.isEven() -> println("x je sudé") else -> throw IllegalNumberException() }
if (x.isOdd()) { System.out.println("x je liché"); } else if (x.isEven()) { System.out.println("x je sudé"); } else { throw new IllegalNumberException(); }
fun sum(a: Int, b: Int = 0): Int {
return a + b
}
fun sum(a: Int, b: Int = 0) = a + b
public int sum(int a, int b) { return a + b; } public int sum(int a) { return sum(a, 0); }
public inline fun measureTimeMillis(block: () -> Unit) : Long {
val start = System.currentTimeMillis()
block()
return System.currentTimeMillis() - start
}
val elapsedTime = measureTimeMillis {
doSomeWork()
}
val doubled = ints.map { value -> value * 2 }
val doubled = ints.map { it * 2 }
public class User { private String name; public User(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
class User (var name: String)
public class User { private String name; public User(String name) {...} public String getName() {...} public void setName(String name) {...} @Override public boolean equals(Object o) {...} @Override public int hashCode() {...} @Override public String toString() {...} public User copy(String name) {...} }
data class User (var name: String)
String s = "Rozměry: " + width + " x " + height + " metrů";
String s = String.format("Rozměry: %d x %d metrů", width, height);
val s = "Rozměry: $width x $height metrů"
if (obj instanceof String) { System.out.println(((String) obj).toLowerCase()); }
if (obj is String) { println(obj.toLowerCase()); }
public class Singleton { private static Singleton instance = null; private Singleton(){ } private synchronized static void createInstance() { if (instance == null) { instance = new Singleton(); } } public static Singleton getInstance() { if (instance == null) createInstance(); return instance; } }
object Singleton
private ExpensiveObject expensiveObject; public ExpensiveObject getExpensiveObject() { if(expensiveObject == null) { synchronized(this) { if(expensiveObject == null) { expensiveObject = new ExpensiveObject(); } } } return expensiveObject; }
val expensiveObject by lazy { ExpensiveObject() }
var notNullString: String = "Hello" notNullString = null //compilation error
var nullableString: String? = "Hello" notNullString = null //OK
fun getZipForUser(user: User?): String? { return user?.address?.zip }
public String getZipForUser(User user) { if (user != null && user.getAddress() != null) { return user.getAddress().getZip(); } else { return null; } }
val displayName = username ?: "N/A"
String displayName = username != null ? username : "N/A";
fun getUserById(id: Long) : User { return userRepository.findOne(id) ?: throw UserNotFoundException() }
User getUserById(long id) { User user = userRepository.findOne(id); if (user == null) { throw new UserNotFoundException(); } return user; }
public inline fun String.isEmpty(): Boolean = length == 0
if (StringUtils.isEmpty(string)) {
if (string.isEmpty()) {