如果char数组是Java中的Object,为什么打印它不显示其哈希码?

时间:2015-07-04 08:11:28

标签: java arrays char

打印char数组不会显示哈希码:

class IntChararrayTest{
    public static void main(String[] args){
        int intArray[] = {0,1,2};
        char charArray[] = {'a','b','c'};
        System.out.println(intArray);
        System.out.println(charArray);
    }
}

输出:

[I@19e0bfd
abc

为什么整数数组打印为哈希码而不是char数组?

6 个答案:

答案 0 :(得分:23)

首先,char数组是Java中的Object,就像任何其他类型的数组一样。它的印刷方式不同。

PrintStream(这是System.out实例的类型)对于字符数组有println的特殊版本 - public void println(char x[]) - 所以它不必调用该数组的toString。它最终调用public void write(char cbuf[], int off, int len),它将数组的字符写入输出流。

这就是为char[]调用println的行为与为其他类型的数组调用println的行为不同。对于其他数组类型,选择public void println(Object x)重载,调用String.valueOf(x),调用x.toString(),返回类似[I@19e0bfd的int数组。

答案 1 :(得分:4)

int数组是一个整数数组,其中可打印字符的char数组。打印机具有打印字符数组的能力,因为这是打印字符串的方式。因此,printwriter将像字符串一样打印它们,而不调用toString()方法将其转换为字符串。将int数组转换为字符串会返回一个哈希码,解释为什么会得到该输出。

以此为例:

array

如果要使用您使用的方法打印这两个序列,它将打印int数组的哈希码,后跟'123'。

答案 2 :(得分:3)

System.out是PrintStream,它有一个char [] arg

的特殊方法
public void println(char x[]) {
    synchronized (this) {
        print(x);
        newLine();
    }
}

int []通过此方法打印

public void println(Object x) {
    String s = String.valueOf(x);
    synchronized (this) {
        print(s);
        newLine();
    }
}

答案 3 :(得分:1)

当然数组是Java中的对象。无论是什么类型的阵列。它始终是Java中的一个对象。

但就您的问题而言,println(char[] array)类中有一个方法java.io.PrintStream可以打印出来自char array的所有字符。这就是原因,当您将char[]作为参数传递给println()时,它不会调用toString()类的java.util.Array方法,而是循环访问数组并打印每个字符。

引用:

http://docs.oracle.com/javase/7/docs/api/java/io/PrintStream.html#println(char[])

答案 4 :(得分:1)

EL Finest]: weaver: 2015-07-16 20:52:31.163--ServerSession(1547425104)--Thread(Thread[application-akka.actor.default-dispatcher-2,5,main])--java.lang.ArrayIndexOutOfBoundsException: 25970 at org.eclipse.persistence.internal.libraries.asm.ClassReader.readClass(Unknown Source) at org.eclipse.persistence.internal.libraries.asm.ClassReader.getInterfaces(Unknown Source) at org.eclipse.persistence.internal.jpa.weaving.ComputeClassWriter.typeImplements(ComputeClassWriter.java:143) at org.eclipse.persistence.internal.jpa.weaving.ComputeClassWriter.typeImplements(ComputeClassWriter.java:150) at org.eclipse.persistence.internal.jpa.weaving.ComputeClassWriter.getCommonSuperClass(ComputeClassWriter.java:62) at org.eclipse.persistence.internal.libraries.asm.ClassWriter.getMergedType(Unknown Source) at org.eclipse.persistence.internal.libraries.asm.Frame.merge(Unknown Source) at org.eclipse.persistence.internal.libraries.asm.Frame.merge(Unknown Source) at org.eclipse.persistence.internal.libraries.asm.MethodWriter.visitMaxs(Unknown Source) at org.eclipse.persistence.internal.libraries.asm.MethodAdapter.visitMaxs(Unknown Source) at org.eclipse.persistence.internal.jpa.weaving.MethodWeaver.visitMaxs(MethodWeaver.java:152) at org.eclipse.persistence.internal.libraries.asm.ClassReader.accept(Unknown Source) at org.eclipse.persistence.internal.libraries.asm.ClassReader.accept(Unknown Source) at org.eclipse.persistence.internal.jpa.weaving.PersistenceWeaver.transform(PersistenceWeaver.java:93) at org.eclipse.persistence.internal.jpa.deployment.JavaSECMPInitializer$1.transform(JavaSECMPInitializer.java:228) at sun.instrument.TransformerManager.transform(TransformerManager.java:188) at sun.instrument.InstrumentationImpl.transform(InstrumentationImpl.java:428) at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(ClassLoader.java:760) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) at java.net.URLClassLoader.defineClass(URLClassLoader.java:455) at java.net.URLClassLoader.access$100(URLClassLoader.java:73) at java.net.URLClassLoader$1.run(URLClassLoader.java:367) at java.net.URLClassLoader$1.run(URLClassLoader.java:361) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:360) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:340) at org.eclipse.persistence.internal.security.PrivilegedAccessHelper.getClassForName(PrivilegedAccessHelper.java:124) at org.eclipse.persistence.internal.jpa.metadata.listeners.EntityListenerMetadata.getClass(EntityListenerMetadata.java:224) at org.eclipse.persistence.internal.jpa.metadata.listeners.EntityClassListenerMetadata.process(EntityClassListenerMetadata.java:81) at org.eclipse.persistence.internal.jpa.metadata.accessors.classes.EntityAccessor.processListeners(EntityAccessor.java:1220) at org.eclipse.persistence.internal.jpa.metadata.MetadataProcessor.addEntityListeners(MetadataProcessor.java:138) at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:591) at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getAbstractSession(EntityManagerFactoryDelegate.java:204) at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.createEntityManagerImpl(EntityManagerFactoryDelegate.java:304) at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:336) at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:302) at play.db.jpa.DefaultJPAApi.em(DefaultJPAApi.java:71) at play.db.jpa.DefaultJPAApi.withTransaction(DefaultJPAApi.java:123) at play.db.jpa.JPA.withTransaction(JPA.java:159) at play.db.jpa.TransactionalAction.call(TransactionalAction.java:16) at play.core.j.JavaAction$$anonfun$7.apply(JavaAction.scala:94) at play.core.j.JavaAction$$anonfun$7.apply(JavaAction.scala:94) at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1$1(Future.scala:24) at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24) at play.core.j.HttpExecutionContext$$anon$2.run(HttpExecutionContext.scala:40) at play.api.libs.iteratee.Execution$trampoline$.execute(Execution.scala:70) at play.core.j.HttpExecutionContext.execute(HttpExecutionContext.scala:32) at scala.concurrent.impl.Future$.apply(Future.scala:31) at scala.concurrent.Future$.apply(Future.scala:485) at play.core.j.JavaAction.apply(JavaAction.scala:94) at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$4$$anonfun$apply$5.apply(Action.scala:105) at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$4$$anonfun$apply$5.apply(Action.scala:105) at play.utils.Threads$.withContextClassLoader(Threads.scala:21) at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$4.apply(Action.scala:104) at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$4.apply(Action.scala:103) at scala.Option.map(Option.scala:145) at play.api.mvc.Action$$anonfun$apply$1.apply(Action.scala:103) at play.api.mvc.Action$$anonfun$apply$1.apply(Action.scala:96) at play.api.libs.iteratee.Iteratee$$anonfun$mapM$1.apply(Iteratee.scala:524) at play.api.libs.iteratee.Iteratee$$anonfun$mapM$1.apply(Iteratee.scala:524) at play.api.libs.iteratee.Iteratee$$anonfun$flatMapM$1.apply(Iteratee.scala:560) at play.api.libs.iteratee.Iteratee$$anonfun$flatMapM$1.apply(Iteratee.scala:560) at play.api.libs.iteratee.Iteratee$$anonfun$flatMap$1$$anonfun$apply$13.apply(Iteratee.scala:536) at play.api.libs.iteratee.Iteratee$$anonfun$flatMap$1$$anonfun$apply$13.apply(Iteratee.scala:536) at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1$1(Future.scala:24) at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24) at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:40) at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:397) at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260) at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339) at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979) at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107) 实际上为您提供了System.out的对象。并且它的PrintStream有一些重载方法,这些方法针对不同类型的println(params)实现不同。

params如果是params,则会将char[]元素输出为char[]输出System.out.println(charArray);,但不会像{abc那样提供哈希码1}}如int[]输出System.out.println(intArray);

PS: - 所有[I@19e0bfd在Java中都被视为array

请参阅Official Doc

此外,对于打印Object,请始终使用array实用程序类。其方法Arrays应该优先用于打印Arrays.toString()个对象。

答案 5 :(得分:1)

其他答案正确解释了您对PrintStream.println单独调用的情况,.print也是如此。但是,您的标题可以涵盖其他打印方式。对于简单的调用,PrintWriter.println.print.write确实有char[]次重载。

但是,格式化没有特殊情况:

 System.out.format ("%s%n", charArray); // or printf
 myPrintWriter.format ("%s%n", charArray); // or printf

每个输出[C@hash,类似于int和其他类型数组的处理。

也不经常在印刷中使用连接:

 System.out.println ("the value of charArray is " + charArray);

输出the value of charArray is [C@hash