【发布时间】:2012-09-23 08:09:59
【问题描述】:
当我为OCPJP 做准备时,我遇到了这个话题——“从控制台读取用户输入”。
有一个例子,它在String 引用中读取username,而在char[] 数组中读取password,但我不明白为什么它使用char 数组。这是代码:-
Console console = System.console();
String username = console.readLine("User Name? ");
char[] password = console.readPassword("Password? ");
这让我产生了疑问。为什么我们不使用 String 引用来存储密码。由于 Strings 是不可变的, 因此读取字符串中的密码必须更安全,因为它的内容无法更改。
那么,在char[] 数组中读取password 的意义何在..
谁能解释一下这个问题?
【问题讨论】: