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 Java Basics Getting Started with Java Strings and Variables

Alexander Oaktree
Alexander Oaktree
515 Points

Trying to wrap it up...

So... 1)Strings are basic data types. 2)Variables are used to store data types so that you can later reference to that information. 3)strings are on of those data types.

However, Craig goes on by showing how to "create a String variable".....

so....are strings variables or data types or both? And how do the two relate to each other?

Please, explain as you would in the most basic way possible! lol

p.s. did not really understand how and why I should imagine a string as a party banner......

2 Answers

Hi Alexander Oaktree,

String is a datatype not a variable. If you create a variable in Java, you have to declare which type of data it can hold. Imagine variables as boxes. One box only for CD's, one only for kitchen stuff and so on.

Each box would be a variable to which you assign a name. The type of the box where all the CD's are in would be of the datatype cd. The datatype of the other box would be of the type kitchen because one box can hold only cd's and the other only kitchen stuff, but you cannot mix them.

So to summarise a little bit.

  • String is a datatype and not a variable
  • You declare a variable with a datatype (you tell the variable which type of data it can hold) and one of those datatypes is the type String

I hope I could clear things up a little. Let me know if you still have questions Alex.

Just saw that Robert was faster :-)

Hi Mario,

More explanation is always better, thanks for adding another perspective!

Alexander Oaktree
Alexander Oaktree
515 Points

Thank you very much Mario! The example with the boxes really clarified the concept! Cheers!

Hi Alexander,

Variables are containers.

In Java, we have to specify what type of data each variable can hold. So, a 'String variable' just means a variable that can only hold the String data-type. You will get an error if you try to assign a number to a variable that was declared as a String.

String myString = "abcd";
int myInt = 5;
char myChar = 'a';

// and so on

Please let me know if this was helpful or not.

Cheers

Alexander Oaktree
Alexander Oaktree
515 Points

Thank you very much for your patience and your help! It was very helpful! Btw, is it wrong to say that a string is the data type "text" or "letters"??

"text" is not a data type. It wouldn't make sense to declare a variable of type "text".

// this doesn't work
"text" myString = "abcd";

I'm not really sure if I understood your question. Feel free to try explaining again if you like and I'll do my best to give a better answer.

Alexander Oaktree
Alexander Oaktree
515 Points

Nono sorry! I probably didn't explain myself properly! I ment like, "outside Java", is it possible to say that a string is a data type that contains text? like words, sentences, etc.?

I'm not sure how to respond. It sounds like you understand what Strings and variables are, so I wouldn't worry too much about the nuance. That will come with time and experience.