-3

I coded a sample Java program:

String str="JAVA is awesome";
char[] ch=str.toCharArray();
System.out.println("The value of ch is : " + ch);

It is showing an unexpected result with some random ascii value... What is the problem?

ConcurrentHashMap
  • 4,998
  • 7
  • 44
  • 53
Rush W.
  • 1,321
  • 2
  • 11
  • 19
  • It's probably printing the address of the `ch` object. What do you expect it to print? – Alex MDC Sep 04 '14 at 15:06
  • What you're seeing will look something like `[C@48a2e6`. This is a composition of the class of `ch` (which is `[C`, or character array), followed by the @ symbol and the hexadecimal string representation of the array's hash code. – JonK Sep 04 '14 at 15:08
  • FYI: In this example alone, you don't need to instantiate ch as a new char[n], and therefore you don't need n either. char[] ch = str.toCharArray(); is fine :) – ne1410s Sep 04 '14 at 15:09
  • possible duplicate of [Java arrays printing out weird numbers, and text](http://stackoverflow.com/questions/4479683/java-arrays-printing-out-weird-numbers-and-text) – JonK Sep 04 '14 at 15:09

3 Answers3

1

You really don't need this step char ch[]=new char[n];

As javadoc says String#toCharArray()- returns a newly allocated character array whose length is the length of this string and whose contents are initialized to contain the character sequence represented by this string.

And in this line System.out.println("The value of ch is : " + ch);

Just 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.

So this few lines below should do what you want to achieve

String str="JAVA is awesome";
char[] ch = str.toCharArray();
System.out.println(java.util.Arrays.toString(ch));

Where Arrays.toString(char[] ch) - Returns a string representation of the contents of the specified array. The string representation consists of a list of the array's elements, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (a comma followed by a space).

SparkOn
  • 8,806
  • 4
  • 29
  • 34
0

This line

System.out.println("The value of ch is : " + ch);

Will print out

The value of ch is : //And the hash code of your array, which is just an object in Java

If you want meaningful data, use the Arrays.toString() overloaded method...

System.out.println("The value of ch is : " + Arrays.toString(ch));

Take a look at the Javadocs for the Array.toString() method to see when it may be useful in your case.

Kon
  • 10,702
  • 6
  • 41
  • 58
  • yeah.. but when i use Arrays.string error comes : Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method toString() in the type String is not applicable for the arguments (char[]) at spidy.test.main(test.java:15) – Rush W. Sep 04 '14 at 15:13
  • I hope you mean `Arrays.toString(ch)`.. And what error comes? Compilation or runtime? Give some information... – Kon Sep 04 '14 at 15:14
0

This code:

System.out.println("The value of ch is : " + ch);

is equivalent to this

System.out.println("The value of ch is : " + ch.toString());

As ch is an array object it inherits toString() method from Object.toString(). This 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.

Try to use one of these for the output you need:

System.out.println("The value of ch is : " + new String(ch)); //prints ch array as String
System.out.println("The value of ch is : " + String.valueOf(ch)); //prints ch array as String
System.out.println("The value of ch is : " + Arrays.toString(ch)); //prints every letter separately
Michał Schielmann
  • 1,372
  • 8
  • 17