0 votes
in JAVA by
Why is the character array preferred over string for storing confidential information?

1 Answer

0 votes
by

In Java, a string is basically immutable i.e. it cannot be modified. After its declaration, it continues to stay in the string pool as long as it is not removed in the form of garbage. In other words, a string resides in the heap section of the memory for an unregulated and unspecified time interval after string value processing is executed.

As a result, vital information can be stolen for pursuing harmful activities by hackers if a memory dump is illegally accessed by them. Such risks can be eliminated by using mutable objects or structures like character arrays for storing any variable. After the work of the character array variable is done, the variable can be configured to blank at the same instant. Consequently, it helps in saving heap memory and also gives no chance to the hackers to extract vital data.

Related questions

0 votes
asked May 29, 2020 in JAVA by anonymous
0 votes
asked Oct 21, 2020 in JAVA by rahuljain1
...