A boxed primitive is created just to call its compareTo
method. It's more efficient to use the associated static compare method (for double and float since Java 1.4, for other primitive types since Java 7) which works on primitives directly.
This method or field is or uses a Map
or Set
of URL
s. Since both the equals
and hashCode
method of URL
perform domain name resolution, this can result in a big performance hit.
This method accesses the value of a Map entry, using a key that was retrieved from a keySet
iterator. It is more efficient to use an iterator on the entrySet
of the map, to avoid the Map.get(key)
lookup.
toString
invoked on a string value is useless JAVA-S0064Calling String.toString
is a redundant operation. Just use the string directly.
Boolean
constructor is inefficient, consider using Boolean.valueOf
instead JAVA-S0066Creating new instances of java.lang.Boolean
wastes memory, since Boolean
objects are immutable and there are only two useful values of this type.
Integer
/Long
constructor is inefficient, use valueOf
instead JAVA-S0067Using Integer
's default constructor is guaranteed to always result in a new object whereas Integer.valueOf
allows the compiler/class library/JVM to cache values, which is known as interning.
Float
/Double
constructor is inefficient, use valueOf
instead JAVA-S0068Using Float
or Double
's default constructor is guaranteed to always result in a new object whereas the valueOf
method of these classes allows the JVM to cache values, which is known as interning.
""
instead of new String()
to create empty strings JAVA-S0063Creating a new java.lang.String
object using the default constructor wastes memory because the object so created will be functionally indistinguishable from the empty string constant ""
.
String
constructor JAVA-S0062Creating a String
using object creation wastes memory because the new String
object so constructed will be functionally indistinguishable from the String
value passed as a parameter. Just use the string directly.
This code explicitly invokes garbage collection via System.gc
or Runtime.gc
. Except for specific use in benchmarking, this is very dubious.
+
within loops is costly and should be replaced by a StringBuffer
/StringBuilder
JAVA-P1006The method seems to be building a String
using concatenation in a loop. In each iteration, the String
is converted to a StringBuffer
/StringBuilder
, appended to, and converted back to a String
.
This can lead to a cost of `O(n^2)`` where n is the number of iterations, as the growing string is recopied in each iteration. This issue is detected only on Java versions 8 and below.
A boxed value is unboxed and then immediately reboxed. This has likely occurred due to an unboxing operation by the programmer, which the java compiler has undone.
""
instead of new String()
to create empty strings JAVA-P0063Creating a new java.lang.String
object using the default constructor wastes memory because the object so created will be functionally indistinguishable from the empty string constant ""
.
toString
invoked on a string value is useless JAVA-P0064Calling String.toString
is a redundant operation. Just use the string directly.
This method or field is or uses a Map
or Set
of URL
s. Since both the equals
and hashCode
method of URL
perform domain name resolution, this can result in a big performance hit.
String
constructor JAVA-P0062Creating a String
using object creation wastes memory because the new String
object so constructed will be functionally indistinguishable from the String
value passed as a parameter. Just use the string directly.
This code explicitly invokes garbage collection via System.gc()
or Runtime.gc()
. Except for specific use in benchmarking, this is very dubious.
Boolean
constructor is inefficient, consider using Boolean.valueOf
instead JAVA-P0066Creating new instances of java.lang.Boolean
wastes memory, since Boolean
objects are immutable and there are only two useful values of this type.
Integer
/Long
constructor is inefficient, use valueOf
instead JAVA-P0067Using Integer
's default constructor is guaranteed to always result in a new object whereas Integer.valueOf
allows the compiler/class library/JVM to cache values, which is known as interning.
Float
/Double
constructor is inefficient, use valueOf
instead JAVA-P0068Using Float
or Double
's default constructor is guaranteed to always result in a new object whereas the valueOf
method of these classes allows the JVM to cache values, which is known as interning.