Java - New Line Character Issue

后端 未结 4 859
半阙折子戏
半阙折子戏 2021-01-28 01:50

I have a code like,

String str = \" \" ; 

while(  cond ) {

  str = str + \"\\n\" ;

}

Now, I don\'t know why at the time of printing, the out

4条回答
  •  Happy的楠姐
    2021-01-28 02:33

    If you really want \n, to get printed, do it like this.

    String first = "C:/Mine/Java" + "\\n";
    System.out.println(first);
    

    OUTPUT is as follows :

    NEWLINE

    For a good reference as to why is this happening, visit JAVA Tutorials

    As referred in that TUTORIAL : A character preceded by a backslash is an escape sequence, and has a special meaning to the compiler. When an escape sequence is encountered in a print statement, the compiler interprets it accordingly

    Hope this might help.

    Regards

提交回复
热议问题