JavaSE:Java基础语法

JavaSE:Java基础语法


前言

语法规范

即使再小的帆也能远航


一、注释

书写注释是一个非常好的习惯

  1. 单行注释
  2. 多行注释
  3. 文档注释
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World!");

        //单行注释

        /*
        多行注释
        多行注释
         */

        //JavaDoc:文档注释
        /**
         * @Description HelloWorld
         * @Author 恰恰
         */

        //有趣的代码注释
        /***
         *                    _ooOoo_
         *                   o8888888o
         *                   88" . "88
         *                   (| -_- |)
         *                    O\ = /O
         *                ____/`---'\____
         *              .   ' \\| |// `.
         *               / \\||| : |||// \
         *             / _||||| -:- |||||- \
         *               | | \\\ - /// | |
         *             | \_| ''\---/'' | |
         *              \ .-\__ `-` ___/-. /
         *           ___`. .' /--.--\ `. . __
         *        ."" '< `.___\_<|>_/___.' >'"".
         *       | | : `- \`.;`\ _ /`;.`/ - ` : | |
         *         \ \ `-. \_ __\ /__ _/ .-` / /
         * ======`-.____`-.___\_____/___.-`____.-'======
         *                    `=---='
         *
         * .............................................
         *          佛祖保佑             永无BUG
         */

    }
}

二、标识符

  1. 关键字

在这里插入图片描述

  1. 标识符注意点
    • 开头:字母、美元$、下划线_
    • 字符组合:字母、美元$、下划线_、数字
    • 不能使用关键字作为变量名或方法名
    • 标识符大小写敏感
    • 合法标识符与非法标识符
    • 不建议用中文或拼音命名

三、数据类型

强类型语言:所有变量必须定义以后才能使用

  • 基本类型
  • 引用类型
    在这里插入图片描述
    在这里插入图片描述
public class Demo02 {
    public static void main(String[] args) {

        //八大基本数据类型

        //整数型(整数)
        int num1 = 10;      //整型,最常用
        byte num2 = 20;     //字符型
        short num3 = 30;    //短整型
        long num4 = 30L;    //长整型,Long类型要在数字后面加个L或l

        //浮点型(小数)
        float num5 = 3.14F;     //单精度型
        double num6 = 3.141592653589793238462643;   //双精度型
        //圆周率100位:π = 3.1415926 5358 9793 2384 6264 3383 2795 0288 4197 1693 9937 5105 8209 7494 4592 3078 1640 6286 2089 9862 8034 8253 4211 7067 9

        //字节型(字符)
        char letter = 'A';  //1个字母或1个汉字或1个数字或一个符号
        char word = '恰';
        char no = '1';
        char note = '#';
        
        //字符串,String不是关键字,是类
        String name = "恰恰";

        //布尔型:对错(判断)
        boolean flag = true;
        //boolean flag = false;

    }
}

什么是字节

  • 位(bit):是计算机 内部数据 储存的最小单位,如11001100是一个八位二进制数,b
  • 字节(byte):是计算机中 数据处理 的基本单位,B
  • 字符:是指计算机中使用的 字母、数字、字和符号

1B=8b
1KB=1024B
1M=1024KB
1G=1024MB

以下仅拓展

1TB=1024GB
1PB=1024TB
1EB=1024PB
1ZB=1024EB
1YB=1024ZB
1BB=1024YB

简称英文中文简称
KBKilobyte千字节
MBMegabyte兆字节
GBGigabyte吉字节千兆
TBTrillionbyte万亿字节太字节
PBPetabyte千万亿字节拍字节
EBExabyte百亿亿字节艾字节
ZBZettabaye十万亿亿字节泽字节
YBYottabyte一亿亿亿字节尧字节
BBBrontobyte一千亿亿亿字节

思考:
电脑的32位和64位的区别是什么?

四、数据类型扩展及面试题讲解

import java.math.BigDecimal;

public class Demo03 {
    public static void main(String[] args) {

        //整数拓展:
        //进制:二进制0b,十进制,八进制0,十六进制0x
        int i = 10;
        int i2 = 010;
        int i3 = 0x10;
        System.out.println(i);      //10
        System.out.println(i2);     //8
        System.out.println(i3);     //16
        System.out.println("========");

        //浮点数拓展
        //银行业务怎么表示?钱?
        //BigDecimal    数学工具类
        //float     有限  离散  舍入误差  大约  接近但不等于
        //double
        //最好完全避免使用浮点数进行比较
        float f = 0.1f;
        double d = 0.1;
        //double d = 1.0/10;
        System.out.println(f);
        System.out.println(d);
        System.out.println(f==d);   //false
        float f1 = 23131312312312313f;
        float f2 = f1 + 1;
        System.out.println(f1==f2); //true
        System.out.println("========");

        //字符拓展
        //所有的字符本质还是数字
        char c1 = 'a';
        char c2 = '中';
        System.out.println(c1);
        System.out.println((int)c1);    //强制转换
        System.out.println(c2);
        System.out.println((int)c2);    //强制转换
        //编码  Unicode表:(A=65  a=97  △=32  U0000 ~ UFFFF)  2字节  0 - 65536  Excel  2^16 = 65536
        char c3 = '\u0061';
        System.out.println(c3);    //a
        System.out.println("========");

        //转义字符
        //  \t  制表符
        //  \n  换行
        //......
        System.out.println("Hello\nworld");

        //对象  从内存分析
        String sa = new String("hello world");
        String sb = new String("hello world");
        System.out.println(sa==sb);     //false
        String sc = "hello world";
        String sd = "hello world";
        System.out.println(sc==sd);     //true

        //布尔值拓展
        boolean flag = true;
        //if(flag==true){}  //新手
        //等同于
        //if(flag){}    //老手
        //Less id more!  代码要精简易读

    }
}

五、类型转换

Java:强类型语言
低---->高
byte,short,char->int->long->float->double
运算中,不同类型的数据先转化为同一类型,然后再进行计算

  • 强制类型转换
  • 自动类型转换
public class Demo05 {
    public static void main(String[] args) {

        int i = 128;
        byte b = (byte)i;
        System.out.println(i);  //128
        System.out.println(b);  //-128  //内存溢出
        //强制转换  (类型)变量名  从高转到低
        //自动转换  从低转到高

        /*
        注意点:
        1.不能对布尔值进行转换
        2.不能把对象类型转换为不相干的类型
        3.在把高容量转换到低容量的时候,强制转换
        4.转换的时候可能存在内存溢出,或者精度问题!
         */

        System.out.println("========");
        System.out.println((int)23.7);  //23
        System.out.println((int)-45.89f);   //-45
        //取整

        System.out.println("========");
        char c = 'a';
        int d = c + 1;
        System.out.println(d);
        System.out.println((char)d);
        
    }
}

public class Demo06 {
    public static void main(String[] args) {
        //操作比较大的数的时候,注意溢出问题
        //JDK7新特性,数字之间可以用下划线分割
        int money = 10_0000_0000;
        int years = 20;
        int total = money * years;
        System.out.println(total);  //-1474836480,计算的时候溢出了
        long total2 = money * years; //默认是int,int不能超过正负20亿,转换之前已经存在问题了(先计算后转换)
        System.out.println(total2);
        long total3 = money * ((long)years);    //先把一个数转换为long,再计算
        System.out.println(total3);
        //L和l,尽量写L
    }
}

六、变量、常量、作用域

1. 变量

  • Java是一种强类型语言,每个变量都必须声明其类型
  • Java变量是程序中最基本的存储单元,其要素包括变量名,变量类型和作用域
    数据类型 变量名 = 值
    (不建议一行定义多个值)
  • 注意事项
    • 每个变量都有类型,基本类型或引用类型
    • 变量名必须是合法的标识符
    • 变量声明是一条完整的语句,必须以分号结束
public class Demo07 {
    public static void main(String[] args) {
        //int a,b,c;    //不建议一行定义多个变量
       // int a=1,b=2,c=3;  //程序可读性
        String name = "qiaqia";
        char x = 'X';
        double pi = 3.14;
    }
}

2.变量作用域

  • 类变量
  • 实例变量
  • 局部变量
public class Demo08 {

    //类变量  static
    static double salary = 2500;

    //属性:变量

    //实例变量:从属于对象,如果不自行初始化,则为这个类型的默认值  0 0.0
    //布尔值:默认是false
    //除了基本类型,其余的都是null
    String name;
    int age;

    //main方法
    public static void main(String[] args) {

        //局部变量:必须声明和初始化值
        int i = 10;
        System.out.println(i);

        //变量类型 变量名字 = new 变量类型();
        Demo08 demo08 = new Demo08();
        System.out.println(demo08.age);
        System.out.println(demo08.name);

        //类变量  static
        System.out.println(salary);

    }

    //其他方法
    public void add(){

    }

}

3.常量

  • 特殊变量,初始化后不能在改变值
public class Demo09 {

    //修饰符,不存在先后顺序
    static final double PI = 3.14;

    public static void main(String[] args) {
        System.out.println(PI);
    }
}

4.变量的命名规范

  • 所有变量、方法、类名:见名知意
  • 类成员变量:首字母小写和驼峰原则
  • 局部变量:首字母小写和驼峰原则
  • 常量:全部大写字母和下划线
  • 类名:首字母大写和驼峰原则
  • 方法名:首字母小写和驼峰原则

七、运算符

  • 算术运算符:+、-、*、/、%、++、–
  • 赋值运算符:=
  • 关系运算符:>、<、>=、<=、==、!=、instanceof
  • 逻辑运算符:&&、||、!(逻辑与或非)
  • 位运算符:&、|、^、~、>>、<<、>>>
  • 条件运算符:? :(三目运算符)
  • 扩展赋值运算符:+=、-=、*=、/=
public class Demo01 {
    public static void main(String[] args) {
        //二元运算符
        //Ctrl+D:复制当前行到下一行
        int a = 10;
        int b = 20;
        int c = 25;
        int d = 25;
        System.out.println(a+b);
        System.out.println(a-b);
        System.out.println(a*b);
        System.out.println(a/b);    //应为0.5,显示0
        System.out.println(a/(double)b);    //0.5
    }

}
public class Demo02 {
    public static void main(String[] args) {
        long a = 123123123123123L;
        int b = 123;
        short c = 10;
        byte d = 8;
        System.out.println(a+b+c+d);    //Long
        System.out.println(b+c+d);  //Int
        System.out.println(c+d);    //Int
        //System.out.println((String)c+d);    //此处对String按F2显示为short型,因为是对short型的c进行强转
        //System.out.println((String)(c+d));  //此处是对(c+d)的结果进行强转
        //运算操作时有Long型则结果为Long型,没遇到Long型,则结果是int型

    }
}
public class Demo03 {
    public static void main(String[] args) {
        //关系运算符返回的结果:正确、错误  布尔值
        int a = 10;
        int b = 20;
        System.out.println(a>b);
        System.out.println(a<b);
        System.out.println(a==b);
        System.out.println(a!=b);
        
        //取余,模运算
        int c = 21;
        System.out.println(c%a);
    }
}

八、自增自减运算符、初识Math类

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

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

        //a = a + 1;
        int c = ++a;    //执行这行代码前,先自增,再给b赋值
        System.out.println(a);  //5
        System.out.println(b);  //3
        System.out.println(c);  //5

        //幂运算 2^3=8 2*2*2=8  很多运算可以通过使用工具类来操作
        double pow = Math.pow(2,3);
        System.out.println(pow);
    }
}

九、逻辑运算符、位运算符

public class Demo05 {
    public static void main(String[] args) {
        //逻辑运算符
        //与(and)  或(or)  非(取反)
        boolean a = true;
        boolean b = false;
        System.out.println(a && b); //逻辑与:真真为真
        System.out.println(a || b); //逻辑或:一真为真
        System.out.println(!(a && b));  //逻辑非:取反

        //短路运算
        int c = 5;
        boolean d = (c<4)&&(c++<4); //c<4为假,逻辑与运算真真为真,即一假则假,所以该式子必为假,则短路不继续进行后续的自增运算
        System.out.println(d);
        System.out.println(c);  //c=5,c没有自增
        
    }
}

public class Demo06 {
    public static void main(String[] args) {
        //位运算
        /*
        A = 0011 1100
        B = 0000 1101
        --------------------
        A&B = 0000 1100     //与:一假为假
        A|B = 0011 1101     //或:一真为真
        A^B = 0011 0001     //异或:同0异1
        ~B = 1111 0010      //取反

        2*8 = 16  2*2*2*2
        效率极高
        <<  *2  左移
        >>  /2  右移

        0000 0000   0   pow
        0000 0001   1   2^0
        0000 0010   2   2^1
        0000 0100   4   2^2
        0000 1000   8   2^3
        0001 0000   16  2*4

         */
        
        System.out.println(2<<3);
    }
}

十、三元运算符及小结

public class Demo07 {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        a+=b;
        System.out.println(a);
        a-=b;
        System.out.println(a);

        //字符串连接符  +  , String
        System.out.println(""+a+b); //1020  //+的执行顺序是从左到右,先空字符串,后面的是执行连接操作
        System.out.println(a+b+""); //30    //后空字符串,就会先执行相加操作

    }
}
public class Demo08 {
    public static void main(String[] args) {
        // x ? y : z
        //x==true,则 y,否则 z

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

    }
}

运算符优先级

十一、包机制

  • 用于区别类名的命名空间
  • 包的本质就是文件夹
  • 语法:
package pkg[.pkg2[.pkg3...]];
  • 一般利用公司域名倒置作为包名;eg. com.baidu.www
  • 导包;import

十二、JavaDoc生成文档

  • JavaDoc命令是用来声称自己的API文档的
  • 参数信息
    • @author 作者名
    • @version 版本号
    • @since 指明需要最早使用的jdk版本
    • @param 参数名
    • @return 返回值情况
    • @throws 异常抛出情况

尝试:
右键show in explorer
文件管理器地址栏cmd
javadoc -encoding UTF-8 -charset UTF-8 xxx.java
index.html


总结

即使再小的帆也能远航

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值