简体   繁体   English

在java中为long数据类型赋值 - 默认为int

[英]Assigning a value to long data type in java - defaulting to int

long val = 5000000000;

The error during this assignment is:此作业期间的错误是:

The literal 5000000000 of type int is out of range int 类型的文字 5000000000 超出范围

Why does the compiler by default assume the literal to be type int when it is declared with type long ?为什么编译器在用long类型声明时默认假定字面量是int类型?

You can use:您可以使用:

long val = 5000000000L;

Check it here 在这里检查

There is aspecific suffixes for long ie L .长有特定的后缀,即L If there is no suffix, then 5000000000 assumed to be an int type.如果没有后缀,则 5000000000 假定为int类型。 And 5000000000 is out of int range, causing the erro.并且 5000000000 超出int范围,导致错误。 So you need to add L at the end of 5000000000 for it to be treated as a long value.因此您需要在 5000000000 的末尾添加L才能将其视为long值。 Change your declaration from将您的声明从

long val = 5000000000;

to

long val = 5000000000L;

Add the letter L at the end of your number as shown below在您的号码末尾添加字母 L,如下所示

long val = 5000000000L;长 val = 5000000000L;

long: The long data type is a 64-bit signed two's complement integer. long:long 数据类型是一个 64 位有符号二进制补码整数。 It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive).它的最小值为 -9,223,372,036,854,775,808,最大值为 9,223,372,036,854,775,807(含)。

You should append 'l' or 'L' to the value explicitly used to initialize the variable;您应该将 'l' 或 'L' 附加到显式用于初始化变量的值; even as small as 0.甚至小到 0。

 long val = 0L;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM