狂神说Java|Java基础

Java基础

01 注释

  1. 单行注释
  2. 多行注释
  3. 文档注释
/**
* @Description HelloWorld
* @Authur xinxin
**/
public class Main {
    public static void main(String[] args) {
        //单行注释
        for (int i = 1; i <= 5; i++) {
            System.out.println("i = " + i);
        }
        /*
        多行注释长这样
        这行也是注释
        */     
    }
}

02 标识符和关键字

img
img

03 数据类型讲解

img
强类型语言安全性高,但速度会比较慢
img

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

        //整数
        int num1 = 10; //最常用
        byte num2 = 20;
        short num3 = 30;
        long num4 = 30L; //Long类型要在数字后面加L

        //小数: 浮点数
        float num5 = 50.1F;
        double num6 = 3.141592653589;

        // 字符
        char name = 'A';
        // 字符串, String不是关键字 是一个类
        //String namea = "你好";

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

img

32位的操作系统只可以用32位的cpu,而64位的系统可以装32 or 64的cpu.
(在内存中的)寻址能力与32位还是64位直接挂钩。
64位最多,最多可以支持128GB的内存。
32位 至多支持4GB内存

04 数据类型扩展及面试讲解

public class Main {
    public static void main(String[] args) {
    //整数扩展: 进制       二进制0b   十进制   八进制0   十六进制0x

    int i = 10;
    int i2 = 010; //0开头代表八进制
    int i3 = 0x10;  //十六进制0x 0~9 A~F
    //八进制的10相当于8,十六进制的10相当于16
    System.out.println(i);  // 10
    System.out.println(i2);  // 8
    System.out.println(i3);  // 16
    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);  //0.1
    System.out.println(d);  //0.1

    float d1 = 23131312312312313f;
    float d2 = d1+1;
    System.out.println(d1==d2);  //true

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

    System.out.println(c2);  //中
    System.out.println((int)c2);  //20013

    //所有的字符本质还是数字
    //编码 Unicode  2字节 表:(97 = a  65=A)

    //U0000 UFFFF

    char c3 = '\u0061'; 
    System.out.println(c3); //a

    //转义字符
    // \t 制表符
    // \n 换行

    System.out.println(“Hello\tWorld”);  //Hello World
    //=================================================
    String sa = new String("Hello world");
    String sb = new String("Hello world"); 
    System.out.println(sa==sb); //false

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

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

    if (flag == true){} //新手
    if (flag){} //老手
    //代码要精简易读
    }
}

05 类型转换

img

public class Main {
    public static void main(String[] args) {
        int i = 128;
        byte b = (byte)i; 
        //byte的最大值是127,赋值128不可以
        //内存溢出

        //转换: 指右转左
        //强制转换  (类型)变量名  高--低
        //自动转换   低--高  

        System.out.println(i); //128
        System.out.println(b); //-128

        /*
        注意点:
        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); //98
        System.out.println((char)d); //b
    }
}
public class Main {
    public static void main(String[] args) {
        //操作比较大的数的时候,注意溢出问题
        //JDK7新特性,数字之间可以用下划线分割
        int money = 10_0000_0000;
        System.out.println(money); //10000000000
        int years = 20;
        int total = money * years;
        System.out.println(total);  //-1474836480, 计算的时候溢出了
        long total2 = money * years;
        System.out.println(total2);  //-1474836480
        //原因在于,money*years是int, 计算完后才把结果转换为long类型
        //转换前已经存在问题了
        //正确操作:
        long total3 = money * ((long)years);
        System.out.println(total3);  //20000000000
    }
}

06 变量、常量、作用域

img

public class Main {
    public static void main(String[] args) {
        //int a,b,c;
        //int a=1,b=2,c=3;
        //虽然可以在一行声明多个变量,但不提倡那种风格
        //出于程序可读性,不要那样做
        int a = 1;
        int b = 2;
        int c = 3;

        String name = "xinxin";
        char x = "X";
        double pi = 3.14;
    }
}

img

public class Demo08 {

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

    //类里面除了方法,还可以定义一些属性
    //属性:变量

    //实例变量:从属于对象。要通过类才能使用它
    //布尔值:默认是false
    //除了基本类型,其余的默认值都是null
    String name;
    int age;

    //main方法 方法在类里面
    public static void main(String[] args) {

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

        //实例变量需要把类先写出来
        //变量类型 变量名字= new Demo08();
        Demo08 demo08 = new Demo08();
        System.out.println(demo08.age); //0
        //虽并未初始化age,还是有输出0
        //实例变量如果不进行初始化,会变成这个类型的默认值
        System.out.println(demo08.name); //null

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

    //其他方法
    public void add(){
        System.out.println(i);
    }
}

img

public class Demo09 {
    //static final double PI = 3.14;
    final static double PI = 3.14;
    //两种写法都可以
    //修饰符不存在先后顺序  修饰符: static final public..

    public static void main(String[] args) {
        System.out.println(PI);
    }
}

img
驼峰原则:除了第一个单词以外,后面的单词首字母大写
lastname->lastName

07 基本运算符

img

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); //30
        System.out.println(a-b); //-10
        System.out.println(a*b); //200
        System.out.println(a/b); //0  //0.5,四舍五入成0了

        System.out.println(a/(double)b); //0.5
    }
}
public class Demo02 {
    public static void main(String[] args) {
       long a = 123123123123123L;
       int b = 123;
       short c = 10;
       byte d = 8;

       //有一个数为long,结果数为long
       System.out.println(a+b+c+d);  //123123123123264 //Long

       //有一个数为double,那结果就为double
       //如果没有long,结果都为Int
       System.out.println(b+c+d);  //141 //Int
       System.out.println(c+d);  //18 //Int
    }
}
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);   //false
        System.out.println(a < b);   //true
        System.out.println(a == b);  //false
        System.out.println(a != b);  //true
    }
}

08 自增自减运算符、初识Math类

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

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

        System.out.println(b);  //3
        System.out.println(c);   //5

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

    }
}

09 逻辑运算符、位运算符

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 || n));  //逻辑与运算:两个变量有一个为真,结果才为true
        System.out.println("!(a && b): "+ !(a && b));  //如果是真,则为假,如果是假则变为真


        //短路运算
        //逻辑与运算  b && a  b为假,那么不会再去计算后面的了
        int c = 5;
        boolean d = (c<4) && ((c++)<4);

        System.out.println(d);   //false
        System.out.println(c);   //5  说明出现短路,&&号后面没执行
    }
}
public class Demo06 {
    public static void main(String[] args) {
        /*
        A = 0011 1100
        B = 0000 1101

        A&B = 0000 1100  上下两个比较,2个都为1才为1,否则就是0 
        A|B = 0011 1101  对应位都是0,结果为0,否则为1
        A^B = 0011 0001  两个位置相同则为0,否则为1
        ~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);   //16

    }
}

10 三元运算符及小结

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); //10

        //字符串连接符 + 在+两侧一旦一侧出现了字符串类型,那么
        //它就会把另外一侧的操作数都转化为String类型再进行连接
        System.out.println(a+b); //30
        System.out.println(""+a+b); //1020
        //""在+前,会拼接
        System.out.println(a+b+""); //30
        //“”在后,会先进行运算
    }
}

package com.kuang.operator;

//导入这个包下所有的类
import com.kuang.base.*;
//*是通配符,会把这个包的所有东西都导入进来

public class Demo08{
    public static void main(String[] args){
        // x ? y : z
        //如果x==true,则结果为y,否则结果为z

        int score = 80;
        String type = score < 60 ? "不及格":"及格";

        System.out.println(type); //不及格

    }
}

11 包机制

包的本质就是一个文件夹

12 JavaDoc生成文档

package com.kuang.base;

/**
 * @author name
 * @version 1.0
 * @since 1.8
 */
public class Doc{
    String name;
    /**
     * @author Kuangshen
     * @param name
     * @return
     * @throws Exception
     */
    public String test(String name) throws Exception{
        return name;
    }
}
posted @ 2025-07-29 01:34  ForeverEver333  阅读(14)  评论(0)    收藏  举报