什么是一个打印的字符数组的输出是什么意思? [英] What does the output of a printed character array mean?
问题描述
我从C现在转向Java和我以下有关弦乐一些教程。在教程一个点,他们从显示的字符数组初始化一个新的字符串,然后打印字符串。我正沿着以下,但我想同时打印字符数组和字符串所以我想这样的:
类无论{
公共静态无效的主要(字符串ARGS []){
的char []你好= {'H','E','L','L','O','。'};
字符串串hello_str =新的String(你好);
的System.out.println(你好++串hello_str);
}
}
我的产量是这样的:
[C @ 9304b1打招呼。
显然,这是不是你将如何打印在Java中的字符数组。但是我不知道是否我刚刚得到的垃圾?我看了一些网站,打印字符数组给你一个地址,但是,这并不像一个地址给我...我还没有发现在网上有很多关于它的。
所以,我怎么只打印?
结果和奖金的问题:
你如何正确地在Java打印的字符数组?
但我不知道是否我刚刚得到的垃圾?
块引用>没有,你得到的
Object.toString()
,这是不是在阵列覆盖:
Object类的toString方法返回一个包含其中的对象是一个实例,该符号字符'@'的类名称的字符串,散列$ C的无符号十六进制再presentation对象$ C。换句话说,该方法返回字符串等于的值:
的getClass()。的getName()+'@'+ Integer.toHexString(哈希code())
块引用>所以它不是的垃圾的,因为它有一个意思......但它不是一个特别的有用的价值,无论是。
和您的奖金的问题...
你如何正确地在Java打印的字符数组?
块引用>呼叫的
Arrays.toString(的char [])
将其转换为字符串...或只是的System.out.println(你好);
这将调用
的println(的char [])
来代替,将其转换成字符串。需要注意的是Arrays.toString
将建立一个字符串,这显然是一个字符数组,而的System.out.println(你好)
大致等同于的System.out.println(新的String(你好))
I'm moving from C to Java now and I was following some tutorials regarding Strings. At one point in the tutorials they showed instantiating a new string from a character array then printing the string. I was following along, but I wanted to print both the character array and the string so I tried this:
class Whatever { public static void main(String args[]) { char[] hello = { 'h', 'e', 'l', 'l', 'o', '.'}; String hello_str = new String(hello); System.out.println(hello + " " + hello_str); } }
My output was something like this:
[C@9304b1 hello.
Clearly, this is not how you would print a character array in Java. However I'm wondering if I just got garbage? I read on some site that printing a character array give you an address, but that doesn't look like an address to me... I haven't found a lot online about it.
So, what did I just print?
and bonus questions: How do you correctly print a character array in java?解决方案However I'm wondering if I just got garbage?
No, you got the result of
Object.toString()
, which isn't overridden in arrays:The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:
getClass().getName() + '@' + Integer.toHexString(hashCode())
So it's not garbage, in that it has a meaning... but it's not a particularly useful value, either.
And your bonus question...
How do you correctly print a character array in java?
Call
Arrays.toString(char[])
to convert it to a string... or justSystem.out.println(hello);
which will call
println(char[])
instead, which converts it into a string. Note thatArrays.toString
will build a string which is obviously an array of characters, whereasSystem.out.println(hello)
is broadly equivalent toSystem.out.println(new String(hello))
这篇关于什么是一个打印的字符数组的输出是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!