Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8632029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:17:48+00:00 2026-06-12T09:17:48+00:00

Possible Duplicate: Why is char[] preferred over string for passwords? When I was preparing

  • 0

Possible Duplicate:
Why is char[] preferred over string for passwords?

When I was preparing for OCPJP I came accross the topic – “Reading User input from console”.

There was an example where it read username in String reference, whereas password in a char[] array, but I couldn’t understand why it used char array.. Here is the code : –

Console console = System.console();

String username = console.readLine("User Name? ");
char[] password = console.readPassword("Password? "); 

This raised a doubt in my mind.. Why didn’t we used String reference to store password. Since Strings are immutable, so it must be more secure to read password in a String, as its content could not be changed for that matter.

So, what’s the whole point in reading password in char[] array..

Can anyone shed some light in this matter?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T09:17:49+00:00Added an answer on June 12, 2026 at 9:17 am

    As you said, strings are immutable, meaning that once you’ve created the string, if another process can dump memory, there’s no way (ok, may with reflection) you can get rid of the data before GC kicks in.

    With an array, you can explicitly wipe the data after you’re done with it: you can overwrite the array with anything you like, and the password won’t be present anywhere in the system, even before garbage collection.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Why is char[] preferred over string for passwords? I read somewhere that
Possible Duplicate: Last but one Char in vb.net String How do I find the
Possible Duplicate: What is the difference between char a[] = “string” and char *p
Possible Duplicate: C++ char* vs std::string Is there any advantage to using char*'s instead
Possible Duplicate: how to copy char * into a string and vice-versa I have
Possible Duplicate: C++ convert int and string to char* Hello, i am making a
Possible Duplicate: Modifying C string constants? Pointer to const char vs char array vs
Possible Duplicate: C++ deprecated conversion from string constant to 'char*' I want to pass
Possible Duplicate: Convert std::string to const char* or char* is there any to get
Possible Duplicate: Convert std::string to const char* or char* Simple question but I'm new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.