Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Basics Getting Started with Java Strings and Variables

Courses Plus Student 524 Points

Why is variable to be written after we use %s ?

String firstName = "Shaun";

console.printf("Hello my name is %s\n"firstName)

2 Answers

Grigorij Schleifer
Grigorij Schleifer
10,365 Points


the %s is a String formatter. It will be replaced by the variable (here your name). After you close the text ("some text with the %s formatter" there should be a comma and then the variable name. It is a convention of the printf method. This method takes a text first and then the variable name that replaces the %s String formatter.

Makes sense?

And I see two typos here:

console.printf("Hello my name is %s\n", firstName); // comma and semicolon added


S Nafees Hyder
S Nafees Hyder
649 Points

because %s may refer to format specifiers for variable interpolation in C standard and other libraries