This file contains 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 io.github.solidpotato.PotatoMaker; | |
import org.springframework.boot.SpringApplication; | |
@org.springframework.boot.autoconfigure.SpringBootApplication | |
public class SpringBootApplication { | |
public static void main(String[] args) { | |
SpringApplication.run(SpringBootApplication.class, args); | |
PotatoMaker.makePotato(); | |
} | |
} |
This file contains 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
plugins { | |
id 'java' | |
id 'maven-publish' | |
id 'signing' | |
} | |
sourceSets { | |
main { | |
java { | |
srcDirs = ['src'] |
This file contains 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
plugins { | |
id 'java' | |
} | |
group 'io.github.nowshad-hasan' // your group id | |
version '1.0-SNAPSHOT' | |
repositories { | |
mavenCentral() | |
} |
This file contains 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
String input = "Hello Java 17!\nIt's a new LTS."; | |
public static void indentString(String input) { | |
// Positive value add spaces beginning of each line. | |
String positiveIndent5 = input.indent(5); // 5 spaces | |
/* | |
1. Hello Java 17! | |
It's a new LTS. | |
*/ |
This file contains 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
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. |
This file contains 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
void checkLocalInterface() { | |
interface Runner { | |
void run(); | |
} | |
Runner runner = () -> System.out.println("Running"); | |
runner.run(); | |
} | |
void checkLocalEnum() { |
This file contains 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
public class FileMismatchExample { | |
/* | |
Here, hello1 and hello3 have same content, hello2 has different content. | |
*/ | |
public static void main(String[] args) { | |
Path path1 = Path.of("hello1.txt"); | |
Path path2 = Path.of("hello2.txt"); | |
Path path3 = Path.of("hello3.txt"); |
This file contains 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
public class NullPointerExceptionExample { | |
/* | |
Stack trace at Java 1.8: Exception in thread "main" java.lang.NullPointerException | |
at exception.NullPointerException.main(NullPointerException.java:14) | |
Stack trace at Java 17: Exception in thread "main" java.lang.NullPointerException: | |
Cannot read field "birthPlace" because "book.author" is null at | |
others.NullPointerExceptionExample.main(NullPointerExceptionExample.java:14) | |
*/ |
This file contains 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
public class SealedClassWithRecordExample { | |
sealed interface Exam permits Student { | |
void giveExam(); | |
} | |
record Student(String name, String rollNumber) implements Exam { | |
@Override | |
public void giveExam() { | |
} |
This file contains 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
/* | |
Interface can be sealed or non-sealed, and it can permit other interface or class. | |
Permitted interface must be sealed/non-sealed and class must be sealed/non-sealed/final. | |
Sealed interface and permitted interface/class must be in same module. For unnamed module, they must be in same package. | |
*/ | |
public sealed interface Move permits Fly, Run { | |
void move(); | |
} | |
// Fly is non-sealed. So, it can be implemented by any class or extended by any interface. |
NewerOlder