Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do classes in the javax.sql package use new String(str)?

Tags:

java

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?

like image 674
Bozho Avatar asked Aug 08 '12 17:08

Bozho


People also ask

Why do we use new string in Java?

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.

Why do we use string class?

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.

What is the purpose of javax sql package?

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.

What are string classes in Java?

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.


2 Answers

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

like image 114
Jon Skeet Avatar answered Nov 15 '22 17:11

Jon Skeet


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.

like image 24
Bozho Avatar answered Nov 15 '22 19:11

Bozho