java

1.1运行机制

image-20230603165124233

1.2数据类型()

1.2.1强类型语言

  • 要求变量的使用严格符合规定,所以变量都必须先定义后才能使用

弱类型语言

与强类型相反

1.2.2Java数据类型分为两大类

  1. 基本类型(primitive type)
  2. 引用类型(reference type)

1.2.3基本数据类型(primitive type)

  • 数值类型

    1. 整数类型

    byte占1个字节:-128-127、

    short占2个字节:-32768-32767、

    int4字节:-21亿 - 21亿-1、

    long8字节:-231-231-1

    1. 浮点类型

    float:4字节、

    double:8字节、

    1. 字符类型(char2字节)
  • boolean类型:占1位只有true****和false

引用数据类型:类、接口、数组

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

        //整数
        int num1 = 10; //最常用
        byte num2 = 20;
        short num3 = 30;
        long num4 = 30L;

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

        //字符
        char name = 'A';
        //字符串
        //String name1 = "中国";

        //布尔值
        boolean flag = true;

    }
}
public class Demo04 {
    public static void main(String[] args) {
        int i = 128;
        byte b = (byte) i;  //内存溢出
        //强制转换  (类型)变量名 高--->低
        //自动转换      低---高
        int a = 128;
        double c = a;
        System.out.println(c);
        System.out.println(b);

        /*
        注意点:
             1. 不能对布尔值进行转换
             2. 不能把对象类型转换为不相干的类型
             3. 高容量转换到低容量的时候,强制转换
             4. 转换的时候可能存在内存溢出,或者精度问题
         */
        System.out.println("===============");
        char c1 = 'a';
        int d = c1 + 1;
        System.out.println(d);
        System.out.println((char)d);
    }
}

image-20230604152754191

1.3什么是字节

  • 位(bit):计算机内部数据储存最小单位,11001100八位数二进制数
  • 字节(byte):数据处理基本单位i,习惯用大写B表示
  • 字符:计算机使用字母、数字、字和符号
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);
        System.out.println(i2);
        System.out.println(i3);


        //浮点数拓展 有限 离散 舍入误差 大约 接近但不等于
        //BigDecimal 数学工具类

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

        float d1 = 12348941567890357f;
        float d2 = d1 + 1;
        System.out.println(d1 == d2);

        //字符拓展
        char a = 'a';
        char b = '中';
        System.out.println(a);
        System.out.println((int) a); //强制转换

        System.out.println((int) b); //强制转换
        System.out.println(b);
        //所有字符本质还是数字 编码 Unicode 表:97 = a 2字节 0 - 65536
        //转义字符 \t制表符 \n换行

        //
        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);
        //对象 从内存分析

        //布尔值拓展
        boolean flag = true;

        if (flag == true) { }//新手
        if (flag){ } //老手

    }
}

image-20230604154250984

1.4变量的定义

public class Demo08 {

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

    //属性:变量

    //实例变量:从属于对象(base.Demo08)
    //除了基本类型,其余的默认值都是null
    String name;
    int age;

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

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

        Demo08 demo08 = new Demo08();
        System.out.println(demo08.age);
        System.out.println(demo08.name);

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

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

    }
}

image-20230604163036105

public class Demo09 {

    //static、final 修饰符不存在先后顺序
    //final 常量
    public static final double PI = 3.14;

    public static void main(String[] args) {

        System.out.println(PI);
    }
}

image-20230604165423794

2.1基本运算符

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);
        
    }
}
public class Demo03 {
    public static void main(String[] args) {

        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);
    }
}

2.2自增自减运算符

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

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

2.3逻辑运算符、位运算符

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

        System.out.println("a&&b" + (a && b));
        System.out.println("a||b" + (a || b));
        System.out.println("!(a&&b)" + (!(a && b)));

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

    }
}
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
        >> /2f
         */
        System.out.println(2<<3);
        System.out.println();
    }
}

2.4三目运算符

	public class Demo08 {
    public static void main(String[] args) {
        // x ? y : z
        //如果x等于true,则结果为y,否则为z.
        int score = 80;
        String s = score < 60 ? "不及格" : "及格";//
        System.out.println(s);
    }
}
posted @ 2023-06-05 17:49  山上_的树  阅读(7)  评论(0)    收藏  举报