Java学习2-基础语法

Java基础语法

一、注释、标识符、关键字

注释

  • 代码量较多时、项目结构复杂时,我们需要注释帮助自己记忆理解

  • 注释不会运行,是给我们写代码的人看的

  • 书写注释是一个非常好的习惯

  • 平时写代码一定要注意规范

  • Java的三种注释

    1. 单行注释://加注释

       

    2. 多行注释:

      /*

      注释

      */

       

    3. 文档注释

 

/** 加回车

  • 代码

    package com.louwen.base;

    // Hello是类名
    public class HelloWorld {
       //main是方法名
       public static void main(String[] args) {
           //单行注释
           //输出一个Hello,World!
           System.out.println("Hello<World!");
           //变量
           String teacher = "louwen";
      }
        /*
           多行注释
            */
       //JavaDoc:文档注释
       /**
        * @Description 描述HelloWorld
        * @Author 作者louwen
        */
    }

     

标识符

  • Java所有组成部分都需要名字。类名、变量名以及方法名都被称为标识符。

     

  • 关键字

     

  • 标识符注意点

     

二、数据类型

强类型语言

  • 要求变量的使用要严格符合规定,所有变量都必须先定义后才能使用(安全性高,速度慢)

弱语言类型

  • 要求变量的使用要符合规定,所有变量都必须先定义后才能使用(安全性低,速度稍快)

Java的数据类型分为两大类

  • 基本类型(primitive type)

  • 引用类型(reference type)

  •  

  • 什么是字节

 

代码

package com.louwen.base;

public class Demo02 {
   public static void main(String[] args) {
       //八大基本数据类型
//整数
       //Integer,Byte //去掉注释后,摁住ctrl点击查看Integer函数(关于int类型的函数),其他同理
       //整数类型,一个字节
       byte num1 = 20;
       //整数类型(四字节),最常用
       int num2 =10;
       //整数类型,两个字节
       short num3 =100;
       //整数类型,四个字节,要在数字后面加个L
       long num4 =1000L;
//小数:浮点数
       //单浮点数,四个字节,要在数字后面加F
       float num5 = 50.1F;
       //双浮点数,八个字节
       double num6 = 3.141592653589793238462643;
//字符类型
       //只能写一个字符
       char name = '文';
       char name1 = 'A';
//字符串
       //定义的类型是字符串,但String不是关键词,是类
       String name2 = "louwen";
//布尔值
       //只有两个值,是或非
       boolean flag = true;
       boolean flag2 = false;



       System.out.println(num1);
       System.out.println(name);
  }
}

代码2

package com.louwen.base;

public class Demo03 {
  public static void main(String[] args) {
       // 整数拓展 进制 二进制0b 八进制0 十进制 十六进制0x
      int i1 = 10;
      int i2 = 010;//八进制0x
      int i3 = 0x10; //十六进制0x 0-9 A-F
      System.out.println(i1);
      System.out.println(i2);
      System.out.println(i3);
      System.out.println("--------------------------");
       //-------------------------------------
       //浮点数拓展   银行业务怎么表示?
       //BigDecimal 数学工具类
       //-------------------------------------
       //float 有限 离散 舍入误差 大约 接近但不等于
       // 最好完全避免使用浮点数进行比较
       // 最好完全避免使用浮点数进行比较
       // 最好完全避免使用浮点数进行比较
       //double
      float f = 0.1f;
      double d = 1.0 / 10;

      System.out.println(f == d);//false
      System.out.println(f);
      System.out.println(d);

      float f1 = 2131312231313f;
      float f2 = f1 + 1;

      System.out.println(f1 == f2); //true
       //-------------------------------------
       //字符拓展
       //------------------------------------
      System.out.println("--------------------------");
      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,20013=中)   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);

      String sc = "Hello World";
      String sd = "Hello World";
      System.out.println(sc == sd);


      System.out.println("--------------------------");
       //布尔值扩展
      boolean flag = true;
      if (flag == true) {
       } //新手
      if (flag) {
       }  //老手
       // Less is More!   代码要精简
   }
}

 

三、类型转换

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

    低------------------------------------------>高

    byte,short,char,int,long,float,double

  • 运算中,不同的数据类型先转换为同一类型,然后运算

 

  • 强制类型转换

  • 自动类型转换

  • 代码

    package com.louwen.base;

    public class Demo05 {
       public static void main(String[] args) {
           int i = 128;
           //强制转换 (类型)变量名 高---->低
           byte b = (byte) i; //byte最大值是127.赋值128,内存溢出
           //自动类型转换   低----->高
           double c = i;

           System.out.println(i);
           System.out.println(b);
           System.out.println(c);

           /*
           注意点:
           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 d = 'a';
           int f= d +1;
           System.out.println(f);
           System.out.println((char)f);
      }
    }
  • 代码2

    package com.louwen.base;

    public class Demo06 {
       public static void main(String[] args) {
           // 操作比较大的数的时候,主义溢出问题
           //JDK7新特性,数值之间可以用下划线分割
           int money =10_0000_0000;
           int years = 20;
           int total= money*years;// -1474836480 计算的时候溢出了
           long total2 =money*years; //默认int计算,转换之前已经存在问题
           long total3 = money*((long)years);//先把一个数转换为long,计算时则使用ling类型

           System.out.println(money);
           System.out.println(total);
           System.out.println(total3);
      }
    }

     

四、变量、常量

变量

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

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

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

    type varName [=value] [{,varName[=value]}];
    //数据类型   变量名 = 值; 可以使用逗号隔开声明多个同类型变量(尽量不使用)
  • 注意事项

    • 每个变量都有类型,类型可以是基本类型,也可以是引用类型。

    • 变量名必须是合法的标识符。

    • 变量声明是一条完整的语句,因此每一个声明都必须以分号结尾

  • 代码

    package com.louwen.base;

    public class Demo07 {
       public static void main(String[] args) {
           int a = 1;
           int b = 2;
           int c = 3; //注意程序可读性,不要在一句定义多个变量
           String name = "louwen";
           char x = 'X';
           double pi = 3.14;

      }
    }
  • 代码2

    package com.louwen.base;

    public class Demo08 {  

       //属性:变量

       //实例变量:从属于对象;如果不自行初始化,这个类型的默认值 0 0.0 u0000
       // 布尔值:默认值是false
       //除了基本类型,其余都是null
       String name;
       int age;

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

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

           //局部方法,只在当前方法内使用;必须声明和初始化
           int i = 10;

           System.out.println(i);

           //变量类型 变量名字 = new Demo08();
           Demo08 demo08 = new Demo08();
           System.out.println(demo08.age);
           System.out.println(demo08.name);

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

      }

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

      }
    }

     

变量作用域

  • 类变量

  • 实例变量

  • 局部变量

  • 代码

    package com.louwen.base;

    public class Demo08 {  

       //属性:变量

       //实例变量:从属于对象;如果不自行初始化,这个类型的默认值 0 0.0 u0000
       // 布尔值:默认值是false
       //除了基本类型,其余都是null
       String name;
       int age;

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

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

           //局部方法,只在当前方法内使用;必须声明和初始化
           int i = 10;

           System.out.println(i);

           //变量类型 变量名字 = new Demo08();
           Demo08 demo08 = new Demo08();
           System.out.println(demo08.age);
           System.out.println(demo08.name);

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

      }

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

      }
    }

     

     

常量

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

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

    final 常量名 = 值;
    final double PI =3.14
  • 常量一般用大写字符

  • 代码

    package com.louwen.base;

    public class Demo09 {

      static final double PI = 3.14;
      //final static修饰符,不存在先后顺序
      final static   double PI2 = 3.14;
      public static void main(String[] args) {

          System.out.println(PI);
          System.out.println(PI2);
      }
    }

     

     

变量的命名规范

  • 所有的变量、方法、类名:见名知意

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

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

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

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

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

五、运算符

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

  • 算术运算符:+ , - ,* ,/ , %(模运算(取余) ,++ ,--

  • 赋值运算符: =

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

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

  • 位运算符: & , | , ^ ,~ , >> , << , >>>(了解!!!)

  • 条件运算符:?:

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

  • 代码1

    package com.louwen.operator;

    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);
           System.out.println(a-b);
           System.out.println(a*b);
           System.out.println(a/(double)b);//有小数时注意作用范围,尤其除法
      }
    }

     

  • 代码2

    package com.louwen.operator;

    public class Demo02 {

       public static void main(String[] args) {

           long a = 1212312123123L;
           int b =123;
           short c = 10 ;
           byte d = 127 ;
           byte d1= 127 ;
           char e = 'a';

           // 自动升为混合运算类型中最高类型

           System.out.println(a+b+c+d); //long
           System.out.println(b+c+d);  //int
           System.out.println(c+d);   //int
           System.out.println(b+e);   //int
           System.out.println(d+e);   //?
           System.out.println(e);   //char
           System.out.println(d+2);   //?
           System.out.println(d+d1);   //?

      }
    }

     

  • 代码3

    package com.louwen.operator;

    public class Demo03 {

    public static void main(String[] args) {

    //关系运算符返回结果: 正确,错误 布尔值
    //常与if语句使用
    int a = 10;
    int b = 20;
    int c = 21;

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

    // 取余,模运算; c/a的余数 21/10 = 2 …… 1
    System.out.println(c%a);
    }
    }

     

  • 代码4

    package com.louwen.operator;

    public class Demo04 {

    public static void main(String[] args) {

    //++ -- 自增,自减 一元运算符
    int a = 3;
    int b = a++; //a++ a = a + 1 即执行这句代码后,先给b赋值,再自增
    System.out.println(a);

    int c = ++a; //在执行这句代码前,先自增,再给c赋值
    System.out.println(c);
    //System.out.println(a);
    //System.out.println(b);
    //System.out.println(c);\

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

    }
    }

     

  • 代码5

    package com.louwen.operator;

    //逻辑运算符
    public class Demo05 {

    public static void main(String[] args) {

    // 与(snd),或(or),非(取反)
    boolean a =true;
    boolean b = false;

    System.out.println("a && b" + (a && b));//逻辑与运算:两个变量都为真,结果才为true
    System.out.println("a || b" + (a || b));//逻辑或运算:两个变量有一个为真,结果才为true
    System.out.println("!(a || b)" +!(a || b));//逻辑与运算:两个变量都为真,结果才为真

    //短路运算
    int c = 5;
    boolean d = (c<4) && (c++<4);
    System.out.println(d);
    // 执行了(c<4)后,结果为假,得出结果,&&后面的语句不再执行,即C++没有执行
    System.out.println(c);
    }



    }

     

  • 代码6

    package com.louwen.operator;

    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*8 = 16 2*2*2*2

    //效率极高!!!!
    << 左移1(<<1)相当于*2
    >> 右移1(<<1)相当于/2
    0000 0000 0
    0000 0001 1
    0000 0010 2
    0001 0000 16 = 2<<3
    */

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


    }



    }

     

  • 代码7

    package com.louwen.operator;

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

    //字符串连接符 + ,出现String类型则为连接
    System.out.println(a+b);
    //如果字符串在前面,则进行拼接
    System.out.println(""+a+b);
    //如果字符串在后面,则进行运算+字符串
    System.out.println(a+b+" 5");

    }
    }

     

  • 代码8

    package com.louwen.operator;


    //导入这个包下所有类!
    import com.louwen.base.*;
    import java.util.Date;

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

    int score = 50;
    String type = score < 60 ? "不及格" :"及格";//必须掌握,精简,易于掌握
    System.out.println(type);

    }
    }

     

六、包机制、JavaDoc

包机制

  • 本质是一个文件夹

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

  • 包语句的语法格式为:

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

    www.baidu.com 包名:com.baidu.www

     

  • 为了能使用某一个包的成员,我们需要在Java程序中明确导入该包,使用“import”语句可完成此功能

    import pkg1[.pkg2[.pkg3...]].(classname|*);

JavaDoc

  • Javadoc命令是用来生成自己API文档的

     

  • 参数信息

    • @author 作者名

    • @version 版本号

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

    • @return 返回值情况

    • @throws 异常抛出情况

  • 代码

    package com.louwen.base;

    /**
    * @author louwen
    * @version 1.0
    * @since 1.8
    */
    public class Doc {

    //属性
    String name;

    //方法
    //加在类上就是类的注释,加在方法上就是方法的注释
    /**
    * @author louwen
    * @param name
    * @return
    * @throws Exception
    */



    public String test(String name) throws Exception {

    return name;

    }
    //命令行生成JavaDoc文件 : javadoc -encoding UTF-8 -charset UTF-8 java文件
    //学会通过IDEA生产JavaDoc文档 -encoding UTF-8 -charset UTF-8 -windowtitle "test"

    }

     

     

  •  

 

posted @ 2021-04-29 12:38  娄文  阅读(46)  评论(0)    收藏  举报