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 Basics Getting Started with Java Strings and Variables

simon k
simon k
591 Points

Double Quotes in String variable

Hi, I'd like to know why double quotes used in the string variable & not parentheses ?

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! I think I understand what you're asking, but I'm not 100% sure. Double quotes is how we refer to a string literal. I believe the parentheses you're referring to are the ones he uses console.printf which is a method run on the console object. I promise you'll learn more about that later. But let's take a look at an example:

String greeting = "Hi there, Simon.";
console.printf(greeting);
console.printf("  How are you?")

The result of this will be two messages printed out one after another. It will look like: Hi there, Simon. How are you?. Because we send in the greeting variable directly in the first print statement, we don't need the quotes. The quotes are there in the variable declaration. That's how we define a string. The second print out needs quotes because we're not sending in a variable rather we're sending in a string literal directly.

Hope this helps! :sparkles: