Java中輸出字符的ASCII值實(shí)例
1. 我們可以通過(guò)將字符強(qiáng)轉(zhuǎn)為int型進(jìn)行輸出那么在控制臺(tái)中我們將會(huì)得到字符的ascii值,這里我們使用nextLine()方法來(lái)接收字符串,可以接收空格/Tab鍵,使用next()方法則不會(huì)接收空格/Tab鍵,但是這里使用nextLine方法不能打印回車(chē)鍵的ascii值因?yàn)樗龅交剀?chē)鍵就截止接收字符了
2. 具體的測(cè)試代碼如下:
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
輸出:

補(bǔ)充知識(shí):Java Integer -128~127
今天刷到了一道題,為什么第一個(gè)為true,第二個(gè)為false?! ?/p>
System.out.println(Integer.valueOf("100")==Integer.valueOf("100")); //true
System.out.println(Integer.valueOf("200")==Integer.valueOf("200")); //false
研究源碼發(fā)現(xiàn)
/**
* 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會(huì)將常用的值(-128 to 127)cache起來(lái)。當(dāng)i值在這個(gè)范圍時(shí),會(huì)比用構(gòu)造方法Integer(int)效率和空間上更好。
以上這篇Java中輸出字符的ASCII值實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
詳解Java線(xiàn)程-守護(hù)線(xiàn)程與用戶(hù)線(xiàn)程
這篇文章主要介紹了Java守護(hù)線(xiàn)程與用戶(hù)線(xiàn)程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
java動(dòng)態(tài)構(gòu)建數(shù)據(jù)庫(kù)復(fù)雜查詢(xún)教程
這篇文章主要介紹了java動(dòng)態(tài)構(gòu)建數(shù)據(jù)庫(kù)復(fù)雜查詢(xún)的實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2021-11-11
springboot集成Feign的實(shí)現(xiàn)示例
Feign是聲明式HTTP客戶(hù)端,用于簡(jiǎn)化微服務(wù)之間的REST調(diào)用,本文就來(lái)介紹一下springboot集成Feign的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-11-11
win10 下 idea2020安裝 JetBrains-agent.jar 包后閃退的問(wèn)題及解決辦法
這篇文章主要介紹了win10 下 idea2020安裝 JetBrains-agent.jar 包后閃退的解決辦法,本文給大家?guī)?lái)原因分析及解決方法,需要的朋友可以參考下2020-08-08
Springboot新建項(xiàng)目Spring Initializr Error問(wèn)題及解決
這篇文章主要介紹了Springboot新建項(xiàng)目Spring Initializr Error問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
Java Swing最詳細(xì)基礎(chǔ)知識(shí)總結(jié)
這篇文章主要介紹了Java Swing最詳細(xì)基礎(chǔ)知識(shí)總結(jié),文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)Java Swing的小伙伴們有很好的幫助,需要的朋友可以參考下2021-05-05
Spring中自定義數(shù)據(jù)類(lèi)型轉(zhuǎn)換的方法詳解
Spring3引入了一個(gè)core.onvert包,提供一個(gè)通用類(lèi)型轉(zhuǎn)換系統(tǒng)。在Spring容器中,可以使用這個(gè)系統(tǒng)作為PropertyEditor實(shí)現(xiàn)的替代,將外部化的bean屬性值字符串轉(zhuǎn)換為所需的屬性類(lèi)型。本文將詳解這一系統(tǒng)的使用方法,需要的可以參考一下2022-06-06

