Java基础语法

注释

注释并不会被执行,只是给人看的

Java中的注释有三种:

  • 单行注释 //
  • 多行注释 /* */
  • 文档注释/** */

标识符

  • 关键字

  • 标识符注意点
    • 所有的标识符都应该以字母(A-Z或者a-z)、美元符($)、或者下划线(_)开始
    • 首字符后可以是字母(A-Z或者a-z)、美元符($)、或者下划线(_)或数字的任何字符组合
    • 不能使用关键字作为变量名或方法名
    • 标识符是大小写敏感的

数据类型

补充内容:

什么是字节?

  • 位(bit):是计算机内部数据存储的最小单位

  • 字节(byte):是计算机中数据处理的基本单位,习惯上用大写B来表示

  • 1B (byte,字节) = 8 bit(位)

  • 字符:是指计算机中使用的字母、数字、字和符号

    1bit 表示1位
    1 Byte 表示一个字节 1B = 8 b
    1024B = 1KB
    1024KB = 1M
    1024M = 1G

    电脑32位和64位区别?

  • 强类型语言

    • ​ 要求变量的使用严格符合规定,所有变量都必须先定义后才能使用
  • 弱类型语言

  1. Java的数据类型分为两大类

    • 基本类型(primtive type)

      • 数值类型

        • 整数类型

          byte占1个字节范围:-128~127

          short 2字节

          int 4字节

          long 8字节

        • 浮点类型

          float 4字节

          double 8字节

        • 字符类型

          char 2字节

      • boolean类型

        占1位其值只有true和false两个

    • 引用类型(reference type)

      • 接口
      • 数组
public class HelloWorld {
    public static void main(String[] args) {
       //八大基本数据类型

        //整数
        int num1 = 10; //最常用
        byte num2 = 20;
        short num3 = 30;
        long num4 = 30L;//Long类型要在数字后面加个L
        Byte
        //小数:浮点数
        float num5 = 50.1F;
        double num6 = 3.141592653589793238462643;

        //字符
        char nmae = 'A';
        //字符串,String不是关键字,类
        String name = '姜新羽';

        //布尔值
        boolean flag = true;
//        boolean flag = false;


    }
}

面试题补充

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

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

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

        float d1 = 2313131223f;
        float d2 = d1 + 1;
        System.out.println(d1==d2);//ture

        //==================================================
        //字符拓展
        //==================================================
        char c1 = 'a';
        char c2 = '中';
        System.out.println(c1);
        System.out.println((int)c1);//强制换行
        System.out.println(c2);
        System.out.println((int)c2);//强制换行
        //所有的字符本质还是数字
        //编码 Unicode 2字节  0-65536    Excel  2^16 = 65536


        char c3 = '\u0061';//Unicode 的使用
        System.out.println(c3);//a
        //转义字符
        // \t   制表符
        // \n   换行
        System.out.println("Hello\tWorld");

        //对象,从内存分析
        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){}//老手
}
  1. 类型转换
public class HelloWorld {
    public static void main(String[] args) {
        int i = 128;
        byte b = (byte) i;//内存溢出

        //强制转换 (类型)变量名   高->低
        //自动转换  低->高
        System.out.println(i);
        System.out.println(b);

        /*
        注意点:
        1. 不能把布尔值进行转换
        2. 不能把对象类型转换为不相干的类型
        3. 在把高容量转换到低容量的时候,强制转换
        4. 转换的时候可能存在内存溢出,或者精度问题!
         */

        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;
        int years = 20;
        int total = money*years;//-1474836480  ,计算的时候移除了
        long total2 = money *years; //默认是int,转换前已经存在溢出了
        System.out.println(total);

        long total3 = money*((long)years);//先把一个数转换为long
        System.out.println(total3);

        //L l
        
    }
}
  1. 变量

    • 变量:可以变化的量!

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

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

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

      • 每个变量都有类型,类型可以是基本类型,也可以是引用类型。
      • 变量名必须是合法的标识符。
      • 变量声明是一条完整的语句,因此每一个声明都必须以分号结束。
    • 变量的作用域

      • 类变量
      • 实例变量
      • 局部变量
      public class Variable{
      	static int allClinks = 0; //类变量
      	String str = "hello world";//实例变量
      	
      	public void method(){
      		int i=0; //局部变量
      }
      }
      
public class HelloWorld {

    //类变量 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);

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

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

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

}
  1. 常量

    • Constant :初始化后不会再改变值

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

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

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

运算符

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

    }




}

package operator;

public class Demo02 {
    public static void main(String[] args) {
        long a = 12341545324563L;
        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
    }
}
package operator;

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);
        System.out.println(a<b);
        System.out.println(a==b);
        System.out.println(a!=b);
//        false
//        true
//        false
//        true
    }
}

package operator;

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

        // a = a + 1;
        int c = ++a;   // 执行完这行代码前,先自增,再给c赋值

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

        // 幂运算 2^3 2*2*2 = 8    很多运算,我们会使用一些工具类来操作
        double pow = Math.pow(2,3);
        System.out.println(pow);
    }
}
package operator;
//逻辑运算符
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||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);

    }
}

package 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  最快计算2*8  位运算
        <<左移  *2
        >>右移  /2
         */

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


package operator;

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

        a+=b;
        System.out.println(a);

        //字符串连接符  + ,String

        System.out.println("" + a + b);
        //3020  先连接了所以不运算
        System.out.println(a + b + "");
        //50   先运算再连接
    }
}

package operator;

//三元运算符

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

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

包机制

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

  • 包语句的语法格式为:

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

    com.wang.xxx

  • 为了能使用某一个包的成员,我们需要在Java程序中明确导入该包。

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

JavaDoc

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

  • 参数信息

    • @author 作者名
    • @version 版本号
    • @since 指明需要最早使用的jdk版本
    • @param 参数名
    • @return 返回值情况
    • @throws 异常抛出情况
    package com.jiang.base;
    
    /**
     * @author  jiangxinyu
     * @version  1.0
     * @since 1.8
     *
     */
    
    public class Doc {
        String name;
    
        /**
         *
         * @param name
         * @return
         * @throws Exception
         */
        public  String test(String name) throws  Exception{
            return name;
        }
    
    }
    
    
  • 命令行生成