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

I do not understand String and Variables. Are they [String variables] or two different things. Need help to understand.

When the teacher says String and Variables I was so happy to learn but at the middle of the video he says String variables. What's going on with me is that I do not understand this concepts. what do they create ? for example. String firstName = "lolo"; variable console.printf("lolo " is learning); and then console.printf("%s is learning java, firstName"); why do we need to put firstName on console.printf? and firstName in this case is variable or String variable ?

3 Answers

andren
andren
28,558 Points

A variable is pretty much just a container that is used to store data for later use, a String is a type of data, so a String variable is simply a variable that contains a String.

printf is a command that formats a string based on placeholders placed in it, in this case it will replace the "%s" found in the string with whatever String we pass to it in the second argument.

It's worth nothing that this line of code from your examples:

variable console.printf("lolo  is learning");

Is incorrect. variable is not a keyword that has any meaning on its own in Java, when you define a variable you do so by first telling Java what type of data you will be storing in the variable and then the name of the variable.

thank you so much. thumbs up!! all the answers you guys gave me helped me a lot!

Stephen O'Dell
Stephen O'Dell
1,596 Points

Two different things. Or, maybe put it this way: two different ways of looking at the same things.

Variables are a data type that might change in value. Using math instead of programming, think about x and y, which stand for some number. In programming, you might say yourHighScore could be a data field that will hold one number, but could change as your score changes. Variables are compared to constants: quantities whose values do not change during program execution.

Strings are a type of data, along with numbers (integers and floating point, for example), or boolean (yes/no, true/false). Strings contain alphanumeric and special character symbols, like "Ted Johnson" or "XYZ123."

Two different ways of looking at the same kind of thing.

thank you so much. thumbs up!!

Steven Parker
Steven Parker
231,007 Points

I'm not a Java programmer, but...

This concept is common to most languages. Variables come in different types, depending on what they hold.

A "string" is a type of thing. It could be a variable or a literal. Something identified with a word, like firstName would be a string variable. Something in quotes, like "John" would be string literal.

I'm guessing that you misplaced the quote mark on this code line:

console.printf("%s is learning java, firstName");  // Here's what you wrote
console.printf("%s is learning java", firstName);  // <-- I'd bet it should be like this

In the second case, the printf function will substitute the "%s" part of the first argument (which is a string literal) using the value of the next argument (which is the string variable firstName). So if firstName was set to "lolo", what would appear on the console would be this:

lolo is learning java

Is it more clear now?

thank you so much. you guys are amazing!