The classes String and StringBuffer are part of the java.lang package.
(i) String in Java is an Object
String is a sequence of characters. But, unlike many other languages that implement strings as character arrays, Java implements strings as objects of type String.
Implementing strings as built in objects allows java to provide a full complement of features that make string handling convenient. Also string objects can be constructed a number of ways, making it easy to obtain a string when needed.
(ii) Strings are Immutable
Strings are immutable i.e. you cannot change the string after creation. However, a variable declared as a String reference can be changed to point at some other String object at any time.
You can still perform all types of string operations. Each time you need an altered version of an existing string, a new string object is created that contains the modifications. The original string is left unchanged. This approach is used because fixed, immutable strings can be implemented more efficiently than changeable ones. For those cases in which a modifiable string is desired, there is a companion class called StringBuffer, whose objects contain strings that can be
modified after they are created.
(iii) String class is final
Both String and StringBuffer classes are final. This allows certain optimizations that increase performance of common string operations as discussed in the previous chapter on modifiers.
(i) String in Java is an Object
String is a sequence of characters. But, unlike many other languages that implement strings as character arrays, Java implements strings as objects of type String.
Implementing strings as built in objects allows java to provide a full complement of features that make string handling convenient. Also string objects can be constructed a number of ways, making it easy to obtain a string when needed.
(ii) Strings are Immutable
Strings are immutable i.e. you cannot change the string after creation. However, a variable declared as a String reference can be changed to point at some other String object at any time.
You can still perform all types of string operations. Each time you need an altered version of an existing string, a new string object is created that contains the modifications. The original string is left unchanged. This approach is used because fixed, immutable strings can be implemented more efficiently than changeable ones. For those cases in which a modifiable string is desired, there is a companion class called StringBuffer, whose objects contain strings that can be
modified after they are created.
(iii) String class is final
Both String and StringBuffer classes are final. This allows certain optimizations that increase performance of common string operations as discussed in the previous chapter on modifiers.
No comments:
Post a Comment