Just thought of sharing the new feature of jdk 8 i used for achieving same result. Using StringJoiner we can construct a sequence of characters separated by a delimiter.
StringJoiner formattedString= new StringJoiner("\n");
formattedString.add("XXX");
formattedString.add("YYY");
System.out.println(formattedString);