How to create multiline strings using Java text blocks
KW.java
class KW { @SuppressWarnings("preview") public static void main(String[] args) { String s1 = """ Welcome to KodingWindow"""; String s2 = "Welcome\nto\nKodingWindow"; System.out.println(s1); //text block string System.out.println("———————————————————————"); System.out.println(s2); //normal string System.out.println("———————————————————————"); System.out.println(s1==s2); System.out.println(s1.equals(s2)); } }
Output
kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW Welcome to KodingWindow ——————————————————————— Welcome to KodingWindow ——————————————————————— true true kodingwindow@kw:~$
Comments and Reactions
What Next?
Java Arrays
Advertisement