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

I am using eclipse as it is used in my institution and we dont use %s or something for string instead we use the variabl

What is the difference in using a %s for printing a string? we use the variable name instead like : String name ="abcd" System.out.print("My name is " + name);

1 Answer

William Li
William Li
Courses Plus Student 26,865 Points

You're quite right.

Java gives you different way to combine variable and String together, what you're doing here is called string concatenation, you're concatenating String and variable together using the + sign; another way is called String interpolation, you're inserting variable into a String using the %s formatter.

They both get the job done equally well, usually which one to use is just a matter of style, though I wanna say that if you're inserting lots of variables into a string, using String Interpolation probably makes it a bit easier to figure out how the output String gonna be by looking at the code.