Kotlin vs Java
final var original = "abc"
System.out.println(original);
System.out.println(new StringBuilder(input).reverse().toString());
System.out.println(original.length());
val original = "abc"
original.also {
println("The original String is $it") // "abc"
}.also {
println("The reverse String is ${it.reversed()}") // "cba"
}.also {
println("The length of the String is ${it.length}") // 3
}
public MyFragment createInstance(Bundle args) {
final var fragment = new MyFragment();
fragment.setArguments(args);
return fragment;
}
fun createInstance(args: Bundle) = MyFragment().apply { arguments = args }
final var original = "abc"
// Map the value and send to the next chain
Optional.of(original)
.map(
str -> {
System.out.println("The original String is " + str); // "abc"
val result = new StringBuilder(input).reverse().toString();
return result;
}
).map(
reversed -> {
System.out.println("The reverse String is " + reversed); // "cba"
val result = reversed.length();
return result;
}
).map(
length -> {
System.out.println("The length of the String is " + length); // 3
return length;
}
)
val original = "abc"
// Map the value and send to the next chain
original.let {
println("The original String is $it") // "abc"
it.reversed() // map it as parameter to send to next let
}.let {
println("The reverse String is $it") // "cba"
it.length // can be evolved to other type
}.let {
println("The length of the String is $it") // 3
}
public File makeDir(String path) {
final var result = new File(path);
result.mkdirs();
return result;
}
fun makeDir(path: String) =
path.let { File(it) }
.also { it.mkdirs() }
if (webview.settings != null) {
final var settings = webview.settings;
settings.javaScriptEnabled = true;
settings.databaseEnabled = true;
}
webview.settings?.run {
javaScriptEnabled = true
databaseEnabled = true
}
public void displaySubstringPosition(final String input, final String sub) {
final var index = input.indexOf(sub);
if (index >= 0) {
System.out.println("The substring " + sub + "is found in " + input + ".");
System.out.println("Its start position is " + index + ".");
}
}
fun displaySubstringPosition(input: String, sub: String) {
input.indexOf(sub).takeIf { it >= 0 }?.let {
println("The substring $sub is found in $input.")
println("Its start position is $it.")
}
}
val numbers = List.of("one", "two", "three");
with(numbers) {
final var firstItem = numbers.get(0);
final var lastItem = numbers.get(numbers.size() - 1);
System.out.println("First item: " + firstItem + ", last item: " + lastItem);
}
val numbers = mutableListOf("one", "two", "three")
with(numbers) {
val firstItem = first()
val lastItem = last()
println("First item: $firstItem, last item: $lastItem")
}