Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Data Structures Getting There Object Inheritance

Dylan Carter
Dylan Carter
4,780 Points

literal double quotes

''' public String toString() { return "Treet: \"" + mDescription + "\" - @" + mAuthor; } '''

okay so I get the concept of the escape char for making a literal double quote, but I don't understand the placement...

the one at the end of mDescription is after the literal quote and before the next syntax quote which I understand....but to make the first literal quote why is the escape also behind the ending of the "treet:" string?

I first thought it was a typo and it should be like this:

public String toString() {
    return "Treet:  "\" + mDescription + "\" - @" + mAuthor;

but when I tried that got syntax errors, can someone explain why this is done like that? I know its not really an important thing but I would just like to understand.

2 Answers

Your first example above, you indicate that the string is Treet:, but the string is actually Treet: " because of the placement of the escape. The escape will only escape a single character, the one following it in the string.

''' public String toString() { return "Treet: \"" + mDescription + "\" - @" + mAuthor; } '''

The first forward slash, after Treet:, is indicating that the next quotation mark read should be literal. This makes sense, because it's indicating that the resulting text should read (begin string with first quote) Treet: " (end string with second quote), then add mDescription, then the next section starts the string with a quote, then escapes the following quote, so it reads as " - @ before ending that string with another quote, then adding mAuthor.

String example = "xxx\""

//  example is a string which reads xxx"

Hi Dylan,

The escape character is always placed before the character that it's escaping because Java is reading your code character by character (so it needs to know to treat the escaping character differently before that character is reached).

As an example, "\ would be escaping the space following the forward slash and wouldn't effect how Java read the double quote.

Hope that helps, let me know if you have more questions about escape characters!

Dylan Carter
Dylan Carter
4,780 Points

okay but if you look in my example youll see it is "xxx\""xx.... if it escapes the character following the slash shouldn't that escape the ending quote for xxx also?