List of helpful shortcuts for faster coding
If you have any other helpful shortcuts, feel free to add in the comments of this gist :)
| static void printBookOld(Book book) { | |
| if (book instanceof Thriller) { | |
| Thriller thriller = (Thriller) book; // must-do explicit casting | |
| System.out.println(thriller.getSuspense()); | |
| } else if (book instanceof AutoBiography) { | |
| AutoBiography biography = (AutoBiography) book; // must-do explicit casting | |
| System.out.println(biography.getContribution()); | |
| } | |
| } |
| public record NewStudent(String name, int age) {} |
| public class OldStudent{ | |
| private String name; | |
| private int age; | |
| public OldStudent(String name, int age) { | |
| this.name = name; | |
| this.age = age; | |
| } |
| String newHtml = """ | |
| <html> | |
| <head> | |
| <title> | |
| Hello Text Block | |
| </title> | |
| </head> | |
| <body> | |
| </body> | |
| </html> |
| String oldHtml = "<html>\n" + | |
| "<head>\n" + | |
| " <title>\n" + | |
| " Hello Text Block\n" + | |
| " </title>\n" + | |
| "</head>\n" + | |
| "<body>\n" + | |
| "</body>\n" + | |
| "</html>"; |
| private static int getDayExpense2(WeekDay day) { | |
| return switch (day) { | |
| case SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY -> 100; | |
| // case FRIDAY -> 200; // without using 'default', this line creates compile error | |
| // case FRIDAY, SATURDAY -> 200 // valid | |
| default -> 200; // valid | |
| }; | |
| } | |
| // We must use 'default' if we have int, String etc. common parameter type. |
| private static void getDayStatus(WeekDay day) { | |
| switch (day) { | |
| case SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY -> System.out.println("On day"); | |
| case FRIDAY, SATURDAY -> System.out.println("Off day"); | |
| } | |
| } |
| private static void getDayStatus(WeekDay day) { | |
| switch(day) { | |
| case SUNDAY: | |
| case MONDAY: | |
| case TUESDAY: | |
| case WEDNESDAY: | |
| case THURSDAY: | |
| System.out.println("On day"); | |
| break; | |
| case FRIDAY: |
| public class PolymorphicMethodOverloading { | |
| public static void main(String[] args) { | |
| Consumer consumer = new Consumer(); | |
| Parent p1 = new Parent(); | |
| Parent p2 = new Child1(); | |
| Parent p3 = new Child2(); | |
| Child1 p4 = new Child1(); | |
| Child2 p5 = new Child2(); |