Java中输出字符的ASCII值实例
1.我们可以通过将字符强转为int型进行输出那么在控制台中我们将会得到字符的ascii值,这里我们使用nextLine()方法来接收字符串,可以接收空格/Tab键,使用next()方法则不会接收空格/Tab键,但是这里使用nextLine方法不能打印回车键的ascii值因为它遇到回车键就截止接收字符了
2.具体的测试代码如下:
importjava.util.Scanner; publicclassMain{ publicstaticvoidmain(String[]args){ Scannersc=newScanner(System.in); Strings=sc.nextLine(); for(inti=0;i输入:
0123456789
输出:
补充知识:JavaInteger-128~127
今天刷到了一道题,为什么第一个为true,第二个为false。
System.out.println(Integer.valueOf("100")==Integer.valueOf("100"));//true
System.out.println(Integer.valueOf("200")==Integer.valueOf("200"));//false
研究源码发现
/** *ReturnsaIntegerinstancerepresentingthespecified *intvalue. *IfanewIntegerinstanceisnotrequired,thismethod *shouldgenerallybeusedinpreferencetotheconstructor *{@link#Integer(int)},asthismethodislikelytoyield *significantlybetterspaceandtimeperformancebycaching *frequentlyrequestedvalues. * *@paramianint
value. *@returnaIntegerinstancerepresentingi. *@since1.5 */ publicstaticIntegervalueOf(inti){ if(i>=-128&&i<=IntegerCache.high) returnIntegerCache.cache[i+128]; else returnnewInteger(i); }privatestaticclassIntegerCache{ staticfinalinthigh; staticfinalIntegercache[]; static{ finalintlow=-128; //highvaluemaybeconfiguredbyproperty inth=127; if(integerCacheHighPropValue!=null){ //UseLong.decodeheretoavoidinvokingmethodsthat //requireInteger'sautoboxingcachetobeinitialized inti=Long.decode(integerCacheHighPropValue).intValue(); i=Math.max(i,127); //MaximumarraysizeisInteger.MAX_VALUE h=Math.min(i,Integer.MAX_VALUE--low); } high=h; cache=newInteger[(high-low)+1]; intj=low; for(intk=0;kvalueOf会将常用的值(-128to127)cache起来。当i值在这个范围时,会比用构造方法Integer(int)效率和空间上更好。
以上这篇Java中输出字符的ASCII值实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。