Created
May 26, 2017 10:49
-
-
Save vyashole/ba52e8c462328a09cf27245c9ee11fc1 to your computer and use it in GitHub Desktop.
Kotlin Blog post examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
return nullableString?.length ?: -1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var s: String = "I Love Kotlin" | |
s = null // compilation error, s can't be null |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if(nullableString != null) { | |
return nullableString.length(); | |
} else { | |
return -1; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var nullableString: String? = "abc" | |
nullableString = null // now this is not an error |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.commons.lang.builder.EqualsBuilder; | |
import org.apache.commons.lang.builder.HashCodeBuilder; | |
import org.apache.commons.lang.builder.ToStringBuilder; | |
public class User { | |
private String name; | |
private int age; | |
public User(String name, int age) { | |
this.name = name; | |
this.age = age; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public int getAge() { | |
return age; | |
} | |
public void setAge(int age) { | |
this.age = age; | |
} | |
@Override | |
public String toString() { | |
return ToStringBuilder.reflectionToString(this); | |
} | |
@Override | |
public int hashCode() { | |
return new HashCodeBuilder() | |
.append(name).append(age).toHashCode(); | |
} | |
@Override | |
public boolean equals(Object other) { | |
if (other == this) { | |
return true; | |
} | |
if ((other instanceof Example) == false) { | |
return false; | |
} | |
Example rhs = ((Example) other); | |
return new EqualsBuilder().append(name, rhs.name) | |
.append(age, rhs.age).isEquals(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data class User(val name: String, val age: Int) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment