---
title: "Why Char array is preferred over String for storing password?"  
description: "Why Char array is preferred over String for storing password?"  
author: "Ronan Jone"  
published: 2018-02-27  
canonical: https://answers.mindstick.com/qa/35074/why-char-array-is-preferred-over-string-for-storing-password  
category: "programming"  
reading_time: 1 minute  

---

# Why Char array is preferred over String for storing password?

## Answers

### Answer by Arti Mishra

**“char[] [array](https://www.mindstick.com/articles/14/array) over a [string](https://www.mindstick.com/articles/1717/string-string-builder-and-string-buffer-in-java) in java”** **Use char [] array** for storing any type of [password](https://www.mindstick.com/blog/243/how-to-create-password-protected-report) because **String objects are [immutable](https://www.mindstick.com/interview/2224/what-do-you-mean-by-string-objects-are-immutable) (means can’t be modifiable ) objects** in java it **[stored](https://www.mindstick.com/interview/644/where-is-the-output-of-explain-stored) the password as a plain text format** as well as it saves the stringin [memory](https://www.mindstick.com/blog/300050/what-causes-sudden-memory-loss), for a **long duration of time**. And there is **no any possibility to modify the [content](https://www.mindstick.com/articles/12427/4-types-of-blog-phrases-that-keep-content-readers-engaged) of string object** because if **any changed occur then the new string will we produced.** **\**\
Benefit for storing the password in char[] array, the **data will be cleared (wiped) explicitly after the whole process is completed.** And if any change occurs then the **[character](https://www.mindstick.com/blog/250/html-character-entities) will be overwritten** in the memory.\
**Here we demonstrate the simple example, for storing the password char[] array over the string objects-** **\**[public](https://www.mindstick.com/interview/757/why-main-in-java-is-declared-as-public-static-void-main) class CharOverString { public [static](https://www.mindstick.com/articles/12098/the-static-keyword-the-static-methods) void main(String[] args) { String strPwd = "Mindstick"; char[] charPwd = new char[] {'M','i','n','d','s','t','i','c','k'}; System.out.println("String Password: " + strPwd ); System.out.println("Character Password: " + charPwd ); } }\
**Output :** String Password: Mindstick Character Password: [C@2a139a55\
\
**"Thanks!!! for Reading"**


---

Original Source: https://answers.mindstick.com/qa/35074/why-char-array-is-preferred-over-string-for-storing-password

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
