Java Java Basics Getting Started with Java Strings and Variables

Ranak Bansal
Ranak Bansal
1,207 Points

What is the purpose of %s?

Why must we use the '%s' when calling a variable in our code? I am unable to understand what the %s does, and if it is being used to call our code, why do we have to type 'firstName' as well?

2 Answers

Steven Parker
Steven Parker
176,603 Points

The "%s" is a format specifier that serves as a placeholder in the template string for where the value of the variable will get plugged in. This particular one reserves space for a string variable, which is what the "s" stands for.

just asking to piggyback off of the original for my understanding, so does the %s call any(all) strings in in the file thus why you have to specify firstName. like if I had another string I would just use %s but at the end call it like console.printf(), lastName;???

Steven Parker
Steven Parker
176,603 Points

The "%s" acts as a placeholder for the string that is supplied as an additional argument, after the template itself. So they both need to be passed as arguments inside the parentheses, like this: "console.printf("%s", lastName)".