Pergunta

I thought the toString method will make a char array to a String, but I was wrong.

    char[] k=new char[2];
    k[0]='k';
    k[1]='k';
    System.out.println(k.toString());

This code will output: [C@112f614.

What exactly happened in this code k.toString()?

Should I never call toString method in a char array?

Thanks!

Happy New Year!

Foi útil?

Solução

You want to use Arrays.toString(char[]{'a','b'});


You can use

char data[] = {'a', 'b', 'c'};
     String str = new String(data);

See the javadoc

public String(char[] value) Allocates a new String so that it represents the sequence of characters currently contained in the character array argument. The contents of the character array are copied; subsequent modification of the character array does not affect the newly created string. Parameters: value - The initial value of the string

http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

Calling toString on an array will call the toString method from Object. Which will return you the hashCode

public String toString() Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method. 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())
Returns: a string representation of the object.

Outras dicas

Because arrays are objects. So calling toString() result to call the toString() method herited from the object class which is :

public String toString() {
    return getClass().getName() + "@" + Integer.toHexString(hashCode());
}

In [C@112f614, [C means that it's an array of char.

If you want to print the content of your array, use Arrays.toString(char[] a)

What exactly happened in this code k.toString()? Should I never call toString method in a char array?

The toString method of a char array inherits the default toString behavior from Object, which is to simply print a unique identifier derived from the object's location in memory. So unless you want that unique identifier, there's not much point in calling k.toString(). (k being your char array)

You should likely never call toString on any sort of array. The Arrays class has many utility methods for creating string representations of arrays.

A partial answer to this question: "What exactly happened in this code k.toString()?"

toString() is inherited from java.lang.Object and not overridden. The implementation just print the canonical class name plus the @-letter plus the hashcode. That is all. Therefore calling this methode is not useful for applications and is just for debugging purposes in IDEs to at least enlighten which type of object is there.

String can take a char array as a constructor.

String s = new String(k);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top