Why its printing X88
public static void main(String [] args)
{
char x = 'X';
int i = 0;
System.out.print(true ? x : 0);
System.out.print(false ? i : x);
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In the first
printstatement, the type of the conditional expression is char (i.e. ‘X’) because this part of section 15.25 of the JLS (which is about the type of a conditional expression) applies:So the first statement prints “X”.
In the second
printstatement, that part doesn’t apply becauseiisn’t a constant expression. Instead, this section applies:Binary numeric promotion converts the char ‘X’ to an int (88) and the second statement prints “88” – hence the overall result of “X88”.