Hi!! I just wanted to know if such questions can be part of some code in the exam?I haven't encountered any so far in the books but was just curious when I was trying out my own practice code.But if it occurs in the exam,how do we represent a char value in binary form to do the (|)or any bitwise operations.The result of the following expn is 107. char x='k'; int y=35; int z=x|y; System.out.println("x|y val="+z); If there is a probability for such a question in the exam please help me regarding this,otherwise don't waste your time.Thanks.
Hi, The example above performs the following 107|35 (Here 107 is the ascii of character 'k') Even though you dont need to know the ascii values , I think you should know that in any arithmetic expression involving byte/short/char, they are always widened to an int. For example: byte b = 10; short s = 12; char c = 'r'; b + s + c = ? int i = b + s + c; Another example: byte b = 3, c = 4; byte d = b + c; //error since b + c will be widened to an int So the above error can be fixed in 2 ways 1. byte d = (byte) (b + c); 2. int d = b + c; I hope this helps. [ November 21, 2003: Message edited by: Cathy Song ]
Joined: Aug 13, 2003
Thanks Kalai for letting me know that such a question doesn't occur in the exam and thanks Cathy for your explanation eventhough I know about the automatic promotion rules.What I didn't understand is the significance of the promotion rules in my question about char representation.Thanks anyways.