for(int i=0; i < 26; i++) {
alphabet[i] = (char)('a' + i);
hello, please explain what does the second line mean. Is it type casting from char to int?
for(int i=0; i < 26; i++) {
alphabet[i] = (char)('a' + i);
hello, please explain what does the second line mean. Is it type casting from char to int?
When you perform arithmetic operations in data types shorter than int
for example, shorts, bytes and chars, the end result of the operation is returned as an int
.
Thus in your case 'a' +i
is an operation on char and int. So the result is an int
.
So if you want a character back from this operation it is mandatory for you to perform an explicit cast.
So the operation here is a type cast from int
to char
.
It is a cast from int
to char
. Addition of int
and char
('a' + i
) make the expression of wider type.
Thanks I came to know it is typecasting syntax it has to be written in that manner.