Joining Strings with a Delimiter
Starting with Java 8, we can join several strings with a delimiter (e.g., comma) via the String.join() method: String result = String.join(“, “, “One”, “Two”, “Three”);// One, Two, ThreeSystem.out.println(result);
Starting with Java 8, we can join several strings with a delimiter (e.g., comma) via the String.join() method: String result = String.join(“, “, “One”, “Two”, “Three”);// One, Two, ThreeSystem.out.println(result);
Beginning with JDK 10, we can rely on the Java Local Variable Type Inference (Var Type) in order to avoid explicit typing. Check out the following sample: var outputStream =
String and character literals provide an escape mechanism that allows express character codes that would otherwise not be allowed in the literal. An escape sequence consists of a backslash character
Java 9 has added the concept of module via Java Platform Module System. In order to obtain the names of packages from a module we can do it as follows:
The Math package is feature-rich and some of the methods are illustrated below. public class MathPkg{public static void main(String args[]){MathPkg mathPkg = new MathPkg();mathPkg.proceed();}private void proceed(){int positiveNum = 5;int negativeNum
It is difficult to maintain correctness in mutable objects, as multiple threads could be trying to change the state of the same object. This can lead to some threads seeing
Swapping two numbers without using a new variable is always a good approach. This helps your application to be memory and performance oriented. public class Swap2Numbers{ int firstNum = 10;
Before Java 8, this was obtained as a String[] via TimeZone class: String[] zoneIds = TimeZone.getAvailableIDs(); Java 8 comes with a new approach via java.time.ZoneId class: Set zoneIds = ZoneId.getAvailableZoneIds();
Starting with JDK 8, you can avoid NullPointerException by returning an Optional. For example, instead of returning null, this method returns an empty Optional: public Optional fetchShoppingCart(long id) { ShoppingCart