本文介绍了Java println(charArray +字符串)vs println(charArray)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有

char c1 = 'S';           // S as a character
char c2 = '\u0068';      // h in Unicode
char c3 = 0x0065;        // e in hexadecimal
char c4 = 0154;          // l in octal
char c5 = (char) 131170; // b, casted (131170-131072=121)
char c6 = (char) 131193; // y, casted (131193-131072=121)
char c7 = '\'';          // ' apostrophe special character
char c8 = 's';           // s as a character
char[] autoDesignerArray = {c1, c2, c3, c4, c5, c6, c7, c8};

还有

System.out.println(autoDesignerArray + "Mustang"); 

输出:[C @ c17164Mustang

Output: [C@c17164Mustang

System.out.println(autoDesignerArray);

输出:谢尔比的

当将char数组与字符串连接在一起时,我不明白为什么会得到奇怪的输出.什么是"[C @ c17164"?内存中的位置?为何我在使用字符串连接时会得到该结果,但我得到单独打印时所期望的结果呢?

I'm not understanding why I get the weird output when I concatenate the char array with a string. What is the "[C@c17164"? The location in memory? And why do I get that when I concatenate with a string, but I get what I would expect when I print it alone?

推荐答案

表达式System.out.println(X + Y)等于表达式System.out.println(X.toString() + Y.toString()).

当您调用System.out.println(autoDesignerArray + "Mustang") autoDesignerArray.toString()(即"[C@c17164")时,它与"Mustang"串联在一起并打印结果.

When you call System.out.println(autoDesignerArray + "Mustang") autoDesignerArray.toString() (which is "[C@c17164") is concatenated with "Mustang" and the result is printed.

这篇关于Java println(charArray +字符串)vs println(charArray)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 21:56