I declared an integer int i = 4945932; and its square is coming out to be some negative random number. How is this possible? What am i doing wrong? Help please.Thanks in advance.
Share
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.
Integer overflow. A Java int cannot be larger than 2,147,483,647; if you try to store a larger number, it overflows.
If you instead use a
long, that can store much larger values, including the one you’re trying to store. If you need even larger values,java.math.BigIntegercan store arbitrary-precision integers; the only limit is your computer’s memory.