| String | String store text characters, alphanumeric, number and punctuation. | ||
| Because all characters are stored as Unicode two-byte characters, String can represent international language symbols. | |||
| String and StringBuffer objects deal with 16-bit Unicode characters to support international alphabets. Although you create String literals with 8-bit ASCII code, Java automatically converts them to 16-bit Unicode. | |||
| Creating Strings | String literals can be created by using the assignment operator as a primitive are: | ||
| String myname = “Hop”; | |||
| Exam Tip | String is not a primitive data type. It’s an Object. | ||
| String can also be created using the new operator: | |||
| String myname = new String(“Hop”_); | |||
| Concatenation | The + and += operator can be used to concatenate strings: | ||
| String hello = “Hello” + “ World”; | |||
| The concat() method can be used to concatenate strings. | |||
| String a = “Hello”;String b = “ World”;String c = a.concat(b); | |||
| The StringBuffer class is actually used to concatenate create strings. | |||
| c is actually created as this in the background:c = new StringBuffer().append(“Hello”).append(“ World”).toString(); | |||
| Constructors | Has nine constructors: | ||
| They can be passed these arguments:(), (String), (charArray), (byteArray), (buffer) | |||
| toString() | All objects inherit the Object class toString() method to provide the string representation of the objects data. | ||
| Primitives are also concatenated to create strings. | |||
| Immutable | String are immutable objects.Their internal value can not be changed. | ||
| String can only be referenced. They have no data structure.String can be assigned a value of null. | |||
| String c is another string object.a and b objects are not changed. | |||
| String Reference | A String object is not the string itself, but a reference to memory. | ||
| Compile reference | Java performs optimization on Strings at compile time. When a literal string is created by several variables, java has these variables reference that one string reference in memory. | ||
| String a = “java”;String b = “java”;a and b reference the same object “java”. b is reassigned to the a variable. | |||
| Even if the string is a literal in a different object or package. | |||
| class Other { static String java = “java” } | |||
| a, b and java of the Other object refer to the same object. | |||
| The possibility of changing the string value by one of the object would in affect change the other advertently /inadvertently. | |||
| That is why objects are immutable. To prevent the string value inadvertently changes. | |||
| Exam Tip | Literal java strings are optimized at compile time to refer to the same object in-memory if the literal value are the same. | ||
| Runtime reference | String java = new String(“java”);This java would not equal a, b. Since the non-literal java is created at runtime and refers to another object in-memory. | ||
| String ja = “ja”;String jv = ja + “va”;jv would not equal a, b or java variable, since the jv is computed at runtime. | |||
| Exam Tip | Equals test on different objects return false.Integer.equals(Long) return false. | ||
| Exam Tip | Non literal values, assignments are evaluated at runtime. | ||
| Exam Tip | Boolean and most of the wrapper object overrides the object equals() method. | ||
| equals() | The equals() method of the String class has been overridden to test for data equality not just reference equality. | ||
| Garbage Collection | Java performs automatic garbage collection on unreferenced strings. | ||
| String y = “yes”;String n = “no”;String m = “maybe”;
String s = “I vote “ + y; //”I vote yes” is created String s = “I vote “ + n; //”I vote no” is created String s = “I vote “ + m; //”I vote maybe” is created |
|||
| Dangling references | The last value of s would be “I vote maybe”.”I vote yes” and ”I vote no” are dangling references.They will automatically be garbage collected. | ||
| String Methods | Methods: | ||
| String content comparison are done using: | |||
| equals() | Is used for memory comparison | ||
| equalsIgnoreCase() | Is used for memory comparison ignoring case. | ||
| length()charAt(int index) | |||
| getChars((int srcBegin, int srcEnd, char[] dst, int dstBegin) | Copies characters from this string into the destination character array. | ||
| indexOf(int ch) | Returns the index within this string of the first occurrence of the specified character. | ||
| compareTo(String str) | Compares two strings lexicographically.The result is a negative integer if this string precedes the argument string. The result is a positive integer if this string follows the argument string. The result is zero if the strings are equal. | ||
| substring(int bet, int end) | Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex – 1. Thus the length of the substring is endIndex-beginIndex. | ||
| Exam Tip | toString() | The toString() of String. Returns the object reference. | |
| Exam Tip | toUpperCase() | If the string is already upper case, the reference of the original string is returned. Else, a new uppercase string is returned. | |
| Exam Tip | The method is substring() not subString(). This can show up on the exam. | ||
| hashCode() | Generates a code for the string which can be used in a hash table for fast lookup. | ||
| Exam Tip | String a = “ java “; ?Same ReferenceString b = “ java”;if (a.trim() == b.trim() )
a.trim() and b.trim() return two different objects. The if statement return false. The string objects are created at runtime and are not literals, thereby not the same reference.
if (a.trim().equals( b.trim()) ) – The if statement returns true. ? Same Value ? Same Reference |
||
| StringBuffer | Upon creation all StringBuffer class have a capacity.The capacity when created in constructor is the size of the passed parameter string. | ||
| As the StringBuffer grows its capacity is automatically increased. | |||
| A StringBuffer is mutable, unlike strings. | |||
| The StringBuffer class is thread safe since its methods are synchronized. | |||
| toString() | To convert a StringBuffer to a string use toString() method. | ||
| Methods: | |||
| capacity() | |||
| reverse()charAt()setCharAt()getChars()
setCharAt() append() insert() |
|||
| Exam Tip | StrinbBuffer b = new StrinbBuffer(“abcdef”);b.delete(3,6);Returns “abcg”, position 3 is inclusive, position 6 is exclusive. | ||
| Actual length is 6But indices goes up to 50-a, 1-b, 2-c, 3-d, 4-e, 5-f ….. 6th elementsBut StringBuffer
Deletes “defg”
deletes(3,3) – deletes nothing deletes(2,3) – deletes “c” deletes(3,4) – deletes “3” deletes(3,5) – deletes “f” deletes(3,6) – deletes nothing deletes(3,2) – get runtime error |
|||