Java基础01

Java基础01

目录

  1. 注释、标识符、关键字

  2. 数据类型

  3. 类型转换

  4. 变量、常量

  5. 运算符

  6. 包机制、JavaDoc

     

注释
  • 单行注释

  • 多行注释

  • 文档注释

public class Demo1 {
   public static void main(String[] args) {
       System.out.println("Hello,World");
       // 单行注释
     
       /*
       多行注释
        */
       
       /**文档注释 与JavaDoc结合使用
        * @Description
        */
  }
}

 

关键字

如下图所示:

 

 

标识符

  • 所有的标识符都应以字母(A-Z或者a-z),美元符($)、或者下划线(_)开始

  • 首字符之后可以是字母(A-Z或者a-z),美元符($)、或者下划线(_)或者数字的任意组合

  • 不能使用关键字作为变名或者方法

  • 标识符大小写敏感

  • 合法标识符举例:age,$salary,_value,__1_value

  • 非法标识符举例:123ek,-salary,#abc

 

数据类型

强类型语音
  • 要求变量的使用要严格符号规定,所有的变量都必须先定义后才可以使用

弱类型语言

 

Java的数据类型分为两大类
  • 基本类型(primitive type)

  • 引用类型(reference type)

 

public class Demo2{
 public static void main(String[] args){
   //整数
   int num1=10;
   byte num2 =20;
   short num3=30;
   long num4=30L;
  //浮点数
   float num5=50.1F;
   double num6=3.1415926;
   //字符
   char name='Java';
   //字符串 字符串不是关键字,类
   String age='123';
   //布尔值:是 非
   boolean flag=true;
   boolean tage=flase;
}
}
public class Demo03 {
   public static void main(String[] args) {
       //浮点拓展
       float f =0.1f;
       double d =1.0/10;
       System.out.println(f==d);//false

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

       /*
       float 有限 离散 舍入误差 大约 接近但不相等
        */
       //=================字符拓展 字符的本质是数字
       // unicode 编码
       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

       char c3 = '\u0061'; // unicode 编码
       System.out.println(c3);

       //========= 转义字符 \t \n

       System.out.println("Hello\tWorld"); // \t 制表符,等于2个空格
       System.out.println("Hello\nWorld");

       // boolean 拓展
       boolean flag = true;
       //if (flag==true){}
       //if (flag){} 建议如此写
  }
}

 

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

类型转换
  • 由于Java是强类型语言,所以要进行有些运算的时候,需要用到类型转换

        ----->
     byte,short,char->int->long->float->double
  • 运算中,不同类型的数据线转化为同一类型,然后进行运算

  • 强制类型转化 (高到低)

  • 自动类型转化(低到高)

public class Demo05 {
   public static void main(String[] args) {
       // 低----->高
       // byte,short,char->int->long->float->double

       int i =128;
       byte b =(byte) i;
       // 强制类型转化 (类型)变量名
       System.out.println(i);//128
       System.out.println(b);// -128

       byte c =1;
       double d = c;
       System.out.println(c);
       System.out.println(d);
       // 自动转化
       /*
       1.不能对布尔值进行转化
       2.不能把对象类型转化为不相干的类型
       3.在把高容量转化到低容量时,强制转化
       4.转化时可能存在内存溢出或者精度问题
        */
       System.out.println((int) 23.7);//23
       System.out.println((int)-45.89f);//45

       //==============
       System.out.println("==============");
       char e ='a';
       int f=e+1;
       System.out.println(f);//98
       System.out.println((char)f);//b
  }
}

public class Demo06 {
   public static void main(String[] args) {
       //操作比较大的数据,注意溢出问题
//       int money = 100000000;
       int money = 10_0000_0000;//数字下可以加下划线,进行区分
       int year = 20;
       int total = money*year;
       System.out.println(total);//-1474836480 长度超限了
       long total2= money*year; //默认是int,转换前已经存在精度损失了
       System.out.println(total2);//-1474836480

       long total3 = money*((long) year);//计算前,先强制转化为高容量数据
       System.out.println(total3);//20000000000
  }
}

 

 

变量
  • 变量是什么:就是可以变化的量

  • Java是一种强类型语言,每个变量都必须声明其类型

  • Java变量是程序中最基本的存储单元,其要是包含变量名、变量类型和作用域

    type varName [=value][{,varName[=value]}]
     //数据类型 变量名=值;可以使用逗号隔开来声明多个同类型变量
     int a = 1;
  
注意:
 
- 每个变量都有类型,类型可以是基本变量,也可以是引用类型
- 变量名必须是合法标识符
- 变量声明是一条完整的语句,因此每个声明都必须以分号结束

##### 变量作用域

- 类变量

- 实例变量

- 局部变量

```java
public class Variable{
  static int allClicks=0;//类变量
  String str="Hello,world";//实例变量
   
  public void method(){
    int i=0;//局部变量
  }
}
 
public class Demo08 {
    //属性:变量
 
    //main方法
    public static void main(String[] args) {
 
    }
}

 

常量
  • 常量(constant),初始化(initialize)后不能再改变值,不会变动的值

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

  • 常量名一般使用大写

    final 常量名=值;
    final double PI=3.14
    public class Demo09 {
       
       //修饰符,不存在先后顺序
       static final double PI=3.14;//常量

       public static void main(String[] args) {
           System.out.println(PI);
      }
    }
变量命名规范
  • 所有变量、方法、类名:见名知意

  • 类成员变量:首字母小写和驼峰原则:monthSalary 除了第一个单词以外,后面的单词首字母大写

  • 局部变量:首字母小写和驼峰原则

  • 常量:大写字母和下划线MAX_VALUE

  • 类名:首字母大写和驼峰原则;Man,GoodMan

  • 方法名;首字母小写和驼峰原则:run(),TestRun()

     

运算符
  • 算术运算符:+,-,*,/,%,++,--

    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);//30
           System.out.println(a-b);//-10
           System.out.println(a*b);//200
           System.out.println(a/(double)b);//0.5
      }
    }
    public class Demo02 {
       public static void main(String[] args) {
           long a=121212121313L;
           int b =123;
           short c = 10;
           byte d=8;

           System.out.println(a+b+c+d);//121212121454 long 处理的数据中,有long,double时,都会转化为long,double
           System.out.println(b+c+d);//141 int   其他类型数据,输出结果都为int
           System.out.println(c+d);//18 int
      }
    }
    public class Demo04 {
       public static void main(String[] args) {
           //++ 自增 --自减
           int a =3;
           int b =a ++; //a++ a=a+1 先给b赋值,a在自增

           System.out.println(a);
           int c =++ a;// ++a a=a+1 先自增,再给b赋值

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

           //幂运算 一些特殊运算,可有使用一些工具类操作
           double pow = Math.pow(3,2);
           System.out.println(pow);
      }
    }

     

  • 赋值运算符:=

  • 关系运算符:>,<,>=,<=,==, !=, instanceof

    public class Demo03 {
       public static void main(String[] args) {
           //关系运算符返回的结果:正确、错误 布尔值
           int a=10;
           int b=20;
           int c=11;

           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);//取余数 模运算
      }
    }

     

  • 逻辑运算符:&&,||,! (与,或,非)

    public class Demo05 {
       public static void main(String[] args) {
           // 与 && 或 || 非 !
           boolean a=true;
           boolean b=false;

           System.out.println("a && b:"+(a&&b));//a && b:false
           System.out.println("a || b:"+(a||b));//a || b:true
           System.out.println("!(a && b):"+!(a&&b));//!(a && b):true

           //短路运算 因c<4为false,后面代码无需判断,所以c的值仍然为5
           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
            * A^B = 0011 1101
            * ~B = 1111 0010
            *
            * 2*8 2*2*2*2 2*8怎么运算最快
            * << 左移 >> 右移动
            * 0000 0000 0
            * 0000 0001 1
            * 0000 0010 2
            * 0000 0011 3
            * 0000 0011 3
            * 0000 0100 4
            * 0000 1000 8
            * 0001 0000 16
            */

           System.out.println(2<<3); //16

      }
    }

     

  • 条件运算符: ?

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

 

  • 拓展赋值运算符: +=,-=,*=,/=

public class Demo07 {
   public static void main(String[] args) {
       int a=10;
       int b=20;
       a+=b;
       a-=b;

       System.out.println(a);
       System.out.println(a);
       //字符串连接符 + ,String
       System.out.println(""+a+b); //1020 字符串 注意:当使用+号链接
       System.out.println(a+b+"");//30 int类型
  }
}

 

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

  • 包语句的语法格式为

    package pkg1[.pkg2[.pkg3]]
  • 一般利用公司域名倒置作为包名;

    com.baidu.com   com.kuangstudy.blog

     

  • 为了能够使用某一包的成员,需要在java程序中明确导入该包。使用import语句

    import package1[.package2...].(classname|*)

 

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

  • 参数信息

    • @author 作者

    • @version 版本号

    • @since 指明最早需要的jdk版本

    • @param 参数名

    • @return返回值

    • @throws异常抛出情况

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

感谢:

狂神大大开源的学习视频,讲的很详细,bilibili搜索狂神说java开源视频!

posted @ 2020-10-30 16:36  蓬莱大侠  阅读(133)  评论(0)    收藏  举报