Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中的System.out.write()不打印整数值中的最低有效值_Java_String_Character Encoding - Fatal编程技术网

Java中的System.out.write()不打印整数值中的最低有效值

Java中的System.out.write()不打印整数值中的最低有效值,java,string,character-encoding,Java,String,Character Encoding,这是我的代码: class writedemo { public static void main(String args[]){ int b; b=1; System.out.write(b); System.out.write('\n'); } } 我得到的输出是“apl函数四题”字符(U+2370) 但这一准则起了作用: class writedemo{ public static void main(Str

这是我的代码:

class writedemo {
    public static void main(String args[]){
      int b;
      b=1;
      System.out.write(b);
      System.out.write('\n');    
    }
}
我得到的输出是“apl函数四题”字符(U+2370)

但这一准则起了作用:

class writedemo{
    public static void main(String args[]){
      int b;
      b='A';
      System.out.write(b);
      System.out.write('\n');    
    }
}

它打印字符
'A'
。有人能帮我吗?我遗漏了什么吗?

如果将字符值
'a'
传递给
int
,则会保存数值。根据ASCII表格,字形
'A'
转换为
65
作为十进制值。下面是方法
System.out::print
System.out::write
之间的区别,这可能会让人困惑:

  • System.out.println(b)
    打印
    65
    ,因为在
    x
    中被理解为一个整数: 打印一个整数,然后终止该行

  • System.out.write(b)打印
    A
    ,因为在
    b
    中被理解为字节:

    将指定的字节写入此流。如果字节是换行符并且启用了自动刷新,则将调用刷新方法

    请注意,字节是按给定方式写入的;要编写将根据平台的默认字符编码进行翻译的字符,请使用print(char)或println(char)方法

System.out.write()存储并打印ASCII值。可以使用System.out.print()显示整数值。两者的区别如下所示:

System.out.write(65);  //will print ASCII value of 65 which is A;
System.out.print(65); // will print just 65

正如马恩杰在他的报告中提到的那样

write(int)将参数解释为要使用的单个字符 printed,而print(int)将整数转换为字符 一串write(49)打印“1”,而print(49)打印“49”


您希望打印什么?是否希望实现将1打印到控制台?是的,我希望打印1。谢谢,但是当我将整数值“1”传递到写入(int b)时。它真的应该打印它的最低有效字节的ASCII等价物吗?@VaibhavRanjith:你通过
'1'
还是
1
?-这有很大的区别!如果
'1'
'1'
作为字符传递给
write(int b)
,字符
'1'
将转换为
49
的ASCII十进制值,该方法将其转换回并打印出
1
。如果你将
1
作为一个整数传递给同一个方法,字符将被打印,因为它在ASCII表中的数字DEC
1
下。是的,这也是我所期望的。但代码会打印U+2370字符,称为应用程序功能四元问题字符。是的,因为IDE无法正确显示这些字符,并将其替换为该字符。