当前位置:网站首页>如何将数字字符串转换为整数
如何将数字字符串转换为整数
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() {
}
}
边栏推荐
- asyncio 警告 DeprecationWarning: There is no current event loop
- Gut | 香港中文大学于君组揭示吸烟改变肠道菌群并促进结直肠癌(不要吸烟)
- 反正切熵(Arctangent entropy):2022.7月最新SCI论文
- MATLAB提取不規則txt文件中的數值數據(簡單且實用)
- 进程与线程
- Technical experts from large factories: how can engineers improve their communication skills?
- redis那些事儿
- phpcms 提示信息页面跳转showmessage
- BI技巧丨权限轴
- ASP.NET-酒店管理系统
猜你喜欢

Stack, monotone stack, queue, monotone queue

The five-year itch of software testing engineers tells the experience of breaking through bottlenecks for two years

Application of high-precision indoor positioning technology in safety management of smart factory

Gut | Yu Jun group of the Chinese University of Hong Kong revealed that smoking changes intestinal flora and promotes colorectal cancer (do not smoke)

After a month, I finally got Kingdee offer! Share tetrahedral Sutra + review materials

高精度室内定位技术,在智慧工厂安全管理的应用

AMS Series 1 - AMS startup process

反正切熵(Arctangent entropy):2022.7月最新SCI论文

Résumé des questions d'entrevue (2) Modèle io, ensemble, principe NiO, pénétration du cache, avalanche de rupture

Hal - General
随机推荐
解决undefined reference to `__aeabi_uidivmod‘和undefined reference to `__aeabi_uidiv‘错误
asyncio 警告 DeprecationWarning: There is no current event loop
Inexplicable problems in the nesting of constraintlayout and relativelayout
Key switch: press FN when pressing F1-F12
Ext file system mechanism principle
Balance between picture performance of unity mobile game performance optimization spectrum and GPU pressure
线性表顺序表综合应用题P18
How to become a senior digital IC Design Engineer (1-2) Verilog coding syntax: Verilog 1995, 2001, 2005 standards
Using activity to realize a simple inputable dialog box
图解网络:什么是虚拟路由器冗余协议 VRRP?
Redis things
Encapsulate a koa distributed locking middleware to solve the problem of idempotent or repeated requests
[vtk] interpretation of source code comments of vtkwindowedsincpolydatafilter
一些常用术语
项目管理精华读书笔记(六)
Internet socket (non) blocking write/read n bytes
Oracle 11g single machine cold standby database
Programmers' entrepreneurial trap: taking private jobs
[OBS] encapsulate the basic process of OBS acquisition
进程与线程