hassasin
hassasin

Reputation: 141

string to char array, showing silly characters

It is a really simple question but I need an another eye to look at my code:

String strtr = "iNo:";
char[] queryNo = strtr.toCharArray();
System.out.println(queryNo + " =this is no");

and the output is:

[C@177b4d3 =this is no

What are these characters, do you have any idea?

Upvotes: 1

Views: 710

Answers (4)

Jayant Varshney
Jayant Varshney

Reputation: 1825

you are printing the object

queryno, as queryno is a character array of on dimension and java is an object oriented language which holds every thing in the form of classes it gives the class name [C to your array where [ denotes total dimension and C denotes character type of array, Rest is the hashcode of the object.

Upvotes: 1

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 135992

Arrays do not override toString(), it is inherited from Object.toString as

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

Upvotes: 1

Narendra Pathai
Narendra Pathai

Reputation: 41935

You are trying to print the array and that is the reason you get gibberish. Try using Arrays.toString(queryNo) and you will see what you expected.

Upvotes: 0

jlordo
jlordo

Reputation: 37813

That's how toString() is implemented for arrays.

The [C denotes that is a char array, 177b4d3 is its hashcode.

You may want to look at

System.out.println(Arrays.toString(queryNo) + " =this is no");

if you want to see your original String again, you need this:

System.out.println((new String(queryNo)) + " =this is no");

Upvotes: 7

Related Questions