当前位置:网站首页>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() {
}
}
边栏推荐
- uniapp scroll view 解决高度自适应、弹框滚动穿透等问题。
- 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
- Qt OpenGL 纹理贴图
- STL教程8-map
- The tutor put forward 20 pieces of advice to help graduate students successfully complete their studies: first, don't plan to take a vacation
- typeScript
- OpenStack中的测试分类
- Momentum of vulnhub
- Concurrent programming - singleton
- uniapp实现点击加载更多
猜你喜欢
随机推荐
XML (DTD, XML parsing, XML modeling)
Differences between MySQL Union and union all
R语言ggplot2可视化:gganimate包创建动态折线图动画(gif)、使用transition_reveal函数在动画中沿给定维度逐步显示数据、在折线移动方向添加数据点
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之cereal
Based on MCU, how to realize OTA differential upgrade with zero code and no development?
Qt OpenGL相机的使用
R语言使用原生包(基础导入包、graphics)中的hist函数可视化直方图(histogram plot)
基于turtlebot3实现SLAM建图及自主导航仿真
Hongmeng third training (project training)
win10 上PHP artisan storage:link 出现 symlink (): Protocol error的解决办法
vulnhub之narak
Understand go language context in one article
鸿蒙第四次培训
并发编程-单例
Modular programming of single chip microcomputer
R language uses data The table package performs data aggregation statistics, calculates window statistics, calculates the median of sliding groups, and merges the generated statistical data into the o
MCDF实验1
DNS multi-point deployment IP anycast+bgp actual combat analysis
(数据库提权——Redis)Redis未授权访问漏洞总结
![[learning notes] DP status and transfer](/img/5e/59c64d2fe08b89fba2d7e1e6de2761.png)







