\ is used as for escape sequence in many programming languages, including Java.
If you want to
- go to next line then use
\nor\r, - for tab use
\t - likewise to print a
\or"which are special in string literal you have to escape it with another\which gives us\\and\"
\ is used as for escape sequence in many programming languages, including Java.
If you want to
- go to next line then use
\nor\r, - for tab use
\t - likewise to print a
\or"which are special in string literal you have to escape it with another\which gives us\\and\"
Imagine you are designing a programming language. You decide that Strings are enclosed in quotes ("Apple"). Then you hit your first snag: how to represent quotation marks since you've already used them ? Just out of convention you decide to use \" to represent quotation marks. Then you have a second problem: how to represent \ ? Again, out of convention you decide to use \\ instead. Thankfully, the process ends there and this is sufficient. You can also use what is called an escape sequence to represent other characters such as the carriage return (\n).
Why is the length of the special backslash character one and not two in Java or Javascript?
What is the key word in Java string split?
regex - Java: Single Backslash String - Why is "\\" illegal? - Stack Overflow
Clarify escaping of backslash in String
Videos
I get why they should be used if we want to show certain special characters, but should not the length be two and not if there are for example two characters within the character itself? Does it work differently special, or does the length of the character itself not matter and the length will always be one technically? If so, why?
For example, the example below will output 6 but should it not be 7 instead since \\ is two characters? I might be missing a simple concept, I apologize:
public class HelloWorld{
public static void main(String []args){
String x = "Mango\\";
System.out.println(x.length());
}
}