Question

I was trying to implement a string reversal algorithm by using two pointer variables at each end of the string where i points to the beginning and j points to the end. The elements at these places are swapped and then i and j are incremented and decremented respectively until i is less than j and i not equal to j. I am storing the string in a character array while doing this since string is immutable in java. However, when I am trying to convert this character array back to a string using the toString() method it is displaying random values. I know the code is correct since if I output the character array it displays the right values.

public class switcher {

int i=0,j;
char temp;

public void reverse(String s){

    char [] ar = s.toCharArray();

    j=ar.length-1;

    while(i!=j&&i<j){
        temp = ar[i];
        ar[i]=ar[j];
        ar[j]=temp;
        i++;
        j--;
    }
    String b=ar.toString();
    System.out.println(b);

    System.out.println(ar);
}

The output is as follows for the two print statements:

amistad [C@22a79c31 datsima

As you can see the string output is not correct. However, the array output is perfect. Please tell me what I am doing wrong.

Was it helpful?

Solution

If you want to print String which should be based on array of character then you should wrap this array with new String object. So instead of

String b = ar.toString();

use

String b = new String(ar);

You need to know that arrays inherit toString() method from Object so its code returns

getClass().getName() + "@" + Integer.toHexString(hashCode());

which means you will see [C as result of getClass().getName() which represents one dimensional array of characters, @ token and hexadecimal form of arrays hexcode 22a79c31.


In case you would want to print content of array with different type of data than char you wouldn't be able to wrap it in String. Instead you will have to iterate over each elements and print them. To avoid writing your own method for this Java gives you java.util.Arrays class with toString(yourArray) method which will iterate over each elements of array and generate String in form

[element0, element1, element2, ... , elementN-1]

OTHER TIPS

ar.toString() will not return the string representation of the character array. It will return the index of the array in memory. If you want b to print out properly, pass ar to it as a constructor, like so: String b = new String(ar);

As others have said, that's correct.

Most Java objects do not do a value conversion in their tostring. Instead they print the tokenized name of the type as known internally to the VM.

To convert a character array to a string for printing you want to do the opposite of what you did to convert it from a string.

The simplest way is simply to make use of the string constructor that takes a character array

So rather then saying:

System.out.println(ar);

You could say

System.out.println(new String(ar));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top