String Data Types
If you need to hold textual data you should use a string data type. For example, to create a String named str and assign it to the text “Java”, you would use the following code.
String str; str = "Java";
Just as with numeric data types these two steps can be combined as follows.
String str = "Java";
You can use the + operator with a string, just like numeric types, however, it has a different effect. Adding two strings attach, or concatenate, them together. For example, the following block of code would print out
“HelloWorld”.
String str = "Hello"; str = str + "World"; System.out.println(str);
The statement str = str + “World” attaches “World” to the end of str.
Important: You should only use a numeric data type when it makes sense to perform mathematical operations on the value (i.e. adding or subtracting from it). Otherwise, the data type used should be String. For example, a social security number should be a string because there is no value in adding or subtracting from a social security number.




