Why can't I assign a 'long' a value of 4 billion?

前端 未结 6 1455
情话喂你
情话喂你 2020-12-09 15:00

I\'m trying to declare a long value in Java, which unfortunately does not work.

This is my code. It results in the following error message: \"The literal 4294967296

6条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-09 15:39

    The answer to your question "why" is because of 4294967296 is not a long. By default java look on any number as on int or double type (depending on if it has or hasn't dot). And only then convert this number to specified type (long in your case). So the error that you see means that your number is bigger then max value fot int. Adding literal attribute at the end let compiller know which type to use (b - bytes, s - short, l - long, f - float)

提交回复
热议问题