|
|
Assignment
- The String data type is a complex data type. Notice
that the word String begins with a capital letter. In Java, the
naming conventions are that classes begin with a capital letter.
String is a class. When you define a variable as data type
String, you are creating an object.
- String tmpName = "Joe Smith";
- The variable tmpName is a String object.
- Because
String is a class, there are methods that are associated with the class
and as a result are available to the objects created from the class.
- .length() - this method will return the number of characters, including spaces, in the String object.
- String tmpName = "Joe Smith";
- int tmpLength = tmpName.length();
- tmpLength would contain the value 9, the total number of characters in the tmpName object, including spaces
- .toUpperCase() - this method will return
the characters of the String object all converted to upper case
letters. It will not modify spaces or special characters, just
the letters.
- ***It is important to note that this
method does not change the original string. That will stay
exactly as it is. It will only return an upper case version of
the string.
- String tmpName = "Joe Smith";
- String upperName = tmpName.toUpperCase();
- upperName would contain the value "JOE SMITH"
- tmpName would still contain the original value "Joe Smith"
- .toLowerCase()
- this method will return the characters of the String object all
converted to lower case letters. It will not modify spaces or
special characters, just the letters.
- It works the same as the toUpperCase() method above.
Concatenation
- Strings and primitive data types can be combined or
"concatenated" together. Whenever you are combining a string with
primitive data, such as integer or double, the primitive data is
converted to string and combined with the string.
- String tmpName = "Joe Smith";
- int tmpAge = 23;
- String newString = tmpName + " is " + tmpAge + " years old.";
- The tmpAge value of 23 is converted to a String value and the resulting concatenation becomes:
- "Joe Smith is 23 years old."
- Notice that when concatenating above, I padded some of the words with spaces. If I had not, the result would have been:
- "Joe Smithis23years old."
- This is a logic error as the sentence is not readable without the spaces.
- Sometimes when concatenating you want the result of a calculation to be included in a string. For example:
- String tmpName = "Joe Smith";
- double discount = .063;
- double price = 19.99
- String
newSentence = tmpName + " receives a discount of " + discount + " on
the price of " + price + " which equals " + discount * price;
- The above would result in:
- "Joe Smith receives a discount of .063 on the price of 19.99 which equals .06319.99"
- Notice that the calculation was not done, the two values were just converted to string and placed next to each other.
- To force a calculation prior to converting to a string in concatenation, you must enclose the calculation in parenthesis.
- String newSentence = tmpName + " receives a discount of " + discount +
" on the price of " + price + " which equals " + (discount * price);
- This would result in:
- "Joe Smith receives a discount of .063 on the price of 19.99 which equals 1.25937"
To learn more about the String class you can view the JavaDocs for this class at:
http://docs.oracle.com/javase/6/docs/api/java/lang/String.html
- JavaDocs are a special form of documentation that is included inside each Java class or program.
- JavaDocs can tell you all that you need to know to use a class.
- It documents:
- The instance variables of the object.
- The constructors, what explicit parameters are required and how the object will be created.
- The methods, what they will return, what explicit parameters they expect, and what they will do.
You can learn more about JavaDocs at this website: http://www.mcs.csueastbay.edu/~billard/se/cs3340/ex7/javadoctutorial.html
|