I want to understand the working of if block from the below code. For example: if my string is rock, then the ASCII would be char_set[r] = 114
char_set[o] = 111,char_set[c] = 99,char_set[k] = 107. So for the first time when char_set[r] = 114 will come to if block then what is going to be checked? Since char_set is boolean type so it could be either true or false but according to my understanding initially char_set[r] = 114 is not set to either true or false. So how can we check if it is either true or false inside the if block. I have looked at the previous related questions but my question was different and couldn't find anything. This question is taken from the book Cracking the coding interview.
public boolean isuniqueChars2(String str) {
if (str.length() > 128)
return false;
boolean[] char_set = new boolean[256];
for (int i = 1; i < str.length(); i++)
{
int val = str.charAt(i);
if (char_set[val]) {
return false;
}
char_set[val] = true;
}
return true;
}
char_set[r] = 114whenchar_setis aboolean[]?charAtcan return 0 to 65535, which is a Unicode/UTF-16 code unit.Please take some time later to understand that.