+ 3
Variable & Placeholder difference
What is the difference between variable & placeholders. How they are used in doing.
5 Answers
+ 4
Variables are named slots of memory that store data for you to use. Placeholders are something you can use in your string so you can more properly format the string in a readable way instead of constantly breaking the string by concatenating variables into place. Placeholders are just a tidier way of doing it.
I threw together an example so you can see it in action.
https://code.sololearn.com/cC7iM3XDZaOL/#java
public class Program
{
public static void main(String[] args) {
String user = "N1H4R";
String welcome = String.format("Welcome to SoloLearn, %s!", user);
System.out.println(welcome);
}
}
EXAMPLE WITHOUT PLACEHOLDERS:
String user = "N1H4R";
String welcome = "Welcome to SoloLearn, " + user + "!";
System.out.println(welcome);
^As you can see, that's not as clean as using placeholders. With simple strings, it doesn't matter too much, but imagine that it was a whole paragraph and you needed to use multiple variables to provide dynamic content? Things can start to get messy and harder to read.
+ 3
You're welcome! Good luck on your studies.
+ 1
yeah ! now I got it. thanks for helping :)
+ 1
yeah ! now I got it. thanks for helping :)
0
yup :)