-1

basically i want the output to be Path = "C:\Users\Public" and it seems to me that System.out.println("Path = "C:\Users\Public""); should work, but it doesn't so the question is why can't java just print the phrase as a combination of characters?

btw. this is my second time "programing" so please in simple terms if possible.

  • 2
    You probably noticed that `"` is used by Java language as special character which opens and closes String *literals* like `"hello wold"`. If there is opening quote there needs to be closing quote as well, so for instance `"hello"world"` would consider middle `"` as closing quote making `"hello"` valid string but at the same time `world"` part as something which compiler wouldn't expect and will complain about it. To make `"` compiler treat `"` inside string literal as simple character we need to escape it. To do so we are placing ``\`` before it. – Pshemo Nov 25 '20 at 03:41
  • 1
    Since ``\`` is also considered as special character (used to escape `"`) to create character ``\`` we also need to escape it with another ``\``. So string literal representing something like `ab"cd\ef` would need to look like `"ab\"cd\\ef"`. Now try to apply that to your example. – Pshemo Nov 25 '20 at 03:41

2 Answers2

2

You should escape the special character such as " and \

System.out.println("Path = \"C:\\Users\\Public\""); 
Ahmed Tounsi
  • 1,482
  • 1
  • 14
  • 24
1

When you use backslash (\), java assumes that you are going to use an escape character. If you want to print that line you should probably use the method below and it should work. And you are using a string inside another one so you should use different types of inverted commas for both to tell the compiler that yeah there is a string inside another one. Otherwise the compiler will assume the middle inverted comma to be the closing one.

You can escape each and every escape character. If you notice I have used a backslash before inner inverted commas (" ") and every other backslash (\). Using a backslash before any escape character escapes it. So this should work.

System.out.println("Path = \"C:\\Users\\Public\"");

I hope now you can print your required output.

Simran Sharma
  • 852
  • 7
  • 16
  • Note that you cannot escape every character. You get "illegal escape character" for e.g., `\C`. What should it mean anyway? – Robert Nov 25 '20 at 03:59
  • Yes, that is the reason you need to escape that backslash before the letter C. So you should use two backslashes before C. Because there are some characters that when escaped have a total different impact. Like `\n` or `\t`. Since `\C` doesn't mean anything, it gives an error that you're escaping an illegal character. Because C is not an escape character. So you should the backslash as well if you want to print it. – Simran Sharma Nov 25 '20 at 04:02
  • Right. Sorry, I should have been clearer. I was referring to your statement "you can escape each and every character", which is not true. I liked the explanation in your answer (that's why I upvoted it), but that part is not accurate and can be misleading. – Robert Nov 25 '20 at 04:06
  • Got it. I'll just edit it. – Simran Sharma Nov 25 '20 at 04:07
  • Ok wait. I wrote each and every "escape" character. Not every cahracter. – Simran Sharma Nov 25 '20 at 04:08
  • Oh. Really? My bad. Guess it's time to close the laptop for today. ;-) – Robert Nov 25 '20 at 04:09
  • Yeah its completely fine. ;-) – Simran Sharma Nov 25 '20 at 04:10