2020年java基础知识课堂笔记

2020年java基础知识课堂笔记

1.注释、 标识符、关键字

注释

注释不会被执行,是写给别人看的

  • 单行注释 // 注释语句
  • 多行注释 /* 注释语句*/
  • 文档注释
/*
 *@author Afan
 *@version 
 */

标识符

定义:类名、变量名以及方法名都被称为标识符

  • 所有的标识符都应该由字母(A-Z或者a-z),美元符($),下划线(_)开始
  • 首字母之后可以是字母(A-Z或者a-z),美元符($),下划线(_)或数字的任何字符组合
  • 不能使用关键字作为变量名或方法名
  • 标识符是大小写敏感
  • 合法标识符举例:age、$salary、_value
  • 非法标识符举例:123abc、-salary、#abc

关键字

abstractassertbooleanbreakbyte
casecatchcharclassconst
continuedefaultdodoubleelse
enumextendsfinalfiniallyfloat
forgotoifimplementsimport
instanceofintinterfacelongnative
newpackageprivateprotectedpublic
returnstrictfpshortstaticsuper
switchsynchronizedthisthrowthrows
transienttryvoidvolatilewhile

2. 数据类型讲解

java是强类型语言

强类型语言:要求变量的使用要严格规定,所有变量都必须先定义后才能使用

java的数据类型

  • 基本数据类型
    • 数值类型
      • 整数类型
        1. byte 占1个字节
        2. short 占2个字节
        3. int 占4个字节
        4. long 占8个字节
      • 浮点类型
        1. float 占4个字节
        2. double 占8个字节
      • 字符类型
        1. char 占2个字节
    • boolean类型
      • true和false 占1个字节

java8种基本数据类型

数据类型字节包装类默认值最小值最大值
byte1Byte0-128127
short2Short0-3276832768
int4Integer0-2^312^31-1
long8Long0L-2^632^63-1
float4Float0.0f1.4***E***-453.4028235***E***38
double8Double0.01.4***E***-3241.7976931348623157***E***308
char2Character065535
boolean1Booleanfalsefalsetrue

实例代码

public class Test
{
    public static void main(String[] args) {
        //八大数据类型

        //整数
        int num1 = 10;//最常用
        byte num2 = 20;
        short num3 = 30;
        long num4 = 30L;//Long类型要在数字后面加个L

        //浮点数:小数
        float num5 = 50.1F;//float类型要在后面加个F
        double num6 = 3.141592653589793238462643;

        //字符
        char name1 = '凡';
        //字符串:String不是关键字,是类
        //String name2 = "阿凡不平凡";

        //布尔值:是非
        boolean flag = true;
        //boolean flag = false;
    }
}
  • 引用数据类型
    • 类型
    • 接口
    • 数组

3. java数据类型拓展

实例代码

public class Test
{
    public static void main(String[] args) {
        //整数拓展:   进制      二进制0b       十进制     八进制0        十六进制0x

        int i = 10;
        int i2 = 010;  //八进制0   逢8进1
        int i3 = 0x10;  //十六进制0x   0~9 A~F 逢16进1

        System.out.println(i);  //10
        System.out.println(i2); //8
        System.out.println(i3); //16
        // -----------------------------------
        //浮点数拓展:浮点数是有限的,离散的,舍入误差,大约,接近但不等于
        //float
        //double
        //结论:浮点数最好完全不要用于比较

        float f = 0.1f;  //0.1
        double d = 1.0/10;  //0.1
        System.out.println(f==d); //false

        float d1 = 22222222222222222222f;
        float d2 = d1 + 1;
        System.out.println(d1==d2); //true
        //------------------------------------
        //字符拓展: 所有字符本质还是数字
        //编码 Unicode表:(97 = a 65 = A)    2字节     0~65536

        char c1 = 'a';
        char c2 = '凡';
        System.out.println(c1);      //a
        System.out.println((int)c1); //97
        System.out.println(c2);      //凡
        System.out.println((int)c2); //20961

        //U0000~UFFFF
        char c3 = '\u0061';
        System.out.println(c3); //a
        //转义字符
        //  \t   制表符
        //  \n   换行
        //  ......
        System.out.println("Hello\nWorld");

        //----------------------------------
        //String创建对象
        String s1 = new String("hello world");
        String s2 = new String("hello world");
        System.out.println(s1==s2);   //false

        String s3 = "hello world";
        String s4 = "hello world";
        System.out.println(s3==s4);   //true

        //布尔值扩展:布尔值默认值是false
        boolean flag = true;
        if(flag == true){}
        if(flag){}     //两者含义相同

    }
}

4. 类型转换

  • 强制转换 (数据类型)变量名 高容量–>低容量
    • 注意点
      1. 不能对布尔值转换
      2. 不能把对象类型转换为不相干的类型
      3. 在把高容量转化为低容量时,强制转换
      4. 转换的时候可能存在内存溢出,或者精度问题

实例代码

public class Test
{
    public static void main(String[] args) {
        
        //操作比较大的数的时候注意溢出问题
        //JDK新特性,数字之间可以用下划线分割
        int money = 10_0000_0000;
        int years = 20;

        int total = money*years;
        long total2 = money*years;
        long total3 = money*((long)years);
        long total4 = ((long)money)*years;

        System.out.println(total);  //-1474836480 ,计算时溢出
        System.out.println(total2); //-1474836480 ,转换之前已经出现问题
        System.out.println(total3); //20000000000 ,先把一个数强制类型转换
        System.out.println(total4); //20000000000

    }
}
  • 自动转换 低容量–>高容量

5. 变量、常量、作用域

变量

  • 变量:就是可以变化的量
  • java是一种强类型语言,每个变量都必须声明其类型
  • java变量是程序中最基本的存储单元,其要素包括变量名变量类型作用域

变量的命名规范

  • 所有变量、方法、类名:见名知意
  • 类成员变量:首字母小写和驼峰原则:如monthSalary 除了第一个单词首字母小写,后面其他单词首字母大写
  • 局部变量:首字母小写和驼峰原则
  • 常量:大写字母和下划线:MAX_VALUE
  • 类名:首字母大写和驼峰原则:GoodMan
  • 方法名:首字母小写和驼峰原则:runRun()

注意事项

  • 每个变量都有类型,类型可以是基本类型,也可以是引用类型
  • 变量名必须是合法的标识符
  • 变量声明是一条完整的语句,因此每一个声明都必须以分号结束

声明变量: 数据类型 变量名 = 值;

常量

  • 常量:初始化(initialize)后不能在改变值!不会变动的值

  • 所谓常量可以理解为一种特殊的变量,它的值被设定后,在程序运行过程中不允许被改变

    final 常量名 = 值;

    final double PI = 3.14;

  • 常量名一般使用大写字符

public class Test{
    //修饰符,不存在先后顺序
    static final double PI = 3.14;
    
    public static void main(String[] args) {
        System.out.println(PI); //3.14
    }
}    

变量作用域

  • 类变量:有static关键字

  • 实例变量:从属于对象,如果不自行初始化,则是这个类型的默认值

    布尔值:默认值是false

    除了基本类型,其余的默认值都是null

  • 局部变量:必须声明和初始化值

public class Variable
{   
    static int allClicks = 0;   //类变量
    String str = "hello world"; //实例变量
    
    public void method(){
        int i = 0; //局部变量
    }
}

实例代码

public class Test{
    //类变量 static
    static double salary = 2500; 

    //属性:变量
    //实例变量:除了8种基本类型,其余类型默认值为null
    String name; 
    int age;

    //main方法
    public static void main(String[] args) {
        //局部变量:必须声明和初始化值
        int i = 10;
        System.out.println(i); //10

        //变量类型 变量名字 = new 变量类型();
        Test test = new Test();
        System.out.println(test.age);  //0
        System.out.println(test.name); //null

        System.out.println(salary); //2500.0
    }
}

6. 基本运算符

在这里插入图片描述

二元运算符实例代码

public class Test{
    public static void main(String[] args) {
        // 二元运算符
        //ctr+D: 复制当前行到下一行
        int  a = 10;
        int  b = 20;
        int  c = 6;

        System.out.println(a+b);  //30
        System.out.println(a-b);  //-10
        System.out.println(a*b);  //200
        //System.out.println(a/b);  //0   除法运算特殊,与数学不同,需要强制转换
        System.out.println(a/(double)b);  //0.5
        System.out.println(a%b) //4 取余 模运算
    }
}
public class Test{
    public static void main(String[] args) {
        long a = 123123123123123L;
        int b = 123;
        short c = 10;
        byte d = 8;
        /*注意:
          1. long参与运算,运算结果是long类型
          2. 比int类型容量低的基本类型参与运算,运算结果是int类型
          */

        System.out.println(a+b+c+d);  //123123123123264  Long类型
        System.out.println(b+c+d);  //141  int类型
        System.out.println(c+d);  //18  int类型
        
    }
}

关系运算符实例代码

public class Test{
    public static void main(String[] args){
        //关系运算符返回的结果:正确  错误  布尔值
        int a =10;
        int b =20;

        System.out.println(a>b);  //false
        System.out.println(a<b);  //true
        System.out.println(a==b);  //false
        System.out.println(a!=b);  //true
        
        Test test = new Test();
        System.out.println(test instanceof Test); //true 
        
        /*instanceof 用来测试一个对象是否为一个类的实例
         *boolean result = obj instanceof class
         *注意点:
         * 1. obj必须为引用类型,不能为基本类型
         * 2. obj可以为null
         * 3. obj可以为class类的直接或间接子类
         * 4. obj可以为class接口的实现类
         */
    }
}

一元运算符

public class Test{
    public static void main(String[] args){
        //++ -- 自加,自减  一元运算符
        int a = 3;

        int b = a++; //执行完这行代码后,先给b赋值,a再自增1
        System.out.println(a); //4

        int c = ++a;   //执行完这行代码后,a自增1,再赋值给b

        System.out.println(a);  //5
        System.out.println(b);  //3
        System.out.println(c);  //5
        //很多数学运算,会使用一些工具类来操作
        double pow =Math.pow(2,3);
        System.out.println(pow); //8.0
    }
}

逻辑运算符

public class Test{
    public static void main(String[] args){
       // 与(and)    或(or)     非(!)
        boolean a = true;
        boolean b = false;

        System.out.println(a&&b);    //false
        System.out.println(a||b);    //true
        System.out.println(!(a&&b)); //true

        //短路运算
        int c = 5;
        boolean d = (c < 4)&&(c++ < 4);
        System.out.println(d);  //false
        System.out.println(c);  //5
    }
}

位运算

  • 左移:<< 相当于*2
  • 右移:>> 相当于 /2
public class Test{
    public static void main(String[] args){
        /*
         位运算是二进制运算
         A = 0011 1100
         B = 0000 1101
         -------------------
         A&B = 0000 1100
         A|B = 0011 1101
         A^B = 0011 0001
         ~B = 1111 0010   (了解!!)

         <<   *2
         >>   /2    效率极高!!!
         */
        System.out.println(2<<3);  //16
        System.out.println(8>>2);  //2
    }
} 

三元运算符

public class Test{
    public static void main(String[] args){
        int a = 10;
        int b = 20;

        a+=b; //a=a+b;
        a-=b; //a=a-b;

        //字符串连接符  + ,String
        System.out.println(a+b);  //30
        System.out.println(""+a+b); //1020
        System.out.println(a+b+""); //30

        //三元运算符  x ? y : z
        //如果 x == true,则结果为y,否则结果为z

        int score = 80;
        String type = score < 60 ? "不及格":"及格";
        System.out.println(type);  //及格
    }
}

7. 包机制

  • 为了更好地组织类,java提供了包机制,用于区别类名的命名空间
  • 包语句的语法格式

package 包名[.包名2[.包名3]];

  • 一般利用公司域名倒置作为包名

eg: com.fan.Xxx

  • 在java中导入某一个包的成员

import 包名.类名;

8. javaDoc生成文档

  • javadoc命令用来生成自己的API文档的

  • 参数信息

    • @author 作者名
    • @version 版本号
    • @since 指明需要最早使用的jdk版本
    • @param 参数名
    • @return 返回值情况
    • @throws 异常抛出情况
  • 使用方法

    • cmd命令行:javadoc 参数(-encoding UTF-8 -charset UTF-8) java文件
    • IDEA生成API文档:打开idea,选择项目。点击工具栏 Tools->Generate JavaDOC

9. 后言

  • 本网课笔记知识来源于 B站 狂神说java
  • 该网课干货满满,老师讲课清晰,重点是免费,值得推荐 !!!
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值