I want to convert a String to a char [] to perform some bitwise operations on it.
My first approach was to use the java standard functions:
String foo = "123456789"
char [] bar = foo.toCharArray();
System.out.println(bar.toString); //output: [C@4263e7f8
So it seems that the data gets altered using this function. If I do it all manually:
  String foo = "123456789"
  char [] bar = new char[foo.length()];
  for (int i=0; i< foo.length(); i++) {
     bar[i]=foo.charAt(i);
  }
 StringBuilder sb = new StringBuilder();
 for (int i=0; i< bar.length; i++) {
     sb.append(bar[i]);
 }
 System.out.println(sb.toString); //output: 123456789
My question is why I get different data when I use the standard functions. I was expecting that I always get the same data after .toCharArray followed by .toString. I'm not sure how a Java String is represented bitwise. May this be a reason?
                        
Whenever you try to print an array by using
toString(), it will printhashcodeinstead of printing the contents. Because:Therefore when you are trying to call
toStringmethod, it will directly call Object's toString method which is:But when you are using
StringBuilderclasstoString(), it will return aStringobject :