2

I know how to print double quotes around a String literal in Java, but how do you print them with a String variable? The code below won't compile.

String upperCasePhrase = capInitials("the initial letters should be capitalized");
System.out.println(\" upperCasePhrase \");
gig6
  • 31
  • 1
  • 1
  • 4

3 Answers3

10

There are actually 2 issues with your code.

  1. You need to make sure that \" is a String. This can be done by surrounding it with double quotes like this: "\""

  2. You need to concatenate Strings when printing them by using a +

Change the last line of code in your question to:

System.out.println("\"" + upperCasePhrase + "\"");
TofferJ
  • 4,678
  • 1
  • 37
  • 49
  • what if the string variable contains a string like '\\' eg: String s = "\\"; how do you get it surrounded by double quotes to get a String "\\"? – nikhil kekan Sep 26 '19 at 00:08
2

You can use single quotes around a char; if you then concatenate that to a String, the char will automatically be converted to a String:

System.out.println('"' + upperCasePhrase + '"');

You may just find this a bit clearer to read than using "\"".

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
  • The `char` values won't actually be converted to `String`. A `StringBuiilder` is created for the string concatenation, and the `char` values are added directly as single characters, without being converted to `String` first. – Andreas Sep 14 '17 at 22:08
  • @Andreas not necessarily. It's typically implemented like that, but it's not [required by the specification](https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.18.1): "If only one operand expression is of type String, then string conversion (§5.1.11) is performed on the other operand to produce a string at run time." So `Character.toString('"').concat(upperCasePhrase)` etc would be valid. – Andy Turner Sep 14 '17 at 22:08
  • It is what the compiler does. My point was that it doesn't go thru the overhead of converting to `String` first, before concatenating. – Andreas Sep 14 '17 at 22:11
  • @Andreas and my point is that it is what the compiler *might* do. The spec quite clearly states that the operand will be converted to a string. – Andy Turner Sep 14 '17 at 22:13
  • Hmm... Code actually compiled as `new StringBuilder(String.valueOf('"')).append(upperCasePhrase).append('"').toString()`, so the first one got converted to `String`, but the second one didn't. *Weird.* – Andreas Sep 14 '17 at 22:14
  • @Andreas `StringBuilder` doesn't have a constructor taking a single char. It *could* have been implemented as `new StringBuider().append('"')` etc. – Andy Turner Sep 14 '17 at 22:15
  • I had expect compiler to generate `new StringBuilder().a‌​ppend('"')...` – Andreas Sep 14 '17 at 22:17
  • @Andreas it may depend on your compiler, as that is what I see. – Andy Turner Sep 14 '17 at 22:25
0

There are special characters reserved for Java language which you cannot explicitly use inside your Strings. Such characters require to be 'escaped' or replaced with an alternative notation:

Backspace is replaced with \b

Newline is replaced with \n

Tab is replaced with \t

Carriage return is replaced with \r

Form feed is replaced with \f

Double quote is replaced with \"

Backslash is replaced with \

Sergey Emeliyanov
  • 5,158
  • 6
  • 29
  • 52