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 static void onShow(Country country) { | |
switch (country) { | |
case NETHERLANDS, POLAND, GERMANY -> System.out.println("European Country"); | |
case INDIA, BHUTAN, NEPAL -> System.out.println("Asian Country"); | |
default -> System.out.println("It's from Wakanda"); | |
} | |
} |
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 static void onShowWithYield(Country country) { | |
String output = switch (country) { | |
case NETHERLANDS, POLAND, GERMANY -> { | |
System.out.println("European Country" + country); | |
yield "Country belongs to Europe Continent"; | |
} | |
case INDIA, BHUTAN, NEPAL -> { | |
System.out.println("Asian Country" + country); | |
yield "Country belongs to Asian Continent"; | |
} |
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 static void onShowWithReturnValue(Country country) { | |
String output = switch (country) { | |
case NETHERLANDS, POLAND, GERMANY -> "European Country"; | |
case INDIA, BHUTAN, NEPAL -> "Asian Country"; | |
default -> "It's from Wakanda"; | |
}; | |
System.out.println(output); | |
} |
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
record Country(String name,String capital){} |
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 SampleRecordClass { | |
record Country(String name,String capital){} | |
public static void main(String[] args) { | |
Country firstCountry = new Country("Netherlands","Amsterdam"); | |
Country secondCountry = new Country("Germany","Berlin"); | |
System.out.println("First country object is " + firstCountry); | |
System.out.println("Second country object is " + secondCountry); | |
System.out.println("Check if both objects equal" + firstCountry.equals(secondCountry)); | |
// Copying object using attribute method of existing records object | |
Country countryClone = new Country(firstCountry.name,firstCountry.capital); |
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 void patternMatchingInstanceOf() { | |
Object o = new CountryData("Netherlands","Amsterdam","Europe"); | |
if (o instanceof CountryData countryData) { | |
System.out.println("This capital of Netherlands is " + countryData.capital()); | |
} | |
} |
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 void patternMatchingInstanceOfScope() { | |
Object o = new CountryData("Netherlands","Amsterdam","Europe"); | |
if (o instanceof CountryData countryData && countryData.continent().equals("Europe")) { | |
System.out.println("This continent of Netherlands is " + countryData.continent()); | |
} | |
} |
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
NumberFormat numberFormatLong = NumberFormat | |
.getCompactNumberInstance(Locale.forLanguageTag("NL"), NumberFormat.Style.LONG); | |
System.out.println(numberFormatLong.format(2000)); | |
System.out.println(numberFormatLong.format(20000)); | |
System.out.println(numberFormatLong.format(200000)); |
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
2 duizend | |
20 duizend | |
200 duizend |
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
NumberFormat numberFormatShort = NumberFormat | |
.getCompactNumberInstance(Locale.forLanguageTag("NL"), NumberFormat.Style.SHORT); | |
System.out.println(numberFormatShort.format(2000)); | |
System.out.println(numberFormatShort.format(20000)); | |
System.out.println(numberFormatShort.format(200000)); |
OlderNewer