why so many “\” s there in this code…what is the usage of it..?

前端 未结 3 681
生来不讨喜
生来不讨喜 2021-01-22 23:16

I am a beginner to servlets..and I got a question seeing this code written inside a servlet.I\'ll be thankful if someone explain me the usage of soo many\"\\\"s here

<         


        
相关标签:
3条回答
  • 2021-01-22 23:43

    You must escape the quotes " inside Strings by writing \". If you don't do this, the Strings will end too early.

    Edit: Example:

    String thisWillNotCompile =  "This String terminates right now" and not later";
    

    Trying to compile this will give you a syntax error.

    String thisWillCompile = "This String doesn't terminate right now\" but now";
    

    This works.

    Edit 2: For more details read up on Escape Sequences in the Java Tutorial.

    0 讨论(0)
  • 2021-01-22 23:48

    \ known as escape character in programming languages. You use this to escape some special characters. For example if you want to print a string called

    Hello " World
    

    If you specify

    system.out.println("Hello " World"); // Sorry Error :(
    

    This will give an error because " is a special character. So you need to escape this by putting

    system.out.println("Hello \" World"); // It works :)
    
    0 讨论(0)
  • 2021-01-22 23:59

    It's to indicate that the character " is not a special character that marks the end of the string, but it's part of the string.

    0 讨论(0)
提交回复
热议问题