George Newton George Newton - 1 year ago 64
Java Question

Escaping variables in java

Can you escape variables in Java? For example, in PHP, I can do this:

$test = 3;
echo "He is {$test} years old";

Which will output:

He is 3 years old

Can you do a similar thing in Java?

Answer Source

You should use String.format

int age = 3;
String newString = String.format("He is %d years old", age);

%d - says int. %s - is for string, %f for float, and more and more formatting options.

You can find a very good explenation here:


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download