Java基础

发布时间 2023-11-07 16:06:26作者: 无心的飞飞

Java基础语法

1.注释

注释不会执行,给写代码的人看

三种注释:

单行注释: //

多行注释:/**/

文档注释:/** */


public class Helloworld {
    public static void main(String[] args) {
        //单行注释
        //控制台输出一个Hello,world
        System.out.println("Hello,World");
        
        /*
        多行注释
         */
        
        /**
         * 文档注释
         * @Description HelloWorld
         * @Author Jiangyunfei
         * */
    }
}

2.关键字和标识符

关键字:

abstract assert boolean break byte
case catch char class const
continue default do double else
enum extend final finally float
for goto if implements importt
instanceof int interface long native
new package private protected public
return strictfp short static super
switch synchronized this this throws
transient try void void while

Java 所有的组成部分都需要名字 (类名、变量名、方法名、标识符)

标识符注意点:

  • 首字符 字母(A-Z a-z)美元符号$ 下划线_ 开始
  • 首字符后——字母(A-Z a-z)美元符号$ 下划线_ 数字(0-9)
  • 不能使用关键字 大小写敏感

3.数据类型

Java是强类型语言:变量符合规定,必须先定义才能使用

Java的数据类型分为两类:

基本类型(primitive type)

byte(-128~127) short int long float double char boolean(true false)

​ 1 2 4 8 4 8 2 1

引用类型(reference type)

类 接口 数组

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

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

        //小数、浮点数
        float num5 = 50.1F;//float类型要在数字后面加个F
        double num6 = 3.14159;//最常用

        //字符
        char name = 'A';
        //字符串,String不是关键字,类
        String namea = "zhangsan";

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

4.什么是字节

  • 位(bit):计算机内部存储的最小单位,11001100 是一个八位二进制数
  • 字节(byte):计算机中数据处理的基本单位,习惯用大写B表示
  • 1B(byte,字节)= 8bit (位)
  • 字符:计算机中使用的字母、数字、字和符号

1bit表示1位

1Byte 1个字节 1B = 8b

1024B = 1KB

1024KB = 1M

1024M = 1G

5.数据类型拓展

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

        int i1 = 10;
        int i2 = 010;  //八进制0
        int i3 = 0x10; //十六进制0x 0-9 A-F 16

        System.out.println(i1);
        System.out.println(i2);
        System.out.println(i3);

        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);
        System.out.println(d);

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

        //字符拓展
        char c1 = 'a';
        char c2 = '中';

        System.out.println(c1);
        System.out.println((int) c1);//强制转换

        System.out.println(c2);
        System.out.println((int) c2);//强制转换

        //所有的字符本质还是数字
        //编码 Unicode表:97 = a , 65 = A  2字节 0 - 65536 Excel  2 16 = 65536

        // U0000 UFFFF
        char c3 = '\u0061';

        System.out.println(c3);

        //转义字符
        // \t 制表符
        // \n 换行
        //.....
        System.out.println("Hello\tWorld");
        System.out.println("Hello\nWorld");

        System.out.println("======");
        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){}
        //Less is More! 代码要精简易读
        
    }
}

6.类型转换

  • Java是强类型语言,进行运算的时候,需要用到类型转换

  • byte、short、char——int——long——float——double

  • 运算中,不同类型的数据要转换成同一类型,然后进行运算

  • 强制类型转换 高——低

  • 自动类型转换 低——高

    
    
    public class Demo5 {
        public static void main(String[] args) {
            int i = 128;
            byte b = (byte) i;//内存溢出
            
            //强制转换 (类型)变量名 高——低
            
            //自动转换  低——高
            int i1 = 128;
            double d = i1;
    
            System.out.println(i);
            System.out.println(b);
            
            /*
            注意点:
            1.不能对布尔值进行转换
            2.不能把对象类型转换成不相干的类型
            3.把高容量转换成低容量的时候,强制转换
            4.转换可能出现内存溢出,或者精度问题
            */
            System.out.print("==============");
            System.out.print((int)23.7); //23
            System.out.print((int)-45.89f); //-45
            
            System.out.print("==============");
            char c = 'a';
            int d = c + 1;
            System.out.print(d); //23    
            System.out.print((char)d);
        }
    }
    
public class Demo6 {
    public static void main(String[] args) {
        //操作比较大的数的时候,注意溢出问题
        //JDK7新特性,数字之间可以用下划线分割
        int money = 10_0000_0000;
        int year = 20;
        int total = money * year;
        System.out.println(total);//-1474836480,计算的时候溢出了
        long total2 = money * year;
        System.out.println(total2);//默认是int,转换之前已经存在问题了?

        long total3 = money*((long)year);//先把一个数转换成long
        System.out.println(total3);
        
        // L L
    }
}

7.变量

  • 变量:可以变化的量

  • 每个变量必须声明其类型

  • 程序中最基本的存储单元,变量名、变量类型、作用域

    type varName [=value][{,varName[=value]}];
    //数据类型 变量名 = 值; 可以用逗号隔开声明多个同类型变量
    

    注意事项:

    • 每个变量都有类型(可以是基本类型,也可以是引用类型)
    • 变量名必须有合法的标识符
    • 变量声明是一条完整的语句,每一个声明必须以分号结束
public class Demo7 {
    public static void main(String[] args) {
        //int a, b, c;
        int a = 1, b = 2, c = 3; //程序可读性
        String name = "jiangyunfei";
        char x = 'X';
        double pi = 3.14;
    }
}

8.变量的作用域

  • 类变量

  • 实例变量

  • 局部变量

    public class Variable {
        static int allClicks = 0; //类变量
        String str = "hello world"; //实例变量
    
        public void method() {
            int i = 0;//局部变量
        }
    }
    
    public class Demo8 {
        //类变量 static
        static double salary = 2500;
        
        //属性:变量
        
        //实例变量:从属于对象,如果不自行初始化,这个类型有默认值 0 0.0
        //布尔值:默认是false
        //除了基本类型,其余的默认都是null
        String name;
        int age;
        
        
        //main方法
        public static void main(String[] args) {
            //局部变量:必须声明和初始化值
            int i = 10;
            System.out.println(i);
            
            //变量类型 变量名字 = newDemo08();
            Demo8 demo8 = new Demo8();
            System.out.println(demo8.age);
            System.out.println(demo8.name);
            
            //类变量
            System.out.println(salary);
        }
        
        //其他方法
        public void add(){}
    }
    

package base;

public class Demo9 {
//修饰符,不存在先后顺序
static final double PI = 3.14;

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

}


## 9.常量

- 常量(Constant):初始化后(initialiae)后不能在改变值  不会动的值

- 可以理解为特殊的变量,它的值设定后,程序运行过程中不允许改变

  ```java
  final 常量名 = 值;
  final double PI = 3.14;
  ```

  常量一般用大写字符

  ```java
  public class Demo9 {
      //修饰符,不存在先后顺序
      static final double PI = 3.14;
  
      public static void main(String[] args) {
          System.out.println(PI);
      }
  }
  ```

## 

## 10.变量的命名规范

- 变量、方法、类名:见名知意
- 类成员变量:首字母小写和驼峰原则:monthSalary 除了第一个单词以外,后面的单词首字母大写
- 局部变量:首字母小写和驼峰原则
- 常量:大写字母和下划线:MAX_VALUE
- 类名:首字母大写和驼峰原则:Man,GoodMan
- 方法名:首字母小写和驼峰原则:run(),runRun()

## 11.运算符

Java支持如下运算符:  **优先级()**

- 算术运算符:+	- * / % ++ --
- 赋值运算符: =
- 关系运算:> < >= <= == != instanceof
- 逻辑运算符:&& || !
- 位运算符:& | ^ ` >> << >>>(了解)
- 条件运算符 ?:
- 扩展赋值运算符 += -= *= /=

## 12.包机制

- 更好地组织类,Java提供了包机制,用于区别类名的命名空间

- 包语句的语法格式为:package pkg1[[.pkg3...]];

- 公司域名倒置作为包名

- 使用某一个包的成员,在Java程序中明确导入该包。使用import语句:import package1[.package2...].(classname|*)

## 13.JavaDoc

javadoc命令用来生成自己的API文档

参数信息:

@author 作者名

@version 版本号

@since 指明最早需要的JDK版本

@param 参数名

@return 返回值情况

@throws 异常抛出情况

生成文档命令:

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

```java
package Operator;

public class Demo1 {
    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 / (double)b);//0.5
    }
}

package Operator;

public class Demo2 {
    public static void main(String[] args) {
        long a = 123123123123123123L;
        int b = 123;
        short c = 10;
        byte d = 8;

        System.out.println(a + b + c + d);//Long
        System.out.println(b + c + d);//Int
        System.out.println((double) c + d);//Int
    }
}
package Operator;

public class Demo3 {
    public static void main(String[] args) {
        //关系运算符的结果:正确 错误 布尔值

        int a = 10;
        int b = 20;
        int c = 21;

        System.out.println(c % a);//    c/a

        System.out.println(a > b);
        System.out.println(a < b);
        System.out.println(a == b);
        System.out.println(a != b);
    }
}
package Operator;

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

        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(2,3);
        System.out.println(pow);//8.0
    }
}

package Operator;

//逻辑运算符
public class Demo5 {
    public static void main(String[] args) {
        //或(or)与(and)非(取反)
        boolean a = true;
        boolean b = false;

        System.out.println("a && b:" + (a && b));//false
        System.out.println("a || b:" + (a || b));//true
        System.out.println("!(a && b):" + !(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 Demo6 {
    public static void main(String[] args) {
        /*
        A = 0011 1100
        B = 0000 1101
        -----------------------------------------
        A&B = 0000 1100  两个为1结果才是1
        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);
    }
}

package Operator;

public class Demo7 {
    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);
        System.out.println(a + b + " ");
    }
}
package Operator;
//三元运算符
public class Demo8 {
    public static void main(String[] args) {
        // x ? y : z
        //如果x==true,结果为y,否则为z

        int score = 50;
        String type = score < 60 ?"不及格":"及格";//必须掌握
        // if
        System.out.println(type);
    }
}
package Operator;

/**
 * @author Jiangyunfei
 * @version 1.0
 * @since 1.8
 */
public class Doc {
    String name;
    /***
     * @author Jiangyunfei
     * @param name
     * @return
     * @throws
     */
    public String test(String name) throws Exception{
        return name;
    }
}