Java长号太大错误?

编程入门 行业动态 更新时间:2024-10-26 08:33:24
本文介绍了Java长号太大错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

为什么我的int号太大而long被分配到min和max?

Why do I get an int number is too large where the long is assigned to min and max?

/* long: The long data type is a 64-bit signed two's complement integer. 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). Use this data type when you need a range of values wider than those provided by int. */ package Literals; public class Literal_Long { public static void main(String[] args) { long a = 1; long b = 2; long min = -9223372036854775808; long max = 9223372036854775807;//Inclusive System.out.println(a); System.out.println(b); System.out.println(a + b); System.out.println(min); System.out.println(max); } }

推荐答案

全部java中的文字数字默认为 ints ,其范围 -2147483648 至 2147483647 包含。

All literal numbers in java are by default ints, which has range -2147483648 to 2147483647 inclusive.

您的文字超出此范围,因此要进行此编译,您需要指明它们 long 文字(即后缀 L ):

Your literals are outside this range, so to make this compile you need to indicate they're long literals (ie suffix with L):

long min = -9223372036854775808L; long max = 9223372036854775807L;

请注意,java支持大写 L 和小写 l ,但我建议不使用小写 l ,因为它看起来像 1 :

Note that java supports both uppercase L and lowercase l, but I recommend not using lowercase l because it looks like a 1:

long min = -9223372036854775808l; // confusing: looks like the last digit is a 1 long max = 9223372036854775807l; // confusing: looks like the last digit is a 1

Java语言规范相同的

如果整数文字后缀为ASCII字母L或l(ell),则整数文字的长度为long;否则它的类型为int(§4.2.1)。

An integer literal is of type long if it is suffixed with an ASCII letter L or l (ell); otherwise it is of type int (§4.2.1).

更多推荐

Java长号太大错误?

本文发布于:2023-06-01 10:54:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/413463.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:长号   太大   错误   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!