Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

escape character  RSS feed

 
meeta gaur
Ranch Hand
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.println("\"");
why is it legal escape character ?, it shouldn't be like ("\\"") ?

System.out.println("\."); if above is legal then why is it is illegal ?

Why do we need here "\\." but not for first "\\"" like this ?
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
meeta gaur wrote:System.out.println("\"");
why is it legal escape character ?, it shouldn't be like ("\\"") ?

System.out.println("\."); if above is legal then why is it is illegal ?

Why do we need here "\\." but not for first "\\"" like this ?


Regarding System.out.println("\""); ... The quote is not a legal character in a string literal, so if you the quote, you will need to escape it with a backslash.

Regarding System.out.println("\."); ... The dot is a legal character in a string literal, and hence, doesn't not have to be escaped. The language also defined it as illegal to escape characters when it is not needed.

Regarding System.out.println("\\."); ... First, you are not escaping the dot -- you are escaping the backslash. The backslash is not a legal character in a string literal, so if you the backslash, you will need to escape it with another backslash.

Regarding System.out.println("\\""); ... Again, you are not escaping the dot -- you are escaping the backslash. The quote is not a legal character in a string literal, so if you the quote, you will need to escape it with a backslash.

Henry
 
meeta gaur
Ranch Hand
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for detailed explanation.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!