---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 9 Edition"
java_version: 9
chapter: "Chapter 15. Expressions"
section: "15.28"
heading: "15.28. Constant Expressions"
parent: "Chapter 15. Expressions"
source: https://docs.oracle.com/javase/specs/jls/se9/html/jls-15.html#jls-15.28
id: "15.28"
url: https://jvm.obfuscated.software/jls/se9/15.28.html
raw: https://jvm.obfuscated.software/jls/se9/15.28.md
---

# 15.28. Constant Expressions

```
ConstantExpression:
  Expression
```

A *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.1](3.10.1.md), [§3.10.2](3.10.2.md), [§3.10.3](3.10.3.md), [§3.10.4](3.10.4.md), [§3.10.5](3.10.5.md))
- Casts to primitive types and casts to type `String` ([§15.16](15.16.md))
- The unary operators `+`, `-`, `~`, and `!` (but not `++` or `--`) ([§15.15.3](15.15.3.md), [§15.15.4](15.15.4.md), [§15.15.5](15.15.5.md), [§15.15.6](15.15.6.md))
- The multiplicative operators `*`, `/`, and `%` ([§15.17](15.17.md))
- The additive operators `+` and `-` ([§15.18](15.18.md))
- The shift operators `<<`, `>>`, and `>>>` ([§15.19](15.19.md))
- The relational operators `<`, `<=`, `>`, and `>=` (but not `instanceof`) ([§15.20](15.20.md))
- The equality operators `==` and `!=` ([§15.21](15.21.md))
- The bitwise and logical operators `&`, `^`, and `|` ([§15.22](15.22.md))
- The conditional-and operator `&&` and the conditional-or operator `||` ([§15.23](15.23.md), [§15.24](15.24.md))
- The ternary conditional operator `? :` ([§15.25](15.25.md))
- Parenthesized expressions ([§15.8.5](15.8.5.md)) whose contained expression is a constant expression.
- Simple names ([§6.5.6.1](6.5.6.1.md)) that refer to constant variables ([§4.12.4](4.12.4.md)).
- Qualified names ([§6.5.6.2](6.5.6.2.md)) of the form *TypeName* `.` *Identifier* that refer to constant variables ([§4.12.4](4.12.4.md)).

Constant expressions of type `String` are always "interned" so as to share unique instances, using the method `String.intern`.

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

Constant expressions are used as `case` labels in `switch` statements ([§14.11](14.11.md)) and have a special significance in assignment contexts ([§5.2](5.2.md)) and the initialization of a class or interface ([§12.4.2](12.4.2.md)). They may also govern the ability of a `while`, `do`, or `for` statement to complete normally ([§14.21](14.21.md)), and the type of a conditional operator `? :` with numeric operands.

**Example 15.28-1. 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."
```
