- Q: So the
Stringclass isfinalbecause its methods are complex? -
A: Yes, it is partly because the
Stringclass implementation is so complex that overriding the class would very likely cause problems. A subclass would have to take account of lots of internal code that maintains the integrity of theStringdata representation, such as the Unicode representation, locale-specific features, upper and lower case schemes and byte conversion.A high level of complexity and compatibility constraints are the most typical reasons you would mark any class
final, but the integrity of theStringclass is also critical to many Java security mechanisms. A custom class that could stand in place of aStringcould be mis-used. - Q: How many objects are created for identical strings?
-
A: Two identical string literal assignments would create two separate string references, but they would both refer to the same string object. This is a special optimisation case that is supported by the fact that string objects are immutable in Java. Once a Java
Stringobject is created it cannot be changed, so it is safe for any number of identical string literal references to point to a singleStringobject.The Java compiler marks string literals in a way that the Java Virtual Machine can identify and add to its String Literal Pool, a collection of references to Java string objects. If identical string literals are found at runtime the String Literal Pool provides a reference to the first instance of the string, and no duplicate
Stringobject is created. That means that two identical string literals refer to the same Java object.This optimisation does not apply to strings declared with the
new String()constructor, as the examples below demonstrate. - Q: What's the use of the
Stringconstructor? -
A: The explicit
Stringconstructor has the same result as using double quotes to implicitly create a string reference, in both cases the given string is assigned to the reference variable. However, passing a double quoted string argument to theStringconstructor actually makes two strings; the argument string is implicitly created inline and then its contents are copied to the newStringinstance. For this reason this constructor is largely redundant and not recommended for general purposes. TheStringconstructor withStringargument is rarely used except to create an independent copy of an existing string variable.// Creates one string implicitlyString s ="This is a test string";// Creates two string instancesString s =newString("This is a test string"); - Q: How should I use the
Stringconstructor? -
A: The
Stringclass does have a constructor but it should not be used unless you have a very clear and definite purpose for it. In most cases a newStringshould be declared by enclosing the relevant text in double quotes assigned to aStringvariable, or passed directly into a method.String myString =
"Efficient String creation."; System.out.println("Anonymous string instance.");There are a number of specialist
Stringconstructors that can be used to create strings from character and byte arrays,StringBufferandStringBuilderreferences, see the Java API documentation for full details.public String(
char[] value); public String(char[] value,intoffset,intcount); public String(byte[] value); public String(byte[] value,intoffset,intcount); public String(StringBuffer buffer); public String(StringBuilder builder);The
intarguments above operate like a substring method parameters, to select only the characters from theoffsetindex of the array onwards bycountcharacters.
String methods and operators
- Q: One case prints a Java code, another a literal string!
-
A: Your sample code shows some confusion between reference values and literal strings. Whenever an object reference is passed to the
System.out.println(Object)method, itstoString()method is used to provide the output.a a1 =
newa(); out.println("a");In this case, the instantiation of the class
a, referenced by the variablea1is legitimate, but the second line completely disregards it and will print the string literal "a". Whenever you put characters in double quote marks like this, the Java interpreter will treat it as aStringobject, not a reference to the classaor instancea1.try{ ... }catch(Exception e) { out.println(e); }In the second case the reference to the exception
ewill be printed by calling itstoString()method. The default implementation oftoString()inherited from theObjectclass will output a coded reference to the object in the Java Virtual Machine. If thetoString()method has been overridden by the exception class, it may provide human readable diagnostic information. - Q: The
==operator doesn't match strings correctly! -
A: Java strings are stored as an immutable sequence of Unicode characters and the class'
equals()method is overridden to compare that character content, so standard logical comparison operators will not give the result one might expect. If you use the simple comparison operator on two string objects that represent the same string, it will returnfalse. Always use theequals(Object)method to compare the contents of two strings, as below. - Q: Is the
+operator overloaded? -
A: Java does not have operator overloading, but string concatenation is a special case. When the
+operator is applied to aString, the two values are appended as a newString. If you use the+operator with aStringand a primitive value, such as anintorlong, the Java interpreter implicitly converts the primitive value a string representation and concatenates them. - Q: How can you say the
+operator is not overloaded in Java! -
A: The plus operator for Java strings is a limited exceptional case that is practically the same as operator overloading. Its a fine distinction but using
+to append object contents is not a general case in Java, it only applies toStringobjects. An equivalent append operation may be meaningful and useful for data storage types but does not have a general application and is not implemented in Java.// Compiler error: "operator + cannot be applied to java.util.Vector"// Vector vector = new Vector() + new Vector(); - Q: How can I reverse the characters in a string?
-
A: This example code gets a
chararray from the input string and loops through it to populate a secondchararray. Theforloop uses two variables,iis incremented andjis decremented at each pass. The output array could be used to create a new string or output directly, as in this case.
String buffers and tokenizers
- Q: What is the difference between
StringandStringBuffer? -
A: The main difference is that in Java
Strings are immutable, which makes them quite inefficient for concatenating large amounts of text, especially in extensive loops. For eachStringconcatenation statement the Java runtime system must instantiate at least one additionalStringobject and then dispose of it.StringBufferis designed exactly to overcome this problem, to build string content in an editable internal buffer without generating lots of additional objects.StringBufferhas many convenience methods to append all Java primitive types, character arrays and objects, and to check and manipulate characters in the buffer. - Q: When should I use a
StringBufferinstead of aString? -
A: In most cases you should use a
StringBufferrather than string concatenation. The character content of JavaStringobjects is immutable. Whenever you concatenate twoStringinstances you actually create a thirdStringobject for the result. This implicitStringobject creation can slow down your program, increase the number of objects in the runtime system and the garbage collection required to dispose of the temporary strings.On a small scale, string concatenation is unlikely to have a significant performance impact, but if you are building strings in a
fororwhileloop, or over many statement lines it is better to use aStringBuffer, orStringBuilderin single threaded applications. - Q: Why don't two
StringBuffers match? -
A: The
Stringclass overrides the default implementation of theequals(Object)method to compare the string contents of each object. In this case equivalent string contents are considered equal. TheStringBufferclass does not override the superclassObjectequals(Object)method, which tests whether the argument refers to the same object reference. - Q: What's the difference in the memory allocation for
StringBuffers? -
A: The key difference between a
Stringand aStringBufferin terms of memory allocation is thatStringobjects are immutable; once the string contents are set they cannot be changed, so the virtual machine can optimise memory use on this basis. The content ofStringBufferscan be expanded beyond their initial buffer size, so the memory allocation needs to be variable and must be managed by the Java runtime system. TheStringBufferclass automatically adjusts its buffer size to fit the string content it is given, but you should instantiate the class with an explicit buffer size large enough to avoid the performance overhead associated with such resizes.StringBuffer buffer =
newStringBuffer(1024);Java programmers should not be concerned with detailed level memory management for
Stringoperations, which will be handled and optimised by the runtime system. The key things are to chooseStringorStringBuffertypes appropriate to the task and set an adequate buffer size. - Q: How can I pad a
StringBuffer? -
A: The
StringBufferinsert(int, String)method can be used to pad the buffer at specific locations. The method inserts the given string at the offset position indicated by theintvalue and shifts the original buffer contents right. The original string contents are preserved and the buffer length is increased by the length of the inserted string. - Q: What is a
StringTokenizerfor? -
A: The standard
java.util.StringTokenizerclass is a special type ofEnumerationthat represents segments of a string, which may be separated by one or more "delimiters". When you construct aStringTokenizerwith a comma delimiter, it will identify each word in a comma separated list for instance. - Q: How can I get a person's initials from their full name?
-
A: The simplest way to extract single words from a string like a full name is to use a
StringTokenizer. TheStringmethodcharAt(int)can then be used to get the first character of each word, as below.
Regular expressions
- Q: How can I check a string has no numbers?
-
A: The simplest way to check that a
Stringdoes not contain any numbers is to use the regular expression classPatternin thejava.util.regexpackage. The method below uses the regular expression[^0-9]+to check that none of the characters in the input string is a number. The square brackets define a character class. The negation modifier^followed by the number range0-9means "not a number". The+quantifier asks the regular expression to match the character class one or more times.
No comments:
Post a Comment