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)效率和空间上更好。

以上这篇Java中输出字符的ASCII值实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • java读取文件:char的ASCII码值=65279,显示是一个空字符的解决

    java读取文件遇到个很神奇的问题,String str1 = "1"; String str2 = "1"; str1 .equals(str1 ) 返回值是false.且他们的长度是不一样的. 这是个用眼睛看不见的问题,因为有个空字符.这里要去掉他 补充知识:ArrayList.addAll(),clone()都是浅克隆,改变一个集合,另一个会跟着改变 ArrayList.addAll(),clone()都是浅克隆. (浅克隆:当改变克隆对象的引用类型的值(注意

  • JAVA实现打印ascii码表代码

    我就废话不多说了,大家还是直接看代码吧~ package com.jalor; public class AAAA { public static void main(String[] args) { outputA(65); outputA(97); } // 打印ascii码表 public static void outputA(int count){ for (int i = 0; i < 26; i++) { System.out.print((char)(count+ i)); }

  • java中char对应的ASCII码的转化操作

    java中,char类型变量可以强制转化为int类型变量,int类型变量也可以强制转化成char类型的变量: char c='a'; int i=98; System.out.println((int)c); System.out.println((char)i); 对于数组类型,其下标为int类型,所以可以直接使用char类型变量,默认强制转换: int[] array=new int[100]; for(int i=0;i<array.length;i++){ array[i]=i; } c

  • Java中异常打印输出的常见方法总结

    前言 Java异常是在Java应用中的警报器,在出现异常的情况下,可以帮助我们程序猿们快速定位问题的类型以及位置.但是一般在我们的项目中,由于经验阅历等多方面的原因,依然有若干的童鞋在代码中没有正确的使用异常打印方法,导致在项目的后台日志中,没有收到日志或者日志信息不完整等情况的发生,这些都给项目埋下了若干隐患.本文将深入分析在异常日志打印过程中的若干情况,并给出若干的使用建议. 1. Java异常Exception的结构分析 我们通常所说的Exception主要是继承于Throwable而来,

  • 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

  • java中判断字段真实长度的实例(中文2个字符,英文1个字符)

    实例如下: public class Char_cn { public static void main(String[] args) { // TODO Auto-generated method stub String haha = "我叫兜兜abcd"; int true_num = String_length(haha); System.out.println("true" + true_num); int false_num = haha.length()

  • java中快速创建带初始值的List和Map实例

    初始化一个List和Map对象并为期加入值的写法如下: List<String> sList = new ArrayList<String>(); sList.add("str1"); sList.add("str2"); Map<String,String> sMap = new HashMap<String, String>(); sMap.put("k1", "v1");

  • Java中IO流解析及代码实例详解

    目录 1.IO流 1.流和流的分类 什么是IO流? IO流的分类? java.io包下需要掌握的流有16个: 2.如何使用流 1.输入流(读文件):FileInputStream 2.输出流(写文件):FileOutputStream 3.文件的拷贝 总结 1.IO流 1.流和流的分类 什么是IO流? I:Input (输入) O: Ouput(输出) IO流的分类? 有多种分类方式: 一种方式是按照流的方向进行分类: 以内存作为参照物 往内存中去,叫做输入(Input).或者叫做读(Read)

  • java中string.trim()函数的作用实例及源码

    trim()的作用:去掉字符串首尾的空格. public static void main(String arg[]){ String a=" hello world "; String b="hello world"; System.out.println(b.equals(a)); a=a.trim(); //去掉字符串首尾的空格 System.out.println(a.equals(b)); } 执行结果: a: hello world ,false a:h

  • java 中 String format 和Math类实例详解

    java 中 String format 和Math类实例详解 java字符串格式化输出 @Test public void test() { // TODO Auto-generated method stub //可用printf(); System.out.println(String.format("I am %s", "jj")); //%s字符串 System.out.println(String.format("首字母是 %c",

  • Java中io流解析及代码实例

    IO流 Java中IO流分为两种,字节流和字符流,顾名思义字节流就是按照字节来读取和写入的,字符刘是按照字符来存取的:常用的文件读取用的就是字符流,在网络通信里面用的就是字节流 下面这张图是Java中IO流的总体框架: 字节流 Java中字节流一般都是以stream结尾的,输入的字节流叫InputStream,输出字节流叫OutputStream;InputStream和OutputStream是表示自己输入/输出的所有类的超类,是抽象类(abstract) 常用的字节流有: 1.FileInp

  • 浅谈Java中hashCode的正确求值方法

    本文研究的主要是Java中hashCode的正确求值方法的相关内容,具体如下. 散列表有一项优化,可以将对象的散列码(hashCode)缓存起来,如果散列码不匹配,就不会检查对象的等同性而直接认为成不同的对象.如果散列码(hashCode)相等,才会检测对象是否相等(equals). 如果对象具有相同的散列码(hashCode),他们会被映射到同一个散列桶中.如果散列表中所有对象的散列码(hashCode)都一样,那么该散列表就会退化为链表(linked list),从而大大降低其查询效率. 一

  • Java中final关键字详解及实例

    final在Java中可以声明成员变量.方法.类以及本地变量.一旦你将引用声明作final,你将不能改变这个引用了,如果你试图将变量再次初始化的话,编译器会报编译错误.  final的含义在不同的场景下有细微的差别,但总体来说,它指的是"不可变". 1. final变量 凡是对成员变量或者本地变量(在方法中的或者代码块中的变量称为本地变量)声明为final的都叫作final变量.final变量经常和static关键字一起使用,作为常量.用final关键字修饰的变量,只能进行一次赋值操作

随机推荐