# Replace Inefficient Constructors with valueOf()

# Description

All calls to a constructor of a primitive type will be replaced by the corresponding static valueOf() method. For example new Integer("1") becomes Integer.valueOf("1").

# Benefits

Using this rule saves memory and CPU cycles, as the constructors are not needed in this case. Furthermore, the constructors are deprecated in Java 9, which is an indication that they will eventually be removed from the language altogether.

# Tags

# Code Changes

# Number object types

The following change applies for Integer, Double, Float and Long.

Pre

new Integer(1);

new Integer("1");

Post

Integer.valueOf(1);

Integer.valueOf("1");

# Boolean objects

Pre

new Boolean(true);
new Boolean("true");
new Boolean(false);
new Boolean("false");
new Boolean("anyOtherStringThanTrue");
new Boolean((String) input);
new Boolean((Boolean) input);

Post

Boolean.valueOf(true);
Boolean.valueOf(true);
Boolean.valueOf(false);
Boolean.valueOf(false);
Boolean.valueOf(false);
Boolean.valueOf((String) input);
Boolean.valueOf((Boolean) input);

# Bytecode JDK 1.8

Pre

public void original() {
   Integer val1 = new Integer(1);
   Integer val2 = new Integer("1");
}
 0 new #2 <java/lang/Integer>
 3 dup
 4 iconst_1
 5 invokespecial #3 <java/lang/Integer.<init>>
 8 astore_1
 9 new #2 <java/lang/Integer>
12 dup
13 ldc #4 <1>
15 invokespecial #5 <java/lang/Integer.<init>>
18 astore_2
19 return

Post

public void transformed() {
   Integer val1 = Integer.valueOf(1);
   Integer val2 = Integer.valueOf("1");
}
 0 iconst_1
 1 invokestatic #6 <java/lang/Integer.valueOf>
 4 astore_1
 5 ldc #4 <1>
 7 invokestatic #7 <java/lang/Integer.valueOf>
10 astore_2
11 return

🛠️ Auto-refactor Available

You can auto-refactor this with jSparrow.
Drop this button to your Eclipse IDE workspace to install jSparrow for free:

Drag to your running Eclipse* workspace. *Requires Eclipse Marketplace Client

Need help? Check out our installation guide.

# Properties

Property Value
Rule ID InefficientConstructor
First seen in jSparrow version 1.0.0
Minimum Java version 5
Remediation cost 5 min
Links