0

Can you explain what is happening in this line of code? Specially what is args[0].tocharArray ?

char[] password = args[0].toCharArray();

7 Answers 7

7

char[] is your datatype. "char" is a single 16 bit character, and char[] is a character array.

args[0] is the first argument that's passed to the program.

.toCharArray(); converts that argument to a character array.

This line of code is basically taking an argument, turning it into a character array, and storing it in "password" which is a character array.

Sign up to request clarification or add additional context in comments.

Comments

4

It's converting the first argument of a Java program—passed as a String[] to the main method—to a character array.

Most password-oriented APIs use char[] so that after calling the method, the caller can "zero-ize" the array, effectively erasing the password from memory. Since Java String instances are immutable, they can't be zero-ized. However, in practice, it's hard to get user-input without using a String. All web frameworks will convert passwords submitted in a web request to a String. Swing password widgets and Java 6's Console class will input char[], however.

Comments

3

args[0] is presumably a String array. Thus it is a call to the method String.toCharArray() which converts a String to an array of chars.

EDIT: Corrected my answer after comment.

Comments

2

It converts the first item of the args array (presumably, the first command line argument passed to the main method, which is of string type) to an equivalent array of chars (an array containing all the chars that build up the string).

Comments

1

args is an array.

The type of the array contains a function called toCharArray which returns an array of characters. NOTE: args is most likely an array of strings

So it takes the string in args[0] and creates an array of characters which represents that string.

Comments

0

args[0] - representing a string toCharArray() - convert this string to char array

Comments

0

I thought this, toCharArray(), might help.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.