Skip to content

Instantly share code, notes, and snippets.

@DRSchlaubi
Created April 11, 2021 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DRSchlaubi/a54ec48d33e25d5d52c78a1dc2a68247 to your computer and use it in GitHub Desktop.
Save DRSchlaubi/a54ec48d33e25d5d52c78a1dc2a68247 to your computer and use it in GitHub Desktop.
Java 8 is outdated ;)
// alt
void onCommand(CommandSender sender) {
if (sender instanceof Player) {
Player player = (Player) sender;
}
}
// neu
void onCommand(CommandSender sender) {
if (sender instanceof Player player) {
player.doSomething(); // player wird zu PLayer gecasted
}
}
// Alt
String input = "test";
String result;
switch (input) {
case "te":
case "est":
result = "te est";
break;
case "tEsT":
result = "TeSt";
break;
case "test":
result = "test";
break;
}
// neu
String input = "test";
String result = switch (input) {
case "te", case "est" -> "te est";
case "tEsT" -> "TeSt";
case "test" -> "test";
}
// Alt
BufferedReader reader = new BufferedReader();
// neu
var reader = new BufferedReader();
// Alt
List<String> list = new ArrayList<>();
list.add("a");
list.add("a");
list.add("a");
list.add("a");
// Neu
List<String> list = List.of("a", "a", "a", "a");
// Alt
String sql = "SELECT * FROM table \n" +
WHERE x = y";
// Neu
String sql = """SELECT * FROM table
WHERE x = y";"""
//Alt
ObjectThatHasFieldCalledI a;
a.i = "test; // => npe weil a ist null
java.lang.NullPointerException: null
<stacktrace>
//neu
ObjectThatHasFieldCalledI a;
a.i = "test; // => npe weil a ist null
java.lang.NullPointerException: Cannot assign field "i" because "a" is null
<stacktrace>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment