声明带有"L"和不带有"L"的长变量有什么区别?

编程入门 行业动态 更新时间:2024-10-28 06:34:47
本文介绍了声明带有"L"和不带有"L"的长变量有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

以下内容有什么区别?

long var1 = 2147483647L; long var2 = 2147483648;

(或任何原始变量声明)带有或不带有L时,是否存在任何性能问题?是强制性的吗?

(or any primitive variable declaration) Does it have any performance issue with or without L? Is it mandatory?

推荐答案

在第一种情况下,您要为 long 变量(L 或 l 后缀表示 long 类型.

In the first case you are assigning a long literal to a long variable (the L or l suffix indicates long type).

在第二种情况下,您要为 long 变量分配一个 int 文字(在不提供后缀的情况下为默认类型)(这会导致从 int 到 long ),这意味着您被限制在从 Integer.MIN_VALUE 到 Integer.MAX_VALUE 的范围内 -2147483648 到 2147483647 ).

In the second case you are assigning an int literal (that's the default type when no suffix is supplied) to a long variable (which causes an automatic type cast from int to long), which means you are restricted to the range from Integer.MIN_VALUE to Integer.MAX_VALUE (-2147483648 to 2147483647).

这就是原因

long var2 = 2147483648;

不通过编译( 2147483648 大于 Integer.MAX_VALUE ).

另一方面

long var2 = 2147483648L;

将通过编译.

更多推荐

声明带有"L"和不带有"L"的长变量有什么区别?

本文发布于:2023-07-28 14:02:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1231593.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   有什么区别   声明   quot

发布评论

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

>www.elefans.com

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