Many classes in the javax.sql package use new String(str)
constructor. For example:
public void setCatalogName(int columnIndex, String catalogName) throws SQLException {
checkColRange(columnIndex);
if (catalogName != null)
colInfo[columnIndex].catName = new String(catalogName);
else
colInfo[columnIndex].catName = new String("");
}
Or
public void setUsername(String name) {
if(name == null)
{
username = null;
} else {
username = new String(name);
}
}
And many more:
javax.sql.rowset.serial.SerialStruct.SerialStruct(SQLData, Map>) javax.sql.rowset.serial.SerialStruct.SerialStruct(Struct, Map>) javax.sql.rowset.RowSetMetaDataImpl.setCatalogName(int, String) javax.sql.rowset.RowSetMetaDataImpl.setColumnLabel(int, String) javax.sql.rowset.RowSetMetaDataImpl.setColumnName(int, String) javax.sql.rowset.RowSetMetaDataImpl.setColumnTypeName(int, String) javax.sql.rowset.BaseRowSet.setCommand(String) javax.sql.rowset.BaseRowSet.setDataSourceName(String) java.text.DateFormatSymbols.setLocalPatternChars(String) javax.sql.rowset.BaseRowSet.setNull(int, int, String)
What is the purpose of this? Isn't it creating unnecessary string instances on the heap?
By new keyword : Java String is created by using a keyword “new”. For example: String s=new String(“Welcome”); It creates two objects (in String pool and in heap) and one reference variable where the variable 's' will refer to the object in the heap.
Strings, which are widely used in Java programming, are a sequence of characters. In Java programming language, strings are treated as objects. The Java platform provides the String class to create and manipulate strings.
The javax. sql package provides the preferred way to make a connection with a data source. The DriverManager class, the original mechanism, is still valid, and code using it will continue to run.
The String class represents character strings. All string literals in Java programs, such as "abc" , are implemented as instances of this class. Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared.
They're potentially protecting against the "small part of a big string" problem:
String x = getHugeStringFromSomewhere();
String y = x.substring(0, 5);
foo(y);
Now suppose the string referred to by y
remains required, but x
doesn't. Because y
refers to the original char[]
, you end up with that tiny string being responsible for a lot of memory.
If you create a new string instead, copying the data:
String x = getHugeStringFromSomewhere();
String y = new String(x.substring(0, 5));
foo(y);
... then the underlying large char array associated with the original string can be garbage collected at the same time as the string.
I've seen this make a huge difference when reading lots of tiny lines from a large text file (e.g. words in a dictionary). IIRC, BufferedReader.readLine
creates a buffer of 80 characters, so each string returned pins at least an 80-character char[]
, even if it's only 5 characters long. It all adds up... (As per comments, apparently this changed in Java 1.5 for readLine
- but substring
still works the same way.)
EDIT: Of course that's still only a guess as to the reason, and it definitely doesn't explain the new String("")
part...
It appears the code has been changed from JDK6 to JDK7 and all instances of new String(str)
were removed. So, although Jon Skeet's suggestion is quite interesting, it was probably a lame piece of code they found and fixed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With