Java基础(一) 2021.1.26

Java基础(一)

一、注释,标识符,关键字

  1. 注释–用斜体表明

    • 书写注释是个好习惯
    • 单行注释 line comment(//)
    • 多行注释 block comment(/* */)
    • 文档注释 JavaDoc(/** */)
    /***
     *
     *   █████▒█    ██  ▄████▄   ██ ▄█▀       ██████╗ ██╗   ██╗ ██████╗
     * ▓██   ▒ ██  ▓██▒▒██▀ ▀█   ██▄█▒        ██╔══██╗██║   ██║██╔════╝
     * ▒████ ░▓██  ▒██░▒▓█    ▄ ▓███▄░        ██████╔╝██║   ██║██║  ███╗
     * ░▓█▒  ░▓▓█  ░██░▒▓▓▄ ▄██▒▓██ █▄        ██╔══██╗██║   ██║██║   ██║
     * ░▒█░   ▒▒█████▓ ▒ ▓███▀ ░▒██▒ █▄       ██████╔╝╚██████╔╝╚██████╔╝
     *  ▒ ░   ░▒▓▒ ▒ ▒ ░ ░▒ ▒  ░▒ ▒▒ ▓▒       ╚═════╝  ╚═════╝  ╚═════╝
     *  ░     ░░▒░ ░ ░   ░  ▒   ░ ░▒ ▒░
     *  ░ ░    ░░░ ░ ░ ░        ░ ░░ ░
     *           ░     ░ ░      ░  ░
     */
    
  2. 标识符

    • 开头以A-Z、a-z、$、_开始
    • 不能使用关键字作为变量名或者标识符
    • 大小写十分敏感

二、数据类型

  1. 强类型语言:(C、Java)要求变量的使用需要严格符合规定, 所有变量必须先定义后使用–安全,速度慢

  2. 弱类型语言:(VB、JS)

  3. Java数据类型

    1. 基本类型

      1. 数值类型

        1. 整数类型:byte、short、int、long

        2. 浮点类型:float、double

        3. 字符类型:char

          String不是基本类型,是类

      2. boolean类型:占一位,true或者false

    //整数
    int num2 = 10;//最常用
    byte num1 = 127;
    short num3 = 30;
    long num4 = 30L;//long类型要在数字后面加L
    
    //小数
    float num5 = 50.1F;//float类型要在数字后面加F
    double num6 = 3.1415926563213548413;
    
    //字符
    char name = 'A';
    /*
    字符串String不是关键字,是类
    String name = "lean";
     */
    
    //boolean类型
    boolean flag = true;
    
    1. 引用类型
      • 接口
      • 数组
    2. 面试拓展
    //整数拓展:   二进制0b    十进制     八进制0开头      十六进制0x开头
    int i1 = 10;
    int i2 = 010;
    int i3 = 0x10;
    
    System.out.println(i1);
    System.out.println(i2);
    System.out.println(i3);
    System.out.println("==============================");
    
    
    //==========================================================
    /*浮点数拓展
    float  有限  离散  舍入误差  大约  接近但不等于
    最好完全不用float类型比较
    最好完全不用float类型比较
    最好完全不用float类型比较
    如需比较需要用BigDecimal 数学工具类
     */
    float f = 0.1f;
    double d = 1.0/10;
    System.out.println(f == d);
    
    float f1 = 13211351f;
    float f2 = f1 + 1;
    System.out.println(f1 == f2);
    System.out.println("==============================");
    
    
    //==========================================================
    /*
    字符拓展
    所有的字符本质上都是数字
    编码Unicode表:a = 97, A = 65
     */
    char c1 = 'a';
    char c2 = '中';
    System.out.println(c1);
    System.out.println((int)c1);//强制类型转换
    System.out.println(c2);
    System.out.println((int)c2);//强制类型转换
    char c3 = '\u0061';//a
    System.out.println(c3);
    System.out.println("==============================");
    
    
    //==========================================================
    /*
    转义字符
    \t  制表符
    \n  换行符
     */
    System.out.println("HelloWorld");
    System.out.println("Hello\tWorld");
    System.out.println("Hello\nWorld");
    System.out.println("==============================");
    
    
    //==========================================================
    /*
    布尔值扩展
    Less is more.
     */
    boolean flag = true;
    if(flag == true){}
    if(flag){}
    

三、类型转换

  • 原因:java是强类型语言,需要把不同数据类型先转化为同一类型,然后进行运算
  • 按照byte, short, char, int, long, float, double储存从小到大
  1. 强制类型转换:高到低
  2. 自动类型转换:低到高
  3. 大数的溢出问题
//操作比较大的数的时候,需要注意溢出问题
//jdk7以后,可以在数字下加下划线来区分单位
int money = 10_0000_0000;//int类型范围粗略记为21亿
System.out.println(money);
int years = 20;

int total1 = money*years;
System.out.println(total1);//-1474836480,结果错误溢出

//改成long类型来储存结果
long total2 = money*years;
System.out.println(total2);//仍然溢出,因为在把值赋给total2前,是用int类型来计算并且储存结果,再赋给long类型值

//把其中一个值强制转换为long类型进行计算
long total3 = money*(long)years;
System.out.println(total3);

//大小写问题:l和L,小写l很容易看成数字1,所以通常只用L表示Long类型
long l = 45555L;
  • 注意点

    1. 不能对bool类型进行转换
    2. 不能把对象类型转换为不相干的类型

    字符和数字之间的转换:

    char c = 'a';
    int d = c+1;
    System.out.println(d);
    System.out.println((char)d);
    
    1. 高容量到低容量是强制转换

    2. 转换的时候可能存在内存溢出,或者精度问题

    System.out.println((int)23.7);//double类型
    System.out.println((int)-45.98f);//float类型
    

四、变量,常量

  1. 变量

    • 变量必须指明其类型
    • 变量声明必须以分号结束
    int a = 1;
    int b = 2;
    int c = 3;
    String name = "嘤嘤嘤";
    char x = 'X';
    double pi = 3.14;
    
    • java变量的要素:变量名,变量类型,作用域
    /*
    类变量:从属于类
    实例变量:从属于对象;如果不初始化,会输出默认值
    局部变量:仅在函数内部有效
    布尔值默认是false
    除了基本类型,其余默认值均为null
     */
    String name;//无须初始化
    int age;
    static double salary = 2500;
    
    //main函数
    public static void main(String[] args) {
        //局部变量:必须声明和初始化值
        int i = 10;
        System.out.println(i);
    
        //实例变量:格式:变量类型 变量名字 = new Demo06();
        Demo06 demo06 = new Demo06();
        System.out.println(demo06.name);
        System.out.println(demo06.age);
    
        //类变量 static
    
    }
    
  2. 常量

    • final 常量名 = 值;
    • 常量名一般用大写字母
  3. 变量的命名规范

    • 首字母小写和驼峰原则
      • 类成员变量:lastName, monthSalary
      • 局部变量:finalSalary
      • 方法名:run(), runningMan()
    • 首字母大写和驼峰原则
      • 类名:Man,Demo
    • 大写字母和下划线
      • 常量:MAX_VALUE, PI
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值