Handling character strings in Java is supported through two final classes: String and StringBuffer. The String class implements immutable character strings Cheap Ovie Ejaria Jersey , which are read-only once the string has been created and initialized, whereas the StringBuffer class implements dynamic character strings. All string literals in Java programs Cheap Nathaniel Clyne Jersey , are implemented as instances of String class. Strings in Java are 16-bit Unicode.
Note : In JDK 1.5+ you can use StringBuilder, which works exactly like StringBuffer Cheap Mohamed Salah Jersey , but it is faster and not thread-safe
The easiest way of creating a String object is using a string literal:
String str1 = "I cant be changed once created!";
A string literal is a reference to a String object. Since a string literal is a reference, it can be manipulated like any other String reference. i.e. it can be used to invoke methods of String class.
The Java language provides special support for the string concatenation operator ( + ), which has been overloaded for Strings objects. String concatenation is implemented through the StringBuffer class and its append method.
String finalString = 聯Hello?+ 聯World?br > Would be executed as
String finalString = new StringBuffer().append(聯Hello?.append(聯World?.toString();
The Java compiler optimizes handling of string literals. Only one String object is shared by all string having same character sequence. Such strings are said to be interned, meaning that they share a unique String object. The String class maintains a private pool where such strings are interned.
Since the String objects are immutable. Any operation performed on one String reference will never have any effect on other references denoting the same object.
Constructors
String class provides various types of constructors to create String objects. Some of them are,
String()
Creates a new String object whose content is empty i.e. 聯聰.
String(String s)
Creates a new String object whose content is same as the String object passed as an argument.
Note: Constructor creates a new string means it does not intern the String. Interned String object reference can be obtained by using intern() method of the String class
String also provides constructors that take byte and char array as argument and returns String object.
String equality
String class overrides the equals() method of the Object class. It compares the content of the two string object and returns the boolean value accordingly.
Note that == compares the references not the actual contents of the String object; Where as equals method compares actual contents of two String objects.