JavaSE02

Java基础语法

变量

image-20240901201140372

image-20240901201655788

变量作用域

分为类变量、实例变量、局部变量

常量

image-20240901202845949

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

    //final代表常量,修饰符不存在先后顺序
    //常量名一般使用大写字符
    static final double PI = 3.14;

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

    //main方法
    public static void main(String[] args) {
        //int a=1,b=2,c=3;  //不建议,注意程序可读性
        String name = "ac";
        char x = 'X';
        double pi = 3.14;

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

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

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

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

    }
}

变量的命名规范

image-20240901204118908

运算符

优先级:括号级别最高,一元 (自增自减)> 二元(算术) > 位 > 关系 > 逻辑 > 三元 (条件)> 赋值
Java语言支持如下运算符

image-20240902232521480

public class Demo01 {
    public static void main(String[] args) {
        //二元运算符
        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);

        long e = 12312312312312312L;
        int f = 121;
        short g = 10;
        byte h = 8;
        System.out.println(e+f+g+h);    //Long
        System.out.println(f+g+h);  //Int
        System.out.println(g+h);    //Int

        //关系运算符返回的结果:   正确,错误   布尔值
        //会和if一起使用
        System.out.println(a>b);
        System.out.println(a<b);
        System.out.println(a==b);
        System.out.println(a!=b);

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

public class Demo02 {
    public static void main(String[] args) {
        //++    --  自增,自减   一元运算符
        int a = 3;
        int b = a++;    //执行完这行代码,先给b赋值,再自增
        //a = a + 1
        System.out.println(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(2, 3);
        System.out.println(pow);
    }
}

public class Demo03 {
    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++<4);
        System.out.println(d);
        System.out.println(c);

        //位运算符
        /*
        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);
    }
}

public class Demo04 {
    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+"");

        //三元运算符
        // x ? y : z
        //如果x==true,则结果为y,否则结果为z
        int score = 50;
        //if
        String type = score < 60 ? "不及格":"及格";  //必须掌握
        System.out.println(type);
    }
}

//导入这个包下所有的类!
import com.achi.base.*;

JavaDoc生成文档

package com.achi.base;

/**
 * @Description JacaDoc生成文档
 * @Author achi
 * @Create 2024-09-03-16:12
 * @version 1.0
 * @since 1.8
 */
public class Doc {
    String name;

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

    //通过命令行生成文档:javadoc 参数  Java文件
    //作业:学会查找使用IDEA生成JavaDoc文档!     面向百度编程!
    //基础部分的一切知识,后面几乎每天都会用
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值