compile-time constant expression

本文介绍了编译期常量表达式的定义及其组成元素,包括基本类型和字符串字面量、类型转换、算术和逻辑操作符等,并列举了多个实例。编译期常量表达式用于switch语句的case标签,在赋值转换中具有特殊意义。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

A compile-time constant expression is an expression denoting a value of primitive type or a String that does not complete abruptly and is composed using only the following:

 

Literals of primitive type and literals of type String (§3.10.5) Casts to primitive types and casts to type String The unary operators + , - , ~ , and ! (but not ++ or -- ) The multiplicative operators * , / , and % The additive operators + and - The shift operators << , >> , and >>> The relational operators < , <= , > , and >= (but not instanceof ) The equality operators == and != The bitwise and logical operators & , ^ , and | The conditional-and operator && and the conditional-or operator || The ternary conditional operator ? : Parenthesized expressions whose contained expression is a constant expression. Simple names that refer to constant variables (§4.12.4) . Qualified names of the form TypeName . Identifier that refer to constant variables (§4.12.4) .

Compile-time constant expressions are used in case labels in switch statements (§14.11) and have a special significance for assignment conversion (§5.2) . Compile-time constants of type String are always "interned" so as to share unique instances, using the method String.intern.

A compile-time constant expression is always treated as FP-strict (§15.4) , even if it occurs in a context where a non-constant expression would not be considered to be FP-strict.

Examples of constant expressions:

true
(short)(1*2*3*4*5*6)
Integer.MAX_VALUE / 2
2.0 * Math.PI
"The integer " + Long.MAX_VALUE + " is mighty big."

 

其中complete abruptly就是指突然结束,比如抛出一个异常之类

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值