• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

escape character

 
Ranch Hand
Posts: 305
Tomcat Server Notepad 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 ?
 
author
Posts: 23833
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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
Tomcat Server Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for detailed explanation.
 
Why is the word "abbreviation" so long? And this ad is so short?
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!