java基础语法


注释、标识符、关键字

注释

  • 注释不会被执行,只是给写代码的人看
  • 书写注释是个非常好的习惯
  • java的三种注释
    1. 单行注释 //
    2. 多行注释 /**/
    3. 文档注释 /** */
  • 平时学习写代码一定要规范
  • 点击传送门->有趣的代码注释

标识符

  • 关键字
    关键字
  • Java所有的组成部分都需要名字。类名、变量名以及方法名都被称为标识符。
  • 标识符是大小写敏感的
public class Demo01 {
    public static void main(String[] args) {
        String AHello = "liujiale";
        String aHello = "liujiale";
        String $Hello = "liujiale";
        String _Hello = "liujiale";
        String _1 = "liujiale";
        
        //非法标识符
        //String class = "liujiale";
        //String 6Hello = "liujiale";
        //String #Hello = "liujiale";
        //String *Hello = "liujiale";

        String Man = "liujiale";
        String man = "liujiale";

        String 王者荣耀 = "百❤王者";
        System.out.println(王者荣耀);
    }
}


数据类型

  • 强类型语言
    • 要求变量的使用要 严格 符合规定,所有变量都必须先定义后才能使用
  • 弱类型语言
    • 要求变量的使用要符合规定
public class Demo02 {
    public static void main(String[] args) {
        String A = "hello";
        int num=12 ;
        System.out.println(A);
        System.out.println(num);
    }
}

  • Java的数据类型分为两大类
    1. 基本类型(primitive type)
    2. 引用类型 (reference type)
      在这里插入图片描述
//基本类型
public class Demo02 {
    public static void main(String[] args) {
        //八大基本数据类型

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

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

        //字符
        char name= '国';
        //字符串,String不是关键字,类
        String name1 ="刘佳乐gaoruican";

        //布尔值
        boolean flag =true;
        boolean flag2=false;
    }
}


什么是字节

  • 位(bit):是计算机 内部数据 储存的最小单位,11001100是一个八位二进制数。
  • 字节(byte):是计算机 数据处理 的基本单位,习惯上用大写B来表示,
  • 1B(byte,字节)=8bit(位)
  • 字符:是指计算机中使用的字母、数字、字和符号
    - 1bit表示1位
    - 1Byte表示一个字节1B=8b
    - 1024B=1KB
    - 1024KB=1M
    - 1024M=G

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

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

        System.out.println(i);
        System.out.println(i2);
        System.out.println(i3);
        System.out.println("==============================================");

        //============================================
        //浮点数拓展?        银行业务怎么表示?  钱
        //Bigdecimal 数学工具类
        //============================================
        //float     有限 离散 舍入误差 大约 接近但不等于
        //double
        //最好完全避免使用浮点数进行比较
        //最好完全避免使用浮点数进行比较
        //最好完全避免使用浮点数进行比较

        float f =0.1f;//0.1
        double d=1.0/10;//0.1

        System.out.println(f==d); //false
        System.out.println(f);
        System.out.println(d);

        float d1=211313131313131313f;
        float d2=d1+1;

        System.out.println(d1==d2);//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 表:(97=a 65=A) 2字节 0-65536 Excel      2 16 =65536

        //U0000 UFFFF

        char c3='\u0061';

        System.out.println(c3);//a

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

        System.out.println("Hello\nWorld");

        System.out.println("==============================================");
        String sa = new String("Hello World");
        String sb = new String("Hello World");
        System.out.println(sa==sb);

        String sc = "Hello World";
        String sd = "Hello World";
        System.out.println(sc==sd);
        //对象 从内存分析

        //============================================
        //布尔值拓展
        //============================================
        System.out.println("==============================================");
        
        boolean flag = true;
        if (flag==true){ }      //新手
        if (flag){}     //老手
        //Less is More 代码要精简易读

    }
}


类型转换

  • 由于java是强类型语言,所以要进行有些运算的时候时,需要用到类型转换。
    在这里插入图片描述
  • 运算中,不同类型的数据要先转化为同一类型,然后在进行运算。
public class Demo05 {
    public static void main(String[] args) {
        //强制转换      (类型)变量名     高-->低
        int i = 128;
        byte b = (byte)i;   //内存溢出

        System.out.println(i);
        System.out.println(b);

        //自动转换      低-->高
        int i1 =128;
        double b1= i1;

        System.out.println(i1);
        System.out.println(b1);

        /*
        注意点
        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;    //-1474836480,计算的时候溢出了
        long total2 =money*years;   //默认是int,转换之间已经存在问题了?

        long total3 = money*((long)years);//先把个人数转换为long
        System.out.println(total3);
        
        //L i

    }
}


变量

  • 变量是什么:就是可以变化的量
  • java是一种强类型语言,每一个变量都必须声明其类型
  • java变量是程序中最基本的存储单元,其要素包括变量名,变量类型和作用域
    在这里插入图片描述
  • 注意事项
    • 每一个变量都有类型,类型可以是基本类型,也可以是引用类型。
    • 变量名必须是合法的标识符。
    • 变量声明式一条完整的语句,因此每一个声明都必须以分号结束
public class Demo07 {
    public static void main(String[] args) {
        //int a,b,c;
        //int a=1,b=2,c=3;
        //程序可读性
        String name = "liujiale";
        char x ='X';
        double pi = 3.14;
    }
}

变量作用域

  • 类变量
  • 实例变量
  • 局部变量
    作用域
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 demo08 = new Demo08();
        System.out.println(demo08.age);
        System.out.println(demo08.name);

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


    }

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

    }

}

变量的命名规范

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

常量

  • 常量(Constant):初始化(initialize)后不能在改变值!不会变动的值
  • 所谓常量可以理解成一种特殊的变量,他的值被设定后,在程序运行过程中不允许被改变。

常量

  • 常量名一般用大写字符。
public class Demo09 {

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

    public static void main(String[] args) {

        System.out.println(PI);
    }
}


运算符

  • java语言支持如下运算符
    在这里插入图片描述
package operator;

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/(double)b);
    }
}

package operator;

public class Demo02 {
    public static void main(String[] args) {
        long a=123123123123123L;
        int b= 123;
        short c= 10;
        byte d= 8;

        //Cast:转换
        //运算中会自动转换为运算类型最高的类型
        System.out.println(a+b+c+d);    //Long
        System.out.println(b+c+d);      //Int
        System.out.println(c+d);        //Int
    }
}

package operator;

public class Demo03 {
    public static void main(String[] args) {
        //关系运算符返回的结果:   正确,错误   布尔值
        //if

        int a= 10;
        int b= 20;
        int c= 21;

        //取余,模运算
        System.out.println(c%a);//  c/a     21/10 = 2 ... 1

        System.out.println(a>b);
        System.out.println(a<b);
        System.out.println(a==b);
        System.out.println(a!=b);
    }
}

package operator;

public class Demo04 {
    public static void main(String[] args) {
        // ++   --      自增,自减       一元运算符
        /*
        a++先使用后加
        ++a先加后使用
         */
        int a= 3;
        int b=a++;      //执行完这行代码后,先给b赋值,再自增
        //a++   a=a+1;
        System.out.println(a);
        //++a   a=a+1;
        int c=++a;     // 执行完这行代码前,先自增,再给c赋值

        System.out.println(a);
        System.out.println(b);
        System.out.println(c);

        //幂运算 2^3   2*2*2=8
        //很多运算我们会使用一些工具类来操作!
        double pow = Math.pow(3, 2);
        System.out.println(pow);
    }
}

package operator;

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

        System.out.println("a&&b:"+(a&&b));     //逻辑与运算:两个变量都为真,结果才为true
        System.out.println("a||b:"+(a||b));     //逻辑或运算:两个变量有一个为真,结果就为true
        System.out.println("!(a&&b):"+!(a&&b)); //如果是真,这变为假,如果是假则变为真

        //短路运算
        int c= 5;
        boolean d= (c<4)&&(c++<10);

        System.out.println(d);
        System.out.println(c);
    }
}

package operator;

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
        ~B  = 1111 0010

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

        0000 0000       0
        0000 0001       1
        0000 0010       2
        0000 0011       3
        0000 0100       4
        0000 1000       8
        0001 0000       16



         */

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

package operator;

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

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

        System.out.println(a);

        //字符串连接符    +   ,String
        //  +是自左向右的
        System.out.println(""+a+b);
        System.out.println(a+b+"");
    }
}

package operator;

//三元运算符
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);
    }
}

运算符的优先级

java运算符的优先级
优先级()


包机制

  • 为了更好的组织类,java提供了包机制,用于区别类名的命名空间。
  • 包语句的语法格式为:
    包语句语法
  • 一般利用公司域名倒置作为包名
  • eg: www.baidu.com com.baidu.www baike
  • 为了能够使用某一个包的成员,我们需要在java程序中明确导入该包。使用“import”语句可完成此功能
    在这里插入图片描述
  • 包的本质就是文件夹

JavaDos

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

点击前往->jkd8帮助文档

package com.leo.base;

/**
 * @author leo
 * @version 1.0
 * @since 1.8
 */

public class Doc {
    
    String name;

    /**
     * @author leo
     * @param name
     * @return
     * @throws Exception
     */
    public String test(String name) throws Exception{
        return name;
    }
    
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

• 你把你闪现给我交了•

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值