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

Amirul Asyraf Muhd Zin
Amirul Asyraf Muhd Zin
495 Points

can explain a little bit..i hardly to understand String firstName = "Asyraf"; console.printf("%s", firstName);

explain

Name.java
// I have setup a java.io.Console object for you named console
String firstName = "Asyraf";
console.printf("%s", firstName);

2 Answers

Timothy Williams
Timothy Williams
1,536 Points

%s is called a placeholder, like a blank________ to fill in. There are other special sequences like %n to indicate "new line" <block> String firstName = "Asyraf"; </block> says "I have a String variable [contains characters] I will call firstName, and in that variable put the characters Asyraf."

console.printf says "Print the stuff to the console (monitor in our case, a printer-console output in the OLD days!)" right after console.printf you place what you want printed in the () brackets. This could have said: <block> console.printf("My name is %s", firstName); </block> which would output to your console "My name is Asyraf"

Rob Bridges
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 Points

Hey there Asyraf,

What we are doing in these two lines is first we're declaring the variable firstName.

The next line we are printing a line from the console, but instead of typing in the word we put in the place holder %s then are telling the console to print whatever value we have defined in firstName to the console.

The %s is just a placeholder for a string variable that we want to pass in.

Thanks, let me know if this doesn't. help.