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?
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.