Can you explain what is happening in this line of code? Specially what is args[0].tocharArray ?
char[] password = args[0].toCharArray();
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.
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.
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.
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).
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.
args[0] - representing a string
toCharArray() - convert this string to char array
I thought this, toCharArray(), might help.
Related
Declaration of a character:
char ch = '';
When I do this i am getting the error 'empty character literal'.
Declaration of a String:
String str = "";
I see no error in doing that to a String.
The question is, why doesn't a similar error show up for the declaration of a String, or why declaration of empty character generating such error where empty string is getting passed
String is a set of chars and String str=""; contains no chars(read: empty string)
but if you want to have Char variable it must have some value. '' means no value.
String is a class in Java with its own syntax and methods. It accepts strings in double quotes. And a string is actually an Array of characters and is hence acceptable to be posted empty.
Char on the other hand is a data type and cannot be left undetermined. It needs to specified NULL.
I would recommend you to read through the Java tutorial documentation hosted on Oracle's website whenever you are in doubt about anything related to Java.
Basically char is a thing you put in a box, and a string is a box to hold all those things. You can have an empty box but not a non-existant thing.
A string is an array of characters. By passing it nothing, i.e. making it equal to "" you basically make an empty array which is fine. But char is a primitive type hence it cannot be "empty". The closest you can get is setting it equal to '\0' which is the null character.
Here char represents the 16-bit integer value of the character in quotes. Refer this table for the values.
There is no representation number for "empty/no character".
In case of String refer their source code. You can see that empty string is represented internally by 0 size char array. So String internally does not have magical representation of empty/no character. For "" String class does not allocate any space per se
I have some question that I wonder about. I know that string are immutable in Java and therefore a new string object is created rather than changed when for example assigning to a existing string object.
Now to my question. Let's suppose that I have the following piece of code:
String a = "Hello World";
String b = "Hello World";
String res = a.substring(0,4) + b.substring(6,10);
How many string objects will be created by the code at line 3 ? Will each call to substring create a new string object ? Will my code above generate 3 new string objects ?
Thanks in advance
Strings in Java are immutable. Basically this means that, once you create a string object, you won't be able to modify/change the content of a string. As a result, if you perform any manipulation on a string object which "appears to" change the content of the string, Java creates a new string object, and performs the manipulation on the newly created one.
Based on this, your code above appears to create five string objects - two are created by the declaration, two are created by calls to substring, and the last one is created after you concatenate the two pieces.
Immutability however leads to another interesting consequence. JVM internally maintains something like a string pool for creating string literals. For saving up memory, JVM will try to use string objects from this pool. Whenever you create a new string literal, JVM will loop into the pool to see if any existing strings can be used. If there is, JVM will simply use it and return it.
So, technically, before Java 7, JVM will create only one string object for your whole code. Even your substring calls won't create new string objects in the pool, it will use the existing "Hello World" one, but in this case it will only use characters from position 0 to 3 for your first call to substring, for example. Starting from Java 7, substring will not share the characters, but will create a new one. So, total object count will be 4 - the last one will be created with the concatenation of the two substrings.
Edit
To answer your question in the comment, take a look at Java Language Specification -
In the Java programming language, unlike C, an array of char is not a
String, and neither a String nor an array of char is terminated by
'\u0000' (the NUL character).
A String object is immutable, that is, its contents never change,
while an array of char has mutable elements.
The method toCharArray in class String returns an array of characters
containing the same character sequence as a String. The class
StringBuffer implements useful methods on mutable arrays of
characters.
So, no, char arrays are not immutable in Java, they are mutable.
Literal a is created newly and kept in the pool.Literal b refer the a, it will not create new one instead.
The line 3 will create 3 new String since substring creates a new string and concatenate creates new Strings every time.
String substring(int beginIndex,int endIndex)
Returns a new string that is a substring of this string. The substring
begins at the specified beginIndex and extends to the character at
index endIndex - 1. Thus the length of the substring is
endIndex-beginIndex.
In my program I'm trying to compare my char array asterixA[] to a String word in an if condition like:
if (word.equals(asterixA))
but it's giving me an error. Is there any other way I can compare them?
you have to convert the character array into String or String to char array and then do the comparision.
if (word.equals(new String(asterixA)))
or
if(Arrays.equals(word.toCharArray(), asterixA))
BTW. if is a conditional statement not a loop
You seem to be taking the "A String is an array of chars" line too literal. String's equals method states that
Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.
It all depends of the circumstances, but generally you compare two objects of the same type or two objects belonging to the same hierarchy (sharing a common superclass).
In this case a String is not a char[], but Java provides mechanisms to go from one to the other, either by doing a String -> char[] transformation with String#toCharArray() or a char[] -> String transformation by passing the char[] as a parameter to String's constructor.
This way you can compare both objects after either turning your String into a char[] or vice-versa.
You can compare the arrays:
if (Arrays.equals(asterixA, word.toCharArray()) {}
do as follows: if (word.equals(new String(asterixA))) { ... }
I am making a hangman game. I have two char arrays and I need to check if they are equal.
One of them has letters and underscores: char checkLetter[]
The other one has only letters: char currentWord[]
Eventually, after the user has guessed all the words in the checkLetter[] array it will also consist of only letters. But I need to keep continually checking (in a boolean method) if the array into which they guess and their letters get stored, is the exact same as the word they are trying to guess.
If I could convert both of the arrays into strings then I could check them for equality. I am not experienced, and I don't know how to do this. Any help help would be appreciated!
You don't need to convert them to strings at all. Use Arrays.equals().
you can convert an char array into string using String's overloaded constructor which takes char[] array as argument.
char[] carr ;
String s = new String(carr);
You may use new String(char[] value)` to create String from char array.
Use the String-constructor:
String str = new String(yourCharArray);
However, that's useless; use Arrays.equals(arr1, arr2) instead.
You really don't need to convert the array, but if you really want to then
try using String word = currentWord.toString() to convert the char array.
I have a big string having at most 100000 character. Instead of using string.charAt[index] to read a character from the string, I converted that string into char array using string.toCharArray() method and now i am working with charArray[index]. which takes less time than string.charAt[index] method. However i want to know that, is there any other way which is faster than string.toCharArray(); method?
I do not think there is a faster way. But please correct me!
A String instance is backed by a char array. charAt() does some index checks which may be the cause for it being slower than working with the array returned by toCharArray(). toCharArray() simply does a System.arraycopy() of the backing array.