> For the complete documentation index, see [llms.txt](https://zjax.gitbook.io/javase/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://zjax.gitbook.io/javase/untitled-1/2.5-chang-liang.md).

# 2.5 常量

**常量（constant）：通常指的是一个固定的值，例如：1、2、3、'a'、'b'、true、false、"helloWorld"等。**

在 Java 语言中，主要是利用关键字 `final` 来定义一个常量。 常量一旦被初始化后不能再更改其值。

**格式**：`final type varName = value;`

常量的声明及使用：

```java
 public class TestConstants {
     public static void main(String[] args) {
         final double PI = 3.14;
         // PI = 3.15; // 编译错误，不能再被赋值！ 
         double r = 4;
         double area = PI * r * r;
         double circle = 2 * PI * r;
         System.out.println("area = " + area);
         System.out.println("circle = " + circle);
     }
 }
```

为了更好的区分和表述，一般将1、2、3、'a'、'b'、true、false、"helloWorld" 等称为**字面常量**，而使用 final 修饰的 PI 等称为**符号常量**。
