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! While you're at it, check out some resources Treehouse students have shared here.

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, Variables, and Formatting

Pedro Silva
Pedro Silva
5,363 Points

I got a little lost, what exactly "%s" is and do?

i understand how to use "%s" in this exercise but not what it is and if this is it's only use, can anyone help me?

1 Answer

Hi Pedro,

It's a placeholder for a string, so at that point within the output string, the java code will insert the contents of a variable which you place after the output string.

String name = "Steve";
console.printf("Insert my name here: %s.", name);

There are other types of placeholders for different data types, so there's different letters for numbers Some details here.

Steve.

Pedro Silva
Pedro Silva
5,363 Points

oh i see, but just for curiosity, if i put a second variable after "name" from your example, he will always choose the first one? and if i put another "%s" on my sentence, it will follow the order the variables were written?

Thanks for the help Steve!

You can do as many placeholders as you like, yes. Then list the variable names in the order you want them to appear:

name = "Steve";
town = "Bungay";
country = "the UK";
console.printf("Hi, my name is %s and I live in %s which is in %s", name, town, country);