zoukankan      html  css  js  c++  java
  • 常量Constant

    常量通常指的是一个固定的值,例如:1、2、3、’a’、’b’、true、false、”helloWorld”等。

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

    声明格式为:

    final  type  varName = value;

    【示例2-9】常量的声明及使用

    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等称为符号常量。

    老鸟建议

    变量和常量命名规范(规范是程序员的基本准则,不规范会直接损害你的个人形象):

    1. 所有变量、方法、类名:见名知意

    2. 类成员变量:首字母小写和驼峰原则:  monthSalary

    3. 局部变量:首字母小写和驼峰原则

    4. 常量:大写字母和下划线:MAX_VALUE

    5. 类名:首字母大写和驼峰原则:  Man, GoodMan

    6. 方法名:首字母小写和驼峰原则: run(), runRun()

    public class TestConstant {
        public static void main(String[]args) {
            int age=18;
            final String NAME="gaoqi";//final修饰的变量符号常量
            final double PI=3.14;
            double r=4;
            double area=PI*r*r;
            double circle=2*PI*r;
            System.out.println("area="+area);
            System.out.println("circle="+circle);
            
            
            
        }
    
    }
  • 相关阅读:
    【NOIP2016】换教室
    【NOIP模拟赛】总结
    【Codeforces Round 418】An impassioned circulation of affection DP
    DP测试总结
    【NOIP2012】疫情控制
    【HNOI2016】序列 莫队+单调栈+RMQ
    【Luogu P2709 小B的询问】莫队
    【HNOI2017】影魔
    【HNOI2017】大佬
    阿里云MaxCompute 2019-7月刊
  • 原文地址:https://www.cnblogs.com/long-holiday/p/10096518.html
Copyright © 2011-2022 走看看