Can someone explain how typecasting works in the line int y = (int) x;
Thank You
public class typecast
{
public static void main(String [] args)
{
double x=10.5;
int y=(int) x;
System.out.println("x="+x);
System.out.println("y="+y);
}
}
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.
The type cast performs a narrowing type conversion. The exact conversion depends on the double value, as follows:
intvalues, it is rounded towards zero.Integer.MIN_VALUEorInteger.MAX_VALUE, depending on the sign.Reference: JLS 5.1.3
Note: “round towards zero” is defined as follows: