Shashank Agarwal
Shashank Agarwal

Reputation: 718

What are the different ways to represent quote character in java in a string literal?

So a googler asked this question, as what are the different ways you can represent a character (in this case the double quote) in JAVA.

One being

String t ="\\u0022";

What are the other ways ?

Upvotes: 2

Views: 474

Answers (2)

Elliott Frisch
Elliott Frisch

Reputation: 201447

In Java, one can escape literal quotes like

String t = "\"";

and with the unicode escape you already gave (with another literal \ because the unicode conversion happens very early in the compilation phase)

String t = "\\u0022";

and you can promote a character like

String t = Character.toString('"');

Upvotes: 2

DripDrop
DripDrop

Reputation: 1002

The two other way are:

  1. An escape character followed by a double quote: \",
  2. The unicode example given: \u0022,
  3. And by converting a char to a string: new Character('"').toString();.

The first one is the simplest, and easiest on the programmer, the second is longer, and generally harder to remember, and the third is easy to remember but generally illogical.

Upvotes: 0

Related Questions