問題描述
我目前正在使用 Java 中的數(shù)據(jù)類型,如果我理解正確,類型 long
接受介于 -9,223,372,036,854,775,808 到 +9,223,372,036,854,775,807 之間的值.現(xiàn)在正如您在下面看到的,我創(chuàng)建了一個(gè)名為 testLong
的 long
變量,盡管當(dāng)我插入 9223372036854775807 作為值時(shí),我收到一條錯(cuò)誤消息:
I am working with data types at the moment in Java, and if I have understood correctly the type long
accepts a value between the ranges of -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807. Now as you can see below, I have create a long
variable called testLong
, although when I insert 9223372036854775807 as the value, I get an error stating:
int 類型的字面量 9223372036854775807 超出范圍.
The literal 9223372036854775807 of the type int is out of range.
我不知道為什么它把 long
數(shù)據(jù)類型稱為 int
.
I don't know why it is referring to the long
data type as an int
.
有人有什么想法嗎?
代碼:
char testChar = 01;
byte testByte = -128;
int testInt = -2147483648;
short testShort = -32768;
long testLong = 9223372036854775807;
float testFoat;
double testDouble = 4.940656458412;
boolean testBool = true;
推薦答案
在末尾加一個(gè)大寫的L
:
long value = 9223372036854775807L;
否則,編譯器將嘗試將文字解析為 int
,因此會(huì)出現(xiàn)錯(cuò)誤消息
Otherwise, the compiler will try to parse the literal as an int
, hence the error message
這篇關(guān)于int 類型的字面量 xyz 超出范圍的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!