01-Java基础语法

注释

Java中的注释有三种

  • 单行注释

    //单行注释
    
  • 多行注释

    /*我
    是多行注释
    */
    
    
    
  • 文档注释

    /**
    ds 
    dsf
    */
    

标识符

关键字

Java所有的组成部分都需要名字。类名、变量名以及方法名都被称为标识符
在这里插入图片描述

标识符注意点

在这里插入图片描述

在这里插入图片描述

数据类型

在这里插入图片描述

package com.company;

public class Demo02 {
    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.1415926;

        //字符
        char name ='A';
        //字符串
        String namea = "五五";

        //布尔值:是非
        boolean flag = true;
    }
}

类型转换

在这里插入图片描述

  • 转换示例
package com.company;

public class Demo05 {
    public static void main(String[] args) {
        int i = 128;
        byte b =(byte)i;//强制转换  高到低

        double c = i;//自动转换  低到高

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

        //精度问题
        System.out.println((int)23.7);  //结果为23
        System.out.println((int)-45.89f); //结果为-45

        char C ='a';
        int d = C+1;
        System.out.println(d);//输出结果为98
    }
}

  • 溢出
package com.company;

public class Demo06 {
    public static void main(String[] args) {
        //操作比较大的数的时候,注意溢出问题
        //jdk7新特征,数字间可以用下划线分割
        int money = 10_0000_0000;
        int years = 20;
        int total = money*years;  //默认是int
        System.out.println(total);//输出结果为复数  -147....  计算的时候溢出了

        long total12 = money*years;  //默认是int,转换之前就已经溢出了

        //正确操作
        long total13 = money*((long)years); //先把一个数转换为Long
    }
}

变量

在这里插入图片描述

变量作用域

  • 类变量
  • 实例变量
  • 局部变量
public class Variable{
    static int allClicks=0;  //类变量
    String str ="HelloWorld";//实例变量
    
    public void method(){
        int i =0; //局部变量
    }
}
package com.company;

public class Demo08 {
    //类变量
    static double salary =27000;

    //实例变量:从属于对象
    String name;
    int age;

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

        //局部变量;必须声明和初始化值
        int i=10;

        //变量类型  变量名字 = new Demo08();
        Demo08 demo08 = new Demo08();
    }

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

    }
}

常量

在这里插入图片描述

运算符

在这里插入图片描述

基本运算符

package operator;

public class Demo01 {
    public static void main(String[] args) {
        //二元运算符
        //Ctrl + D :复制当前行到下一行
        int a= 10;
        int b= 20;
        int c= 30;
        int d= 40;
        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 Demo01 {
    public static void main(String[] args) {
        //Ctrl + D :复制当前行到下一行
        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);
    }
}

自增自减运算符

package operator;

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

        //幂运算2^3
        double pow = Math.pow(2,3);
    }
}

逻辑运算符

package operator;

public class Demo05 {
    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
    }
}

位运算符

package operator;

public class Demo06 {
    public static void main(String[] args) {
        /*
        A = 0011 1100
        B = 0000 1101

        A&B = 0000 1111
        A|B = 0011 1101
        A^B = 0011 0001
        ~B  = 1111 0010
        * */

    }
}

条件运算符

?:

package operator;

//三元运算符
public class Demo07 {
    public static void main(String[] args) {
        // x ? Y : Z   如果x的值为true,则结果为y,否则为z
        int score = 80;
        String type = score<60? "不及格" :"及格";
        System.out.println(type);
    }
}

包机制

在这里插入图片描述


  • 在这里插入图片描述

JavaDoc

在这里插入图片描述

package base;
/**
 *@author Purvis
 *@version 1.0
 *@since 1.8
 * */
public class Doc {
    String name;
    // /**回车 快捷键
    /**
     *
     * @param name
     * @return
     */
    public String test(String name){
        return name;
    }
}

JavaDoc编译成文档

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
文档
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值