当前位置:网站首页>如何将数字字符串转换为整数
如何将数字字符串转换为整数
2022-07-03 10:23:00 【zhang__1234】
如"12345",转换为12345这种形式
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
}
}
以上三种方式均可
关于Integer类应该知道的
Integer a=1;
Integer b=1;
System.out.println(a==b);//true
Integer c=128;
Integer d=128;
System.out.println(c==d);//false
第一个输出true,第二个输出false
因为对于Integer类 当数字处于-128到127之间的时候 是可以直接取出的
以下为源码 涉及到Integer里面的一个缓存
当大于127的时候 就相当于new Integer(x)了 ==比较的为地址 所以第二个为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() {
}
}
边栏推荐
- JGG专刊征稿:时空组学
- POI excel 单元格换行
- Key switch: press FN when pressing F1-F12
- Hal -- writing hardware drivers
- 数据库增量备份 - DB INCR DB FULL
- [vtk] interpretation of source code comments of vtkwindowedsincpolydatafilter
- 2. Hal hardware abstraction layer
- 行业唯一!法大大电子合同上榜36氪硬核科技企业
- Tablespace creation management and control file management
- 浅析-JMM内存模型
猜你喜欢
高精度室内定位技术,在智慧工厂安全管理的应用
Summary of interview questions (2) IO model, set, NiO principle, cache penetration, breakdown avalanche
Tencent micro app to get wechat user information
Multi dimensional monitoring: the data base of intelligent monitoring
Google Earth engine (GEE) - ghsl global population grid dataset 250 meter resolution
Processes and threads
How to clean up v$rman_ backup_ job_ Details view reports error ora-02030
AMS Series 1 - AMS startup process
PHP server interacts with redis with a large number of close_ Wait analysis
MATLAB提取不规则txt文件中的数值数据(简单且实用)
随机推荐
程序进程管理工具-go supervisor
AIDL
Hal - General
Unity移动端游戏性能优化简谱之 画面表现与GPU压力的权衡
如何成为一名高级数字 IC 设计工程师(1-4)Verilog 编码语法篇:表达式
用了这么久线程池,你真的知道如何合理配置线程数吗?
Commonly used discrete random distribution
00后抛弃互联网: 毕业不想进大厂,要去搞最潮Web3
Program process management tool -go Supervisor
一文搞懂Go语言Context
数据库增量备份 - DB INCR DB FULL
2022-07-02: what is the output of the following go language code? A: Compilation error; B:Panic; C:NaN。 package main import “fmt“ func mai
Using activity to realize a simple inputable dialog box
C语言日志库zlog基本使用
Double linked list of linear list
Google Earth engine (GEE) - ghsl global population grid dataset 250 meter resolution
英特尔13代酷睿旗舰曝光,单核5.5GHz
Using onvif protocol to operate the device
解决undefined reference to `__aeabi_uidivmod‘和undefined reference to `__aeabi_uidiv‘错误
历经一个月,终于拿到金蝶Offer!分享一下四面面经+复习资料