常量通常指的是一个固定的值,例如: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); } }

常量Constant的更多相关文章

  1. 基于TensorFlow的深度学习系列教程 2——常量Constant

    前面介绍过了Tensorflow的基本概念,比如如何使用tensorboard查看计算图.本篇则着重介绍和整理下Constant相关的内容. 基于TensorFlow的深度学习系列教程 1--Hell ...

  2. tensorflow中常量(constant)、变量(Variable)、占位符(placeholder)和张量类型转换reshape()

    常量 constant tf.constant()函数定义: def constant(value, dtype=None, shape=None, name="Const", v ...

  3. 常量指针(pointer to constant)和指针常量(constant pointer)

    一个指针可以操作两个实体,一个是指针值(即地址),一个是间访值(即指向的实体).于是指针的常量性也分为两种:常量指针(pointer to constant)和指针常量(constant pointe ...

  4. struts2常用的常量constant

    常用的常量配置  struts.serve.static.browserCache 该属性设置浏览器是否缓存静态内容.当应用处于开发阶段时,我们希望每次请求都获得服务器的最新响应,则可设置该属性为fa ...

  5. 如何在Java中定义常量(Constant)

    原本引自  http://blog.csdn.net/autofei/article/details/6419460 /** * Method One */ interface ConstantInt ...

  6. struts2常用的常量constant(转)

    原文地址:http://blog.csdn.net/wfcaven/article/details/5937548 常用的常量配置  struts.serve.static.browserCache  ...

  7. go语言入门教程:基本语法—常量constant

    一.常量的使用 1.1 常量声明 常量是一个简单值的标识符,在程序运行时,不会被修改的量. const identifier [type] = value 显式类型定义: const b string ...

  8. struts常量<constant>说明

    1.<constant name="struts.action.extension" value="do" />这个时候访问action都必须加.d ...

  9. Java中定义常量(Constant) 的几种方法

    为了方便大家交流Spark大数据,浪尖建了微信群,目前人数过多,只能通过浪尖或者在群里的朋友拉入群.纯技术交流,偶有吹水,但是打广告,不提醒,直接踢出.有兴趣加浪尖微信. 常量使用目的 1,为什么要将 ...

随机推荐

  1. Objective-C 对象的类型与动态结合

    创建: 2018/01/21 更新: 2018/01/22 标题前增加 [Objective-C] 完成: 2018/01/24 更新: 2018/01/24 加红加粗属性方法的声明 [不直接获取内部 ...

  2. E20171121-sl

    contrast  n. 对比,对照; 差异; 对照物,对立面; [摄] 反差;

  3. bzoj 1622: [Usaco2008 Open]Word Power 名字的能量【模拟】

    模拟即可,注意包含可以是不连续的 方便起见读入的时候全转成小写 #include<iostream> #include<cstdio> using namespace std; ...

  4. ngCordova插件说明

    转载自 http://my.oschina.net/u/1416844/blog/495026 参 考http://blog.csdn.net/superjunjin/article/details/ ...

  5. SpringBoot2.x版本整合SpringSecurity、Oauth2进行password认证

    很多人在进行项目开发时都会用到Oauth2.0结合SpringSecurity或者Shiro进行权限拦截以及用户验证,网上也有很多的案例,前几天项目里边需要用到,顺便整合了进来,特此写篇博客,记录下过 ...

  6. mydatepicker97 日历控件

    官方教程: http://www.my97.net/

  7. 数据传递--------博客-----------springMVC源码分析--RequestToViewNameTranslator请求到视图名称的转换

    参考来源:http://blog.csdn.net/qq924862077/article/details/54286976?utm_source=gold_browser_extension Req ...

  8. HTML基础2——综合案例2——复杂的嵌套列表

    <html> <head> <title></title> </head> <body> <ul type="d ...

  9. 编写UI自动化测试用例原则

    1.一个脚本是一个完整的场景,从用户登陆操作到用户退出系统关闭浏览器.2.一个脚本脚本只验证一个功能点,不要试图用户登陆系统后把所有的功能都进行验证再退出系统3.尽量只做功能中正向逻辑的验证,不要考虑 ...

  10. oracle创建临时表空间、用户表空间、创建用户关联表空间、授权等

    1.创建临时表空间 CREATE TEMPORARY TABLESPACE test_temp TEMPFILE 'C:\oracle\product\10.1.0\oradata\orcl\test ...