What are Strings?
Strings are variables that contain a list of characters. We can represent things such as words or sentences of human language, text from a file, or input from the user's keyboard as Strings. To the computer, a String is simply a consecutive list of characters in memory (this is actually an array, more on these later) as the diagram below shows.
Creating Strings
Strings are declared much in the way as ints, chars, etc.... The difference is that the S in String is capitalized.String address;
There are many ways we can create Strings in Java, the easiest of which is to simply write the desired contents of the String into a program. To create a String in Java, use double-quotes (") around the characters that you would like to include, and simply write them into your code.
Here are some examples of Strings that may occur within your code:
"This is a String"
"ABC123"
"100100100"
" "
"Q"
Now that you know what types of things a string can store, and how to write them, here is how you assign a value to a String.
String address = "66 University Crescent";
Now, address is available and contains the String 66 University Crescent. Strings can also occur as part of an expression, most often to add something to them.
String Expressions
Sometimes when creating a String literal you want to display the contents of a variable, but don't know what the value of the variable will be until after the program has started. In these cases, you can simply append the variables to the String literal using the + operator.For example, if we have a number that we want to include within a String, we can write it as follows:
double kilometersToSchool = 14.5;
String message = "The University is " + total + " kilometers away!";
The second line of code above can be read as 'add (or append) the double total to the end of the String The University is , and then append kilometers away! to the end of that result. When this code is executed, the variable message will contain the text The University is 14.5 kilometers away!.
Special Cases
These are two special cases to consider when dealing with Strings.1.When we want to include a double-quote within a String.
We cannot simply place it into a String literal, because Java will confuse it with the two double-quotes occuring at either end of the String. However, we can include a double-quote inside a String by preceding it with a backslash.
"\"That's the beauty of it, it doesn't do anything!\" - anonymous"
When your String is read by the computer, the \" combination will be replaced with a double-quote character, ". So the first String in the example above would contain "That's the beauty of it, it doesn't do anything!" - anonymous
2.An empty string.
An empty string is a String with no characters in it.
"" (the empty string)
This can be useful when initializing Strings.
Length
Strings are known as having a particular length. The length of a String is simply the count of the number of characters that occurs within it. This count is represented as an integer, and can be obtained via the length method. Knowing the length is useful because you might want to restrict user input based on the number of characters they type, or only process Strings greater than a particular length.String literals can range from 0 to 2^16 (65536) characters, though Strings obtained from other sources, such as files or user input, have no practical limit on length. Also note that the empty String, "", has a length of zero since it contains no characters.



Strings

