Java中输出字符的ASCII值实例
作者:smalllxp 发布时间:2023-02-27 08:59:11
标签:Java,字符,ASCII值
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
0
投稿
猜你喜欢
- 实际项目中pom.xml依赖写法: <dependency> <groupId>org.springf
- 本文实例为大家分享了Android App获取屏幕旋转角度的具体代码,供大家参考,具体内容如下一、获取屏幕旋转角度的方法是:int rota
- 本文主要介绍了关于单例模式的一些问题,想学习C#单例模式的同学们可以看一看,还是有些帮助c#中的单例模式单例模式是指在设计一个类时,保证在运
- @Autowired注解和静态方法及new关系一、@autowired 与new new出来的对象无法调用@Autowired注入
- ref和out都是C#中的关键字,所实现的功能也差不多,都是指定一个参数按照引用传递。对于编译后的程序而言,它们之间没有任何区别,也就是说它
- 实现消息队列的两种方式Apache ActiveMQ官方实例发送消息直接在Apache官网http://activemq.apache.or
- 本文就是会将数组里面的单词进行倒序排列 例如 how old are you -> you are old how示例程序输出结果:t
- 我们在想对一个可枚举的对象集合进行去重操作时,一般第一个想到的就是就是Linq的Distinct方法。先定义一个类,然后使用Distinct
- 目录源码使用源码@Target({ElementType.TYPE, ElementType.METHOD})@Retention(Rete
- 一、表创建一、表创建//创建一个空表DataTable dt = new DataTable();//创建一个名为"Table_N
- 本文实例为大家分享了Rxjava实现轮询定时器的具体代码,供大家参考,具体内容如下作用1、实现了延迟若干毫秒后,执行next操作,只执行一次
- 主要从以下几方面来介绍一下@Scope注解@Scope注解是什么@Scope注解怎么使用@Scope注解的使用场景1,@Scope注解是什么
- 最近被.net winform中的控件布局搞困惑了,由于控件都是使用Dock方式的,操作起来也是比较方便,如果最大化,窗口大小调整等,都可以
- 用VS2010 + OpenCV 2.4.9 实现简单人脸识别,供大家参考,具体内容如下首先放效果图(为了防止辣眼睛,后期处理了下):首先声
- 🌱1. 什么是反射机制?首先大家应该先了解两个概念,编译期和运行期,编译期就是编译器帮你把源代码翻译成机器能识别的代码,比如编译器把java
- 什么是容器?在Java的GUI界面设计中,关于容器的理解,从字面意思我们就可以认为它是存放控件的地方,而这个地方依托在窗体之上,常用的容器是
- 在日常开发中经常遇到控件不能随着父容器大小的改变而且自动改变控件的所在位置和大小。以下是实现的代码 /// <summary>
- 一、什么是异常异常机制:异常机制是指当程序出现错误后,程序如何处理。具体来说就是程序发生异常,异常机制提供程序的退出安全通道。通俗来说:就是
- 目录1、this代表了()的对象引用,super表示的是当前对象的()对象?2、输出内容是:3、下面程序的输出是:()4、执行下列代码的输出
- 出现乱码请修改为BufferedReader br = new BufferedReader(new InputStreamReader(n