Originally posted by Thomas Paul: You were taught incorrectly. As long as there are no variables on the right hand side there isn't a problem.
Well, in the case that the value to be assigned fits within the assigned variable, there isn't a problem. Notice that this works:
while this causes an error:
The reason for this is that the compiler can determine if the value being assigned can fit within the variable that it is being assigned to. This can be done because you're assigning a constant value. If you were to use a variable, the compiler wouldn't know if it fit in a variable or not and, hence, would always throw an error. This works:
while this doesn't:
Even though we can see that 3 will be assigned to the char and we know that 3 fits within the range, the compiler isn't sure what the value of i will be when the assignment occurs. Therefore, an error is issued. I hope that helps, Corey
In Corey's last example, if i is made final then it will work since the compiler knows that the value of i can never change.
Joined: Dec 20, 2001
Originally posted by Thomas Paul: In Corey's last example, if i is made final then it will work since the compiler knows that the value of i can never change.
In which case, the compiler treats the variable as if it was a literal. (Hence, it works - good point, Thomas.)
Karin Paola Illuminate
Joined: Oct 18, 2002
I know what the range is of a char: \u0000 to \uffff, or 0 to 65535 But, I don't know how to determine if 'a' * 2 fits in the range of a char and 'a' * 2000 does not fit in the range. It looks to me like "acacadabra". Should I know the char value of 'a' * 2 for the exam, for example? Thanks. My Study Notes [ March 19, 2003: Message edited by: Karin Paola Illuminate ]
I not only use all the brains that I have, but all that I can borrow. [Laurence J. Peter]
Joined: Dec 20, 2001
You don't need to know the values of individual characters for the exam. Just for reference, however, 'a' is equivalent to 97. Therefore, 'a' * 2 = 194 and 'a' * 2000 = 194000, which is out of the range of a char which only goes up to 2^16 (65535). If you have to know the value of a particular character for a question on the exam, you'll be given that information. Corey
Joined: Mar 06, 2003
Originally posted by mohamed hamdy: i was tought that this line: char c='c'-'a'; will give compile error because of the promotion of character literals (or any integral literals narrower tha the type int) to int. can someone comment me?
You can initialize char and byte primitive with the integer const expression if it fits the size of the relevant type for instance byte b = 10; // ^byte ^int char c = 1; // ^char ^int