0

In the following code snippet why is char[] is used, when we can use String?

public void passgen(){ int length = 10; System.out.println("Your Password is...."); char[] pass = generatePswd(length); a = new String(pass); System.out.println(a); } char[] generatePswd(int len)

4th Apr 2017, 5:04 AM
Sujay S Bhat
Sujay S Bhat - avatar
2 Réponses
+ 5
I think it's because it's a password generator, it is design to generate the password character by character, therefore it used char array instead of String, and later convert the char array to String to display the result password to the user
4th Apr 2017, 6:13 AM
Heng Jun Xi
Heng Jun Xi - avatar
+ 4
Char means a single character,where as a string can store multiple characters.And you are using char array. A string :- Hello char array :- H,e,l,l,o This is done to treat each character of string individually.
4th Apr 2017, 6:01 AM
Meharban Singh
Meharban Singh - avatar