public class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
fun main() {
println("Hello World!")
}
public final double PI = 3.14;
public final double CALUCLATED_PI = calculatePi();
public void type() {
String stringValue = "Value";
final String STRING_CONSTANT = "Const value";
var value = "Value";
final var CONSTANT = "Const value";
}
private double calculatePi() {
return 3.14;
}
const val PI = 3.14
val CALCULATED_PI = calculatePi()
fun type() {
var stringValue: String = "Value"
val STRING_CONSTANT: String = "Const value"
var value = "Value"
val CONSTANT = "Const value"
// value = 128 <- error
}
fun calculatePi(): Double {
return 3.14
}
Optional.ofNullable(name)
.map(String::toUpperCase)
.orElse("UNKNOWN");
name?.toUpperCase ?: "UNKNOWN"
Not implemented
String changeMyName(String name) {
return Optional.ofNullable(name).map(String::toUpperCase).orElse("UNKNOWN");
}
fun changeMyName(name: String?) = name?.toUpperCase() ?: "UNKNOWN"
Not implemented
changeMyName("Patryk");
changeMyName("Patryk", "My default value");
...
String changeMyName(String name, String defaultName) {
return Optional.ofNullable(name)
.map(String::toUpperCase)
.orElse(defaultName);
}
String changeMyName(String name) {
return getMyName(name, "UNKNOWN");
}
changeMyName("Patryk")
changeMyName("Patryk", "My default value")
...
fun changeMyName(name: String?, defaultName: String = "UNKNOWN") = name?.toUpperCase() ?: defaultName
Not implemented
changeMyName(defaultName = "My default value", name = "Patryk")
...
fun changeMyName(name: String?, defaultName: String = "UNKNOWN") = name?.toUpperCase() ?: defaultName
Not implemented
StringUtil.myCapitalize("patryk");
...
class StringUtil {
static String myCapitalize(String text) {
return text.substring(0, 1).toUpperCase() + text.substring(1);
}
}
"patryk".myCapitalize()
...
fun String.myCapitalize() = this.substring(0, 1).toUpperCase() + this.substring(1)
Not implemented
val date = 2 JANUARY 2018
...
infix fun Int.JANUARY(year: Int) = Date(year - 1900, Calendar.JANUARY, this)
Not implemented
@Test
fun `Should return true if the user is active`() {
// ...
}
public class Person {
private String name;
private String surname;
public Person(String name, String surname) {
this.name = name;
this.surname = surname;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return Objects.equals(name, person.name) &&
Objects.equals(surname, person.surname);
}
@Override
public int hashCode() {
return Objects.hash(name, surname);
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", surname='" + surname + '\'' +
'}';
}
}
data class Person(val name: String, val surname: String)
public class SynchronizedSingleton {
private static SynchronizedSingleton instance = null;
private SynchronizedSingleton() {
}
private synchronized static void createInstance() {
if (instance == null) {
instance = new SynchronizedSingleton();
}
}
public static SynchronizedSingleton getInstance() {
if (instance == null) {
createInstance();
}
return instance;
}
}
object SynchronizedSingleton {
}
String string = name + " " + surname;
val string = "$name $surname"
String calculatedValue = null;
...
String getCalculatedValue() {
if (calculatedValue == null) {
System.put.prinltn("in progress ...";)
calculatedValue = takeFromDatabase();
}
return calculatedValue;
}
val calculatedValue: Int by lazy {
println("in progress ...")
takeFromDatabase()
}
Integer observable = null;
...
void setObservable(Integer n) {
System.out.println("Old: " + observable + " new: " + n);
observable = n;
}
var observable: Int by observable(0) { property, oldValue, newValue ->
println("Old: $oldValue and new: $newValue")
}
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}
for (i in 1..5) {
println(i)
}
Not implemented
fun showType(objectToCheck: Any): String {
return when (objectToCheck) {
0 -> "Number: zero"
in 10..99 -> "Two digits number"
is String -> "String: ${objectToCheck.length}" // smart casting
else -> "Unknown type"
}
}
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 7);
List<Integer> evenNumbers = list.stream()
.filter(number -> number % 2 == 0)
.collect(Collectors.toList());
val list = listOf(1, 2, 3, 4, 5, 7)
val evenNumbers = list.filter { it % 2 == 0 }