JAVA基础---2. 常量

常量分类:

  • 字符串常量:双引号括起来的就是字符串常量,"abc"、"你好"
  • 整数常量:100、200...
  • 浮点数常量: 1.1、-3.14...
  • 字符常量:单引号括起来的就是字符常量,'A'、'中'...
  • 布尔常量:仅true、false
  • 空常量:null(没有任何数据)
public class Demo01Const{
    public static void main(String[] args){
        
        //字符串常量
        System.out.println("abc");
        System.out.println("");//字符串两双引号之间的内容为空,可以正常打印
        System.out.println("xyz");
        
        //整数常量
        System.out.println(-100);
        
        //浮点数常量
        System.out.println(3.14);
        
        //字符常量
        System.out.println('A');
        //System.out.println('');//两单引号之间不可以为空,否则出错
        //System.out.println('AB');//两单引号之间必须有且仅有一个字符,多了不行。
        
        //布尔常量
        System.out.println(true);
        System.out.println(false);
        
        //空常量 
        System.out.println(null);//空常量不可以用来直接打印输出
    }
}

数据类型

  • 基本数据类型
  • 引用数据类型:字符串、数组、类、接口、Lambda

    基本数据类型

  • 整数型 byte short int long
  • 浮点型 float double
  • 字符型 char
  • 布尔型 boolean

    注意事项:
  • 字符串不是基本类型而是引用类型
  • 浮点型可能只是一个近似值,并非精确值。
  • 数值范围和字节数不一定相关,例如float数据范围比long更广,但float是4字节,long是8字节
  • 浮点数当中默认值是double,如果一定使用float类型,需要加上一个后缀F;如果是整数,默认为int型,如果一定要使用long类型,需要加上一个后缀L。推荐使用大写字母后缀。

猜你喜欢

转载自www.cnblogs.com/deer-cen/p/12109355.html