java基础知识

java注释

  1. 单行注释 // 单行注释
  2. 多行注释 /* 多行注释 */
  3. 文档注释/**
    1. ​ *文档注释
    2. ​ */

java标识符

1597642500733

1597642897617

java数据类型

是一种强类型语言,所有变量都必须定义后才能使用

数据类型分为2类

### 基础类型(8大基础类型)

1. 数值类型
 1. 整数类型 int byte short long
 2. 浮点类型  float double    
 3. 字符类型  char 字符    
2. bool类型  boolean

引用类型

1. 类
2. 接口
3. 数组

注意点

  1. (最好不使用浮点数进行比较)

  2. (强制转换,可以将字符变成数字 (int))

  3. 转义字符 \t 制表符 \n换行

    public class Demo1 {
        public static void main(String[] args) {
            String s1 = new String("Hello world");
            String s2 = new String("Hello world");
            System.out.println(s1==s2); // false
            String s3 = "Hello world";
            String s4 = "Hello world";
            System.out.println(s3==s4); //true
        }
    }
    
  4. char 只能是一个字符 String 是一串字符

类型转换

public class Demo2 {
    public static void main(String[] args) {
        int i = 128;

        byte b = (byte) i;  // 注意内存溢出
        // 强制转换 (类型)变量名   高 > 低

        double d = i;
        // 自动转换             低 > 高
        char c = 'a';
        int a = c+1;
        System.out.println(a); // 98
        System.out.println((char)a);  // b
        /*
        由低到高排序
        byte,short,char -->int --> long --> float --> double
        注意点:
        1 不能对布尔值转换
        2 不能吧对象转换成不相干的类型
        3 在吧高容量转换到低容量的时候,强制转换
        4 转换的时候可能存在内存溢出,或者精度问题
        */
    }
}

溢出问题

public class Demo3 {
    public static void main(String[] args) {
        // 操作比较大的数,注意溢出问题
        int money = 10_0000_0000;
        int year = 20;
        int total = money * year;
        long total2 = money * year;
        System.out.println(total); // -1474836480溢出了
        System.out.println(total2); // 默认是int,转换之前已经存在问题了

        long total3 = money * ((long)year); // 向将一个数转换成long
        System.out.println(total3);  // 20000000000
    }
}

变量

public class Demo4 {
    static int l1 = 0;  // 类变量

    String str = "hell0";  // 实例变量  从属于对象的
    int i; // 实例变量,如果没有初始值,默认值为0, 其他都是null
    boolean flag; // bool值默认是false

    //其他方法
    public void add(){
        System.out.println(l1);
    }
    // 主程序
    public static void main(String[] args) {
        int i = 0;  // 局部变量 必须声明和初始化值
        System.out.println(l1);

        // 变量类型 变量名字 = new Demo4();
        Demo4 demo4 = new Demo4();
        demo4.add();
        System.out.println(demo4.i);

    }
}

1597651645642

常量

初始化就不能改变的值,不会变的值

所谓常量可以理解成一种特殊的变量,他的值被设定后,在程序运行过程中不允许改变

final 常量名 = 值;
final double PL = 3.14;

长量名一般用大写表示

基本运算符

1597651910154

public class Demo2 {

   public static void main(String[] args) {
       // ++ -- 自增自减
       int a = 3;
   	
       // a++   先复制,在++  这么记忆
       int b = a++; // 执行完这行代码后,先给b赋值,在自增
       // a = a+1

       // a = a+1
       int c = ++a; // 执行完这行代码前,先自增,在给b赋值,

       System.out.println(a);  // 5
       System.out.println(b);  // 3
       System.out.println(c);  // 5
   }
}
public class Demo3 {
    public static void main(String[] args) {
        boolean a = true;
        boolean b = false;
        System.out.println("与"+(a && b));
        System.out.println("或"+(a || b));
        System.out.println("非"+!(a));

        // 短路运算
        int c = 5;
        boolean d = (c<4) && (c++ < 4);
        System.out.println(d);  // 第一步就是错的了,不会看后面的
        System.out.println(c); // 5 不会运算后面的了
    }
}

位运算

/*
a = 0011 1100
b = 0000 1101
------------
a&b 0000 1100
a|b 0011 1101
a^b 0011 0001
~b  1111 0010

<<  左移  == 乘以2
>>	右移  == 除以2
*/

三元运算

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

包机制

1597656083172

javaDoc

package base;

/**
 * @author 作者号
 * @version 版本号
 * @since 指定使用的jdk版本
 * @param 参数名
 * @return 返回值情况
 * @throws 异常抛出情况
 */
public class Demo1 {
    String name;
    public static void main(String[] args) {
        System.out.println(111111111);
    }
}

javadoc -encoding UTF-8 -charset UTF-8 Demo1.java

posted @ 2020-08-17 18:04  李淳罡zZ  阅读(132)  评论(0)    收藏  举报