Skip to content

Commit b29fbad

Browse files
stsypanovcl4es
authored andcommitted
8267844: Replace Integer/Long.valueOf() with Integer/Long.parse*() where applicable
Reviewed-by: redestad
1 parent d38b314 commit b29fbad

File tree

6 files changed

+31
-13
lines changed

6 files changed

+31
-13
lines changed

src/java.base/share/classes/com/sun/crypto/provider/CipherCore.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,7 @@ private static int getNumOfUnit(String mode, int offset, int blockSize)
201201
if (mode.length() > offset) {
202202
int numInt;
203203
try {
204-
Integer num = Integer.valueOf(mode.substring(offset));
205-
numInt = num.intValue();
204+
numInt = Integer.parseInt(mode.substring(offset));
206205
result = numInt >> 3;
207206
} catch (NumberFormatException e) {
208207
throw new NoSuchAlgorithmException

src/java.base/share/classes/java/lang/Integer.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,7 +1413,7 @@ public static Integer decode(String nm) throws NumberFormatException {
14131413
int radix = 10;
14141414
int index = 0;
14151415
boolean negative = false;
1416-
Integer result;
1416+
int result;
14171417

14181418
if (nm.isEmpty())
14191419
throw new NumberFormatException("Zero length string");
@@ -1443,15 +1443,15 @@ else if (nm.startsWith("0", index) && nm.length() > 1 + index) {
14431443
throw new NumberFormatException("Sign character in wrong position");
14441444

14451445
try {
1446-
result = Integer.valueOf(nm.substring(index), radix);
1447-
result = negative ? Integer.valueOf(-result.intValue()) : result;
1446+
result = parseInt(nm, index, nm.length(), radix);
1447+
result = negative ? -result : result;
14481448
} catch (NumberFormatException e) {
14491449
// If number is Integer.MIN_VALUE, we'll end up here. The next line
14501450
// handles this case, and causes any genuine format error to be
14511451
// rethrown.
14521452
String constant = negative ? ("-" + nm.substring(index))
14531453
: nm.substring(index);
1454-
result = Integer.valueOf(constant, radix);
1454+
result = parseInt(constant, radix);
14551455
}
14561456
return result;
14571457
}

src/java.base/share/classes/java/lang/Long.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,7 +1254,7 @@ public static Long decode(String nm) throws NumberFormatException {
12541254
int radix = 10;
12551255
int index = 0;
12561256
boolean negative = false;
1257-
Long result;
1257+
long result;
12581258

12591259
if (nm.isEmpty())
12601260
throw new NumberFormatException("Zero length string");
@@ -1284,15 +1284,15 @@ else if (nm.startsWith("0", index) && nm.length() > 1 + index) {
12841284
throw new NumberFormatException("Sign character in wrong position");
12851285

12861286
try {
1287-
result = Long.valueOf(nm.substring(index), radix);
1288-
result = negative ? Long.valueOf(-result.longValue()) : result;
1287+
result = parseLong(nm, index, nm.length(), radix);
1288+
result = negative ? -result : result;
12891289
} catch (NumberFormatException e) {
12901290
// If number is Long.MIN_VALUE, we'll end up here. The next line
12911291
// handles this case, and causes any genuine format error to be
12921292
// rethrown.
12931293
String constant = negative ? ("-" + nm.substring(index))
12941294
: nm.substring(index);
1295-
result = Long.valueOf(constant, radix);
1295+
result = parseLong(constant, radix);
12961296
}
12971297
return result;
12981298
}

src/java.base/share/classes/jdk/internal/misc/VM.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,8 @@ public static void saveProperties(Map<String, String> props) {
272272
s = props.get("java.class.version");
273273
int index = s.indexOf('.');
274274
try {
275-
classFileMajorVersion = Integer.valueOf(s.substring(0, index));
276-
classFileMinorVersion = Integer.valueOf(s.substring(index+1, s.length()));
275+
classFileMajorVersion = Integer.parseInt(s.substring(0, index));
276+
classFileMinorVersion = Integer.parseInt(s.substring(index + 1));
277277
} catch (NumberFormatException e) {
278278
throw new InternalError(e);
279279
}

test/micro/org/openjdk/bench/java/lang/Integers.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ public void parseInt(Blackhole bh) {
7676
}
7777
}
7878

79+
@Benchmark
80+
public void decode(Blackhole bh) {
81+
for (String s : strings) {
82+
bh.consume(Integer.decode(s));
83+
}
84+
}
85+
7986
/** Performs toString on small values, just a couple of digits. */
8087
@Benchmark
8188
public void toStringSmall(Blackhole bh) {

test/micro/org/openjdk/bench/java/lang/Longs.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.openjdk.jmh.annotations.Warmup;
3636
import org.openjdk.jmh.infra.Blackhole;
3737

38+
import java.util.concurrent.ThreadLocalRandom;
3839
import java.util.concurrent.TimeUnit;
3940

4041
@BenchmarkMode(Mode.AverageTime)
@@ -48,16 +49,20 @@ public class Longs {
4849
@Param("500")
4950
private int size;
5051

52+
private String[] strings;
5153
private long[] longArraySmall;
5254
private long[] longArrayBig;
5355

5456
@Setup
5557
public void setup() {
58+
var random = ThreadLocalRandom.current();
59+
strings = new String[size];
5660
longArraySmall = new long[size];
5761
longArrayBig = new long[size];
5862
for (int i = 0; i < size; i++) {
63+
strings[i] = "" + (random.nextLong(10000) - 5000);
5964
longArraySmall[i] = 100L * i + i + 103L;
60-
longArrayBig[i] = ((100L * i + i) << 32) + 4543 + i * 4;
65+
longArrayBig[i] = ((100L * i + i) << 32) + 4543 + i * 4L;
6166
}
6267
}
6368

@@ -69,6 +74,13 @@ public void toStringSmall(Blackhole bh) {
6974
}
7075
}
7176

77+
@Benchmark
78+
public void decode(Blackhole bh) {
79+
for (String s : strings) {
80+
bh.consume(Long.decode(s));
81+
}
82+
}
83+
7284
/** Performs toString on large values, around 10 digits. */
7385
@Benchmark
7486
public void toStringBig(Blackhole bh) {

0 commit comments

Comments
 (0)