// a.
int n = 493;
System.out.println(n);
// b.
int n = 493;
System.out.println(
String.format("Value of n: %d", n));
// c.
// The Kotlin if statement and while loop
// look the same as they do in Java. However,
// the Elvis operator is translated differently.
// Java version:
boolean b = true;
System.out.println(b ? "true" : "false");
// Kotlin version:
var b = true
println(if (b) "true" else "false")
// d.
for(int n = 1; n <= 10; n++) {
print(n + " ");
}
// e.
for(int n = 1; n <= 10; n += 2) {
print(n + " ");
}
// f.
for(int n = 10; n >= 1; n -= 2) {
print(n + " ");
}
// g
// Translate this function into Kotlin
// and write a main method to test it:
public String makeGreeting(String firstWord, String name) {
return firstWord + ", " + name + ", how are you?";
}
Look at these sections in the Introduction to Kotlin
document:
Constants Unit Named Parameters Arrays Lists Classes