当前位置:网站首页>How to convert a numeric string to an integer
How to convert a numeric string to an integer
2022-07-03 11:56:00 【zhang__ one thousand two hundred and thirty-four】
Such as "12345", Convert to 12345 This form
package com.isea.java;
public class Test {
public static void main(String[] args) {
String str = "1234";
Integer num1 = new Integer(str);
int num2 = Integer.parseInt(str);
Integer num3 = Integer.valueOf(str);
System.out.println(num1 + "\t" + num2 + "\t" + num3);//1234 1234 1234
}
}
The above three methods are all acceptable
About Integer Class should know
Integer a=1;
Integer b=1;
System.out.println(a==b);//true
Integer c=128;
Integer d=128;
System.out.println(c==d);//false
The first output true, The second output false
Because for Integer class When the number is in -128 To 127 In between It can be taken out directly
Here is the source code involves Integer There is a cache
When more than 127 When Equivalent to new Integer(x) 了 == Compared with address So the second one is false
private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer cache[];
static {
// high value may be configured by property
int h = 127;
String integerCacheHighPropValue =
VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
try {
int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
} catch( NumberFormatException nfe) {
// If the property cannot be parsed into an int, ignore it.
}
}
high = h;
cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
// range [-128, 127] must be interned (JLS7 5.1.7)
assert IntegerCache.high >= 127;
}
private IntegerCache() {
}
}
边栏推荐
- R语言使用gridExtra包的grid.arrange函数将ggplot2包的多个可视化图像横向组合起来,ncol参数自定义组合图列数、nrow参数自定义组合图行数
- vulnhub之Ripper
- R语言使用data.table包进行数据聚合统计计算滑动窗口统计值(Window Statistics)、计算滑动分组中位数(median)并合并生成的统计数据到原数据集中
- R language uses grid of gridextra package The array function combines multiple visual images of the lattice package horizontally, and the ncol parameter defines the number of columns of the combined g
- Vulnhub's Nagini
- STL Tutorial 9 deep copy and shallow copy of container elements
- Cacti监控Redis实现过程
- R language ggplot2 visualization: gganimate package creates dynamic line graph animation (GIF) and uses transition_ The reveal function displays data step by step along a given dimension in the animat
- R language uses the aggregate function to calculate the mean value (sum) of dataframe data grouping aggregation without setting na The result of RM calculation. If the group contains the missing value
- Kibana~Kibana的安装和配置
猜你喜欢

XML (DTD, XML parsing, XML modeling)

During FTP login, the error "530 login incorrect.login failed" is reported

STL Tutorial 9 deep copy and shallow copy of container elements

错排问题 (抽奖,发邮件)

Raven2 of vulnhub

Excel表格转到Word中,表格不超边缘纸张范围

Qt OpenGL 旋转、平移、缩放

vulnhub之tomato(西红柿)

(数据库提权——Redis)Redis未授权访问漏洞总结

Quantitative calculation research
随机推荐
Hongmeng fourth training
Vulnhub pyexp
银泰百货点燃城市“夜经济”
Notes on 32-96 questions of sword finger offer
【学习笔记】dp 状态与转移
Momentum of vulnhub
鸿蒙第三次培训(项目实训)
Quantitative calculation research
Based on MCU, how to realize OTA differential upgrade with zero code and no development?
DNS多点部署IP Anycast+BGP实战分析
OpenGL 绘制彩色的三角形
vulnhub之Nagini
Cacti监控Redis实现过程
Download address and installation tutorial of vs2015
The excel table is transferred to word, and the table does not exceed the edge paper range
Dynamic programming (interval DP)
R语言使用gridExtra包的grid.arrange函数将lattice包的多个可视化图像横向组合起来,ncol参数自定义组合图列数、nrow参数自定义组合图行数
OpenGL 索引缓存对象EBO和线宽模式
Solve msvcp120d DLL and msvcr120d DLL missing
OPenGL 基本知识(根据自己理解整理)