Java 使用显式类型转换将十六进制整数转换为字符?

Java 使用显式类型转换将十六进制整数转换为字符?,java,type-conversion,hex,Java,Type Conversion,Hex,我对以下内容进行了编码,但o/p不是预期值?有人引导我吗 问题:编写一个示例程序来声明十六进制整数,并使用显式类型转换将其转换为字符 class hexa { public static void main(String ar[]) { int hex=0xA; System.out.println(((char)hex)); } } 请告诉我: 为什么产出有差异 /*code 1*/ int hex = (char)0xA; System.out.println(hex);

我对以下内容进行了编码,但o/p不是预期值?有人引导我吗

问题:编写一个示例程序来声明十六进制整数,并使用显式类型转换将其转换为字符

class hexa
{
public static void main(String ar[])
{
    int hex=0xA;
    System.out.println(((char)hex));
}
}
请告诉我:
为什么产出有差异

/*code 1*/
int hex = (char)0xA; 
System.out.println(hex); 
/*code 2*/
int hex = 0xA; 
System.out.println((char)hex);
十六进制值0xA(或十进制10)在ASCII中为“\n”(换行字符)。
因此,输出

编辑(感谢您在评论中提供更正:

int hex = (char) 0xA;
System.out.println(hex); //here value of hex is '10', type of hex is 'int', the overloaded println(int x) is invoked.

int hex = 0xA;
System.out.println((char) hex); //this is equivalent to System.out.println( '\n' ); since the int is cast to a char, which produces '\n', the overloaded println(char x) is invoked.

我想你想打印字母
A
。 不要使用
打印


输出是什么?您的期望是什么?为什么输出有差异?/*代码1*/int hex=(char)0xA;System.out.println(hex);/*代码2*/int hex=0xA;System.out.println((char)hex);如果是这种情况,那么%n doesNewline是什么呢?没有这个printf更像是
print
,而不是
println
。为什么在输出上有差异/*code 1*/int hex=(char)0xA;System.out.println(hex);/*code 2*/int hex=0xA;System.out.println((char)hex);关于您的编辑:在您编写的代码注释中,在
int
char
上调用了
toString
,但这是不正确的。没有自动装箱功能,但调用了
println
的适当重载版本和类型。
int hex = (char) 0xA;
System.out.println(hex); //here value of hex is '10', type of hex is 'int', the overloaded println(int x) is invoked.

int hex = 0xA;
System.out.println((char) hex); //this is equivalent to System.out.println( '\n' ); since the int is cast to a char, which produces '\n', the overloaded println(char x) is invoked.
int hex=0xA;
System.out.printf("%X%n", hex);