Possible Duplicate:
‘ \ ’-Invalid character constant?
In Java, I am trying to initialize a char variable like below, which it is not allowing.
char ch = '\';
Any reason behind this? It's giving a compilation error.
Possible Duplicate:
‘ \ ’-Invalid character constant?
In Java, I am trying to initialize a char variable like below, which it is not allowing.
char ch = '\';
Any reason behind this? It's giving a compilation error.
you need to escape it:
char backslash = '\\';
char quotation = '\'';
The reason is, this \'
is a single quotation mark.
System.out.println(backslash); // prints \
System.out.println(quotation); // prints '
Characters like \
, "
and '
hold special meaning. Therefore to use them as character literals, you need to escape them. They need to be written as '\\'
, '\''
and '\"'
respectively.
e.g. char c = '\\';
Similarly, to include them in strings, you need to escape them.
e.g. String path = "C:\\Program Files\\Java"