Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert a string to char[] without copying the object?

I have a string and I need to traverse it as a char array. Of course the normal method is to use toCharArray()

String str = "Hello";
char[] charArr = str.toCharArray();

Now, the source code for toCharArray() is as follows.

public char[] toCharArray() {
    // Cannot use Arrays.copyOf because of class initialization order issues
    char result[] = new char[value.length];
    System.arraycopy(value, 0, result, 0, value.length);
    return result;
}

So Java is creating a new object in memory of type char[] and copying the string object to the new object.

My question is whether or not it is possible to use a string as a char[] without copying the array. My intention is to save on memory space. If this is not possible, is there a reason so?

Thanks in advance!

like image 473
Jordan Epstein Avatar asked May 25 '16 16:05

Jordan Epstein


People also ask

How to convert a string to a char in Java?

This tutorial discusses methods to convert a string to a char in Java. The simplest way to convert a character from a String to a char is using the charAt (index) method. This method takes an integer as input and returns the character on the given index in the String as a char.

How to copy the contents of a string to char array?

A way to do this is to copy the contents of the string to char array. This can be done with the help of c_str () and strcpy () function. The c_str () function is used to return a pointer to an array that contains a null terminated sequence of character representing the current value of the string.

How do you copy a character from one string to another?

Step 1: Get the string. Step 2: Create a character array of the same length as of string. Step 3: Traverse over the string to copy character at the i’th index of string to i’th index in the array.

How do I get a char * pointer from a string?

You can use &mystring [0] to get a char * pointer, but there are a couple of gotcha's: you won't necessarily get a zero terminated string, and you won't be able to change the string's size. You especially have to be careful not to add characters past the end of the string or you'll get a buffer overrun (and probable crash).


3 Answers

No, it isn't. Not without reflection, which you should avoid. Messing with the underlying char[] in a string via reflection is a recipe for subtle bugs. You can access individual characters in a string using charAt, but if you really need a char[] you should just call toCharArray.

If this is not possible, is there a reason so?

First reason: encapsulation. The array is a private implementation detail.

Second reason: immutability. Strings are immutable, but arrays never are. So you could modify the underlying char array, and the string would be mutated, much to the surprise of any developer relying on the normal immutability.

like image 67
Jon Skeet Avatar answered Sep 30 '22 01:09

Jon Skeet


Since you are expecting the array back, there is no way to get an array without creating a new array.

This is the cleanest way. Even though you create your own function and do some trick, you'll end up in creating new array.

Go with your current codes.

like image 20
Suresh Atta Avatar answered Sep 30 '22 01:09

Suresh Atta


Here is how you can traverse a String somehow as a char array without using toCharArray() :

for (int i = 0; i < str.length(); i++) {
    char c = str.charAt(i);
}
like image 29
Nicolas Filotto Avatar answered Sep 30 '22 01:09

Nicolas Filotto