Java中输出字符的ASCII值实例
作者:smalllxp 时间:2023-02-27 08:59:11
1. 我们可以通过将字符强转为int型进行输出那么在控制台中我们将会得到字符的ascii值,这里我们使用nextLine()方法来接收字符串,可以接收空格/Tab键,使用next()方法则不会接收空格/Tab键,但是这里使用nextLine方法不能打印回车键的ascii值因为它遇到回车键就截止接收字符了
2. 具体的测试代码如下:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
for(int i = 0; i < s.length(); i++){
System.out.println((int)s.charAt(i));
}
sc.close();
}
}
输入:
0123456789
输出:
补充知识:Java Integer -128~127
今天刷到了一道题,为什么第一个为true,第二个为false。
System.out.println(Integer.valueOf("100")==Integer.valueOf("100"));//true
System.out.println(Integer.valueOf("200")==Integer.valueOf("200"));//false
研究源码发现
/**
* Returns a <tt>Integer</tt> instance representing the specified
* <tt>int</tt> value.
* If a new <tt>Integer</tt> instance is not required, this method
* should generally be used in preference to the constructor
* {@link #Integer(int)}, as this method is likely to yield
* significantly better space and time performance by caching
* frequently requested values.
*
* @param i an <code>int</code> value.
* @return a <tt>Integer</tt> instance representing <tt>i</tt>.
* @since 1.5
*/
public static Integer valueOf(int i) {
if(i >= -128 && i <= IntegerCache.high)
return IntegerCache.cache[i + 128];
else
return new Integer(i);
}
private static class IntegerCache {
static final int high;
static final Integer cache[];
static {
final int low = -128;
// high value may be configured by property
int h = 127;
if (integerCacheHighPropValue != null) {
// Use Long.decode here to avoid invoking methods that
// require Integer's autoboxing cache to be initialized
int i = Long.decode(integerCacheHighPropValue).intValue();
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - -low);
}
high = h;
cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
}
private IntegerCache() {}
}
valueOf会将常用的值(-128 to 127)cache起来。当i值在这个范围时,会比用构造方法Integer(int)效率和空间上更好。
来源:https://blog.csdn.net/qq_39445165/article/details/84882569
标签:Java,字符,ASCII值
0
投稿
猜你喜欢
深入讲解我们说的CAS自旋锁到底是什么
2022-05-21 14:43:08
详解Asp.Net MVC的Bundle捆绑
2021-09-10 05:21:38
Android开发改变字体颜色方法
2022-11-10 05:51:01
SpringBoot项目启动时如何读取配置以及初始化资源
2021-11-19 04:04:11
Java使用 try-with-resources 实现自动关闭资源的方法
2022-01-09 06:54:46
Spring项目中使用Junit单元测试并配置数据源的操作
2022-06-02 05:32:27
java实现删除某条信息并刷新当前页操作
2022-06-26 07:12:12
springboot整合腾讯云短信开箱即用的示例代码
2023-04-02 06:06:38
Android自定义有限制区域的图例角度自识别涂鸦工具类完结篇
2022-10-22 23:05:13
Unity实现仿3D轮转图效果
2023-11-24 12:26:56
C#异步使用需要注意的几个问题
2023-03-05 04:33:08
JAVA包装类及自动封包解包实例代码
2022-10-23 13:28:54
Java中ArrayList初始化的四种方法详解
2022-03-29 21:50:13
Android 7.0 运行时权限弹窗问题的解决
2023-06-30 18:26:13
Android使用FontMetrics对象计算位置坐标
2023-02-06 15:35:20
C# 并行和多线程编程——认识和使用Task
2022-03-28 05:48:04
Android自带emoji表情的使用方法详解
2021-11-25 09:28:22
c#入门之类型转换详解
2022-03-17 01:36:52
SpringBoot中Dozer的使用小结
2023-11-25 01:24:38
C#微信开发之启用开发者模式
2022-07-07 11:24:54