02day

day02

注释

// 单行注释

/* */多行注释

/** */文档注释

关键字

关键字

标识符

  1. 所有的标识符都应该以字母吗,美元¥,下划线开头

  2. 首字母之后可以是任意字母,美元,下划线,数字组合

  3. 不能使用关键字作为变量名或方法名

  4. 标识符区分大小写

数据类型

  1. 所有变量使用的时候都必须严格复合要求,所有变量都必须先定义之后才能使用

  2. 定义变量:数据类型+变量名=值; 分号结尾

  3. java数据类型分为两种:基本类型、引用类型

  4. 基本类型:数值类型(整型(byte 一个字节 、short 两个字节、int 四个字节、long 八个字节)、浮点型(float 四个字节、double 八个字节)、字符类型(char 两个字节))布尔类型(true、false 一位)

  5. 引用类型:类、接口、数组

  6. //进制 二进制0b 八进制0 十进制 十六进制0x
           int i = 10;
           int i1 = 010;
           int i2 = 0x10;  //十六进制0x 0~9 A~F 16
           System.out.println(i);
           System.out.println(i1);
           System.out.println(i2);
  7. //浮点数扩展 银行业务用BigDecimal 数学工具类
           //最好完全使用浮点数比较
           //最好完全使用浮点数比较
           //最好完全使用浮点数比较
           float a = 0.1f;
           double b = 1.0 / 10;
           System.out.println(a == b);//false

           float c = 1212212121212f;
           float d = c + 1;
           System.out.println(c == d);//true
  8. //字符型拓展
           char h1 = 'a';
           char h2 = '中';

           System.out.println(h1);
           System.out.println((int) h1);//强制转换
           System.out.println(h2);
           System.out.println((int) h2);//强制转换
           //所有的字符本质上都是数字
           //编码 Unicode 2字节 最多可以表示0~65536个字符(不止)2^16=65536

           //U0000~UFFFF
           char c3 = '\u0061';
           System.out.println(c3); //a

           //转义字符
           //\t 制表符
           //\n 换行
           System.out.println("hello\t\nworld!");
  9. String sc = new String("hello,world!");
           String sb = new String("hello,world!");
           System.out.println(sc == sb);//false

           String sd = "hello,world";
           String se = "hello,world";
           System.out.println(sd == se);//true
           //对象 从内存分析
  10. //布尔值
           boolean flag = true;
           if (flag == true) {
          }
           if (flag) {
          }
           //less is more

类型转换

java是强类型语言,有些运算的时候需要用到强制类型转换

低--------------------->高 自动转换

byte,short,char->int->long->float(小数的优先级大于int整数型)->double

高---------------------->低 强制转换

//操作数比较大的时候,注意一出问题
       //JDK7新特性,数字之间可以用下滑线分割
       int money = 10_0000_0000;
       int years=20;
       int total=money*years;//-1474836480,计算的时候已经移溢出了,应先转换在计算
       long total2=money*years;//-1474836480,转换之前就已经存在问题了
       long total3=money*((long)years);//20000000000,先转换,在计算


       System.out.println(money);
       System.out.println(total);
       System.out.println(total2);
       System.out.println(total3);

 

 

 

 

posted @ 2021-04-08 17:45  卧剑之鱼  阅读(70)  评论(0)    收藏  举报