Luca Franceschini
PhD seminars, 20 September 2018
public class Student {
int idNumber;
String name, email;
}
public class Student {
private int idNumber;
private String name, email;
}
public class Student {
private int idNumber;
private String name, email;
public Student(int idNumber, String name, String email) {
this.idNumber = idNumber;
this.name = name;
this.email = email;
}
}
public class Student {
private int idNumber;
private String name, email;
public Student(int idNumber, String name, String email) {
this.idNumber = idNumber;
this.name = Objects.requireNonNull(name);
this.email = Objects.requireNonNull(email);
}
}
public class Student {
private int idNumber;
private String name, email;
public Student(int idNumber, String name, String email) {
this.idNumber = idNumber;
this.name = Objects.requireNonNull(name);
this.email = Objects.requireNonNull(email);
}
public int idNumber() { return idNumber; }
public String name() { return name; }
public String email() { return email; }
}
public class Student {
private final int idNumber;
private final String name, email;
public Student(int idNumber, String name, String email) {
this.idNumber = idNumber;
this.name = Objects.requireNonNull(name);
this.email = Objects.requireNonNull(email);
}
public int idNumber() { return idNumber; }
public String name() { return name; }
public String email() { return email; }
}
public final class Student {
private final int idNumber;
private final String name, email;
public Student(int idNumber, String name, String email) {
this.idNumber = idNumber;
this.name = Objects.requireNonNull(name);
this.email = Objects.requireNonNull(email);
}
public int idNumber() { return idNumber; }
public String name() { return name; }
public String email() { return email; }
}
public final class Student {
private final int idNumber;
private final String name, email;
public Student(int idNumber, String name, String email) {
this.idNumber = idNumber;
this.name = Objects.requireNonNull(name);
this.email = Objects.requireNonNull(email);
}
public int idNumber() { return idNumber; }
public String name() { return name; }
public String email() { return email; }
@Override public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof Student)) return false;
Student s = (Student) o;
return idNumber == s.idNumber && name.equals(s.name) && email.equals(s.email);
}
}
public final class Student {
private final int idNumber;
private final String name, email;
public Student(int idNumber, String name, String email) {
this.idNumber = idNumber;
this.name = Objects.requireNonNull(name);
this.email = Objects.requireNonNull(email);
}
public int idNumber() { return idNumber; }
public String name() { return name; }
public String email() { return email; }
@Override public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof Student)) return false;
Student s = (Student) o;
return idNumber == s.idNumber && name.equals(s.name) && email.equals(s.email);
}
@Override public int hashCode() {
int result = Integer.hashCode(idNumber);
result = 31 * result + name.hashCode();
result = 31 * result + email.hashCode();
return result;
}
}
public final class Student {
private final int idNumber;
private final String name, email;
public Student(int idNumber, String name, String email) {
this.idNumber = idNumber;
this.name = Objects.requireNonNull(name);
this.email = Objects.requireNonNull(email);
}
public int idNumber() { return idNumber; }
public String name() { return name; }
public String email() { return email; }
@Override public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof Student)) return false;
Student s = (Student) o;
return idNumber == s.idNumber && name.equals(s.name) && email.equals(s.email);
}
@Override public int hashCode() {
int result = Integer.hashCode(idNumber);
result = 31 * result + name.hashCode();
result = 31 * result + email.hashCode();
return result;
}
@Override public String toString() {
return String.format("Student{idNumber=%d,name=%s,email=%s}", idNumber, name, email);
}
}
data class Student(
val idNumber: Int,
val name: String,
val email: String)
Automatically gives
equals()
hashCode()
copy()
toString()
Any
... and abstract classes, interfaces, enums etc.
val strings = setOf("a", "b", "c", "cc")
val (idNumber, name, email) = student
for ((key, value) in map) { ... }
max(strings, { a, b -> a.length < b.length })
if (x is String && x.length > 0) {
print(x.length)
}
val s: String = null // compilation error
val s: String = null // compilation error
val s2: String? = null // ok
Still some guarantees:
s2.length // error: variable 's2' can be null
student?.email?.length
student?.email ?: "default@email.com"
student!!.email
if (student != null) { ...
import foo.Bar;
var x: String? = Bar.method()
Getters, setters and boolean methods follow standard naming conventions
Kotlin/JVM
Kotlin/JS
Kotlin/JVM
Kotlin/Native
Kotlin/Native
The Java promise: "write once, run everywhere"
(old ideas...)
package foo
expect class Foo {
fun method()
}
package foo
actual class Foo {
actual fun method() {
...
}
}
Common code
Platform code
Things I like:
Things I don't:
Other nice things: