java基础(1.注释、标识符、关键字 2.数据类型 3.类型类型扩展及面试题 4.类型转化 5.变量、常量、作用域 6.基本运算符 7.自增、自减、初识Math类 8.逻辑运算符、位运算符 9.三元运算符及小结 10.包机制 11.JAVADoc生成文档)

java基础

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


注释

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

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

  • java中注释有三种:

    1. 单行注释: Ctrl+/

    2. 多行注释:Ctrl+Shift+/

    3. 文档注释:

      public static void main(String[] args){
          //单行注释
          /*多行注释
          多行注释*/
          /**文档注释*/
          System.out.println("hello world");
      //有趣代码注释
      //                            _ooOoo_    
      //                           o8888888o    
      //                           88" . "88    
      //                           (| -_- |)    
      //                            O\ = /O    
      //                        ____/`---'\____    
      //                      .   ' \\| |// `.    
      //                       / \\||| : |||// \    
      //                     / _||||| -:- |||||- \    
      //                       | | \\\ - /// | |    
      //                     | \_| ''\---/'' | |    
      //                      \ .-\__ `-` ___/-. /    
      //                   ___`. .' /--.--\ `. . __    
      //                ."" '< `.___\_<|>_/___.' >'"".    
      //               | | : `- \`.;`\ _ /`;.`/ - ` : | |    
      //                 \ \ `-. \_ __\ /__ _/ .-` / /    
      //         ======`-.____`-.___\_____/___.-`____.-'======    
      //                            `=---='    
      //    
      //         .............................................    
      //                  佛祖保佑             永无BUG
      
      
      }
      

      标识符

      • java所有的组成部分都需要名字。类名、变量名以及方法名都都被称为标识符
      • 标识符注意点
        1. 所有的标识符都应该以字母(A-Z或者a-z),美元符($),或者下划线(_)开始
        2. 首字符之后可以是字母(A-Z或者a-z),美元符($),下划线(_)或数字的任何组合
        3. 不能使用关键字作为变量名或方法名
        4. 标识符是大小写敏感的
        5. 可以使用中文名,但是一般不建议这样去使用,也不建议使用拼音,很low

      关键字

2.数据类型

  • 强类型语言(安全性高、速度慢)

    • 要求变量的使用要符合规定,所有的变量都必须先定义后使用
  • java的数据类型分为两大类

    1. 基本类型
    2. 引用类型

    public class Dem0{
        public static void main(){
            //八大基本数据类型
            int num1 = 10;
            byte num2 = 127;
            short num3 = 30;
            long num4 = 30L;//Long类型要在数字后面加L
            //小数:浮点数
            float num5 = 50,1F;//float类型要在数字后面加F
            //字符
            char = 'A';
            //字符串,String不是关键字,类
            String namea = "你好!"
            //布尔值:是非
            boolean flag = true;
            //boolean flag = flase;
            
        }
    }
    
  • 什么是字节

    • 位(bit):是计算机内部数据存储的最小单位
    • 字节(byte):是计算机中数据处理的基本单位,
    • 1B(byte,字节) = 8bit(位)
    • 字符:是计算机中使用的字母、数字,字和符号
  • 32位的电脑和64位的电脑区别:寻址能力不同

3.类型类型扩展及面试题

public class Demo2{
    public static void main(String[] agrs){
        //整数拓展:  进制   二进制0b   十进制    八进制0    十六进制0x
        int i = 10;
        int i2 = 010;
        int i3 = 0x10;
        System.out.println(i);
        System.out.println(i2);//八进制0
        System.out.println(i3 );//十六进制0x   0-9  A-F 16
        System.out.println("--------------------------------");
        //=============================================================
        //浮点数拓展    银行业务怎么表示   不要用float和double
        //BigDecimal   数学工具类
        //===============================================================
        //float    有限   离散    舍入误差    大约    接近但不等于
        //double
        float f = 0.1f;//0.1
        double d = 1.0/10;//0.1
        System.out.println(f==d);//false
        float d1 = 12345678f;
        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=65536=2的十六次方
        char c3 = '\u0061';
        //转义字符
        //  \t  制表符
        //    \t  换行
        //.....
        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);
        //对象   从内存分析

        //布尔值得扩展
        boolean flag = true;
        if (flag){}//老手
        if (flag==true){}//新手
        //less is more!  代码要精简易读

    }
}

4.类型转化

  • 由于java是强类型语言,所以要进行有些运算的时候的,需要用到类型转换
  • 运算中,不同类型的数据先转化为同一类型,然后进行运算
    • (低-高):byte,short,char, < int < long < float < double
  • 强制类型转换
  • 自动类型转换
 public static void main(String[] args) {
     int i = 128;//128
        byte b = (byte) i;//-128   内存移除
        //强制转换     (类型)变量名    高--低
        //自动转换                    低---高
        System.out.println(i);
        System.out.println(b);

        /*注意点:
        * 1.不能对布尔值进行转换
        * 2.不能把对象类型转换为不相干的类型
        * 3.把高容量转换为低容量的时候,强制转换
        * 4.转换的时候可能存在内存溢出,或精度问题
        * 5.
        * */
        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);
        System.out.println((char)d);
        //操作比较大的数的时候,注意溢出问题
        //JDK7新特性,数字之间可以用下划线分割
        int money = 10_0000_0000;
        System.out.println(money);//1000000000
        int years = 20;
        int total = money*years;//-1474836480,计算的时候溢出了
        System.out.println(total);//-1474836480
        long total2 =money*years;
        System.out.println(total2);//-1474836480
        long total3 = money*((long)years);//先把一个数变为long
        System.out.println(total3);//20000000000

    }

5.变量、常量、作用域

变量

  • 什么是变量:就是可以变化的量!
  • java是一种强类型语言,每个变量都必须声明其类型
  • java变量是程序中最基本的存储单元,其要素包括变量名,变量类型和作用域
type verName [=verName]
    //数据类型  变量名 = 值;
  • 注意事项:

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

    2. 变量名必须是合法的标识符

    3. 变量声明是一条完整的语句,因此每一个声明都必须以分好结束

      //int a,b,c;
              int a=1,b=2,c=3;
              String name = "VzhV";
              char x = 'x';
              double pi = 3.14;
      

    变量作用域

    • 类变量:写在类里面
    • 实例变量:写在类中间
    • 局部变量:写在方法里面
    public class Veriable{
        static int all=0;//类变量static关键词
        String str="hello world";//实例变量
        public void method(){
            int i = 0;//局部变量
        }
        System.out.println("========================================================")
        //类变量 static
        static double salary = 9555;
    
        //属性:变量
    
        //实例变量(作用域比局部变量大):类里方法外。从属于对象;如果不自行初始化,这个类型的默认值  null
        //布尔值默认值是:true和false
        //除了基本类型其余默认值都是null
        String name;
        int age;
        //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(){
    
        }
    }
    

    常量

    • 常量:初始化后不能在改变值,不会变动的值

      final 常量名 = 值;
      final double PI=3.14;
      //修饰符不存在顺序
          static final double PI=3.14;
          public static void main(String[] args) {
              System.out.println(PI);
          }
      
    • 常量一般使用大写字母

    • 变量命名规则:

      1. 所有变量名、方法、类名:见名知意
      2. 类成员变量:首字母大写和驼峰原则,除了以一个单词以外,后面的单词首字母要大写
      3. 局部变量:首字母小写和驼峰原则
      4. 常量:大写字母和下划线
      5. 类名:首字母大写和驼峰原则
      6. 方法名:首字母小写和驼峰原则

      6.基本运算符:

      • **java语言支持一下运算符: **

        1. 算数运算符:*+, -, , /, %, ++, --

        2. 赋值运算符:=

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

        4. 逻辑运算符:&&, ||, !

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

        6. 条件运算符:?, :

        7. 扩展运算符:*+=, -=, =, /=

          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);
                  System.out.println("==================================================");
              }
          public static void main(String[] args) {
                  long a = 123123123123123L;
                  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(c+d);//int
                  System.out.println("==================================================");
              }
          public static void main(String[] args) {
                  //关系运算符返回的结果:正确  错误   布尔值
                  int a = 10;
                  int b = 20;
                  int c = 21;
                  //取余,模运算
                  System.out.println(c%a);//   c/a  21/20 = 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
                  System.out.println("==================================================");
              }
          
          

      7.自增、自减、初识Math类

      public static void main(String[] args) {
              //++:自增     --:自减
              int a = 3;
              int b = a++;//执行完这行代码后,先给b赋值,在自增
              //a = a+1;
              System.out.println(a);
              //a++   a=a+1//执行完这行代码前,先自增,给b赋值
              int c = ++a;
              System.out.println(a);
              System.out.println(b);
              System.out.println(c);
              //幂运算 2*2*2 = 8;使用工具类来操作
              double pow = Math.pow(3,2);
              System.out.println(pow);
          }
      public static void main(String[] args){
           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+"");//依旧进行运算
          }
      }
      

      8.逻辑运算符、位运算符

      • 逻辑运算符
      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 || b));//逻辑与运算  两个变量有一个为真,结果为true
              System.out.println("! (a && b):"+! (a && b));//如果是真,则变为假,如果为假则为真
              //短路运算
              int c = 5;
              boolean d = (c<4) && (c++ < 4);
              System.out.println(d);
              System.out.println(c);
          }
      
      • 位运算符

        public static void main(String[] args) {
                /*
                * A =     0011  1100
                * B =     0000  1101
                *---------------------------------------
                * A & B   0000 1100   两真则真
                * A | B   0011 1101   一真则真
                * A ^ B   0011 0001   相同为0
                * ~ 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
                * */
                //2左移
                System.out.println(2<<3);
            }
        

      9.三元运算符及小结

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

      10.包机制

      • 为了更好的组织类,java提供了包机制,用于区别类名的命名空间
      • 包语句的语法格式为:package pkg1[ .pkg2[ .pkg3]];
      • 一般利用公司域名倒置作为包名
      • 为了能够使用某一个包的成员,我们需要在java程序中明确导入该包。使用”inport“语句可完成次功能
        • 包语句的语法格式为:import package1[ .package2[ .classname|*];

      11.javaDoc生成文档

      • JAVADoc命令是用来生成自己API文档的
      • 参数信息
        1. @author 作者名
        2. @version 版本号
        3. @since 指明需要最早使用的JDK版本
        4. @param 参数名
        5. @return 返回值情况
        6. @throws 异常抛出情况
      package com.zhou.base;
      /*
       * @author       kuanshhen
       * @oversion     1.0
       * @since        1.8
       * */
      public class Doc {
          String name;
      
          /**
           *
           * @param name
           * @return
           * @throws Exception
           */
          public String test(String name) throws Exception{
              return name;
              //我是通过命令行  javadoc   参数   java文件
              //javadoc -encoding UTF-8 -charset UTF-8 Doc.java
          }
      }
      
      • 和注释的区别:
        • 选中当前类右击
        • 选中show in explorer
        • 在导航栏前加空格加cmd回车
        • 输入命令:javadoc -encoding UTF-8 -charset UTF-8 Doc.java
posted @ 2021-04-05 09:14  园小白  阅读(77)  评论(1编辑  收藏  举报