当前位置:网站首页>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() {
}
}
边栏推荐
- Colleagues wrote a responsibility chain model, with countless bugs
- OpenGL 绘制彩色的三角形
- Excel表格转到Word中,表格不超边缘纸张范围
- Cacti监控Redis实现过程
- Yintai department store ignites the city's "night economy"
- Groovy test class and JUnit test
- 牛牛的组队竞赛
- The world's most popular font editor FontCreator tool
- Concurrent programming - singleton
- Capturing and sorting out external Fiddler -- Conversation bar and filter [2]
猜你喜欢
STL tutorial 10 container commonalities and usage scenarios
同事写了一个责任链模式,bug无数...
Visual studio 2022 downloading and configuring opencv4.5.5
Download address and installation tutorial of vs2015
Duplicate numbers in the array of sword finger offer 03
Wrong arrangement (lottery, email)
Excel表格转到Word中,表格不超边缘纸张范围
STL Tutorial 9 deep copy and shallow copy of container elements
This article explains the complex relationship between MCU, arm, MCU, DSP, FPGA and embedded system
Kibana - installation and configuration of kibana
随机推荐
OpenStack中的测试分类
Simple factory and factory method mode
Sheet1$. Output [excel source output] Error in column [xxx]. The returned column status is: "the text is truncated, or one or more characters have no matches in the target code page.".
Colleagues wrote a responsibility chain model, with countless bugs
uniapp scroll view 解决高度自适应、弹框滚动穿透等问题。
量化计算调研
R语言使用data.table包进行数据聚合统计计算滑动窗口统计值(Window Statistics)、计算滑动分组中位数(median)并合并生成的统计数据到原数据集中
Duplicate numbers in the array of sword finger offer 03
Interview experience in summer camp of Central South University in 2022
Groovy测试类 和 Junit测试
抓包整理外篇fiddler———— 会话栏与过滤器[二]
STL教程9-容器元素深拷贝和浅拷贝问题
vulnhub之GeminiInc
The tutor put forward 20 pieces of advice to help graduate students successfully complete their studies: first, don't plan to take a vacation
DNS多点部署IP Anycast+BGP实战分析
PHP导出word方法(一mht)
vulnhub之tomato(西红柿)
Kibana - installation and configuration of kibana
rxjs Observable filter Operator 的实现原理介绍
同事写了一个责任链模式,bug无数...