java基础

Java基础

注释

书写注释是很重要的过程,是一个很好的习惯,注释并不会被执行。

注释有三种

  1. 单行注释:用 // 表示
  2. 多行注释:用 /* */表示
  3. 文档注释:用//**/完成的,可以配置一系列参数
public class Helloworld {
    public static void main(String args[]){
        // 单行注释
        // 输出Hello world!
        System.out.println("Hello world!");
        /* 多行注释:
           能注释一段文字*/
    }
}
    /**文档注释
     * @Description:Helloworld!
     * @Author:Atlanties
     */

标识符

关键字

有一系列的关键字,同其他语言一样。

image-20210910230350484

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

标识符注意点

  • 所有的标识符都应该以字母,美元符$,下划线_,开始
  • 首字母之后可以是任意字母,任意符号
  • 不能用关键字作为变量名或方法名
  • 标识符是大小写敏感的

数据类型

强类型语言

  • 要求变量的使用严格符合规定,所有变量都必须先定义后使用,安全性高,
  • 因此速度就慢
    java,c

弱类型语言

python

  • 可以随便定义
  • 但是安全性不够

java的数据类型分为两大类

  1. 基本类型(primitive type)
  • 整数类型(byte 8 bit, short 16 bit, int 32 bit, long 64 bit)
  • 浮点类型(float 32 bit, double 64 bit)
  • 字符类型 (char 16 bit)
  • 布尔类型 (bool 占 1位)
  1. 引用类型(reference type)
    除了以上八个类型以外都是引用类型。
    类,接口,数组;
public class Demo03 {
    public static void main(String args[]){
        String a = "10";
        int num = 10;//最常用
        byte num2 = 20;
        int num3 = 500;
        long num4 = 30L; //long类型一般在数字后加L
        float num5 = 3.14F;//float类型要在小数后加F
        double num6 = 3.1415926;

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

        boolean flag = true;//表示是非

        //实际上各个类型都是用类组织的
        System.out.println(a);
    }
}

类型转换

public class Demo05 {
    public static void main(String args[]){
        int i = 128;
        byte j = (byte)i;// 内存溢出
        //自动转换:低->高
        //强制转换:高->低
        System.out.println(j);

        double k = i;
        System.out.println(k);
        /**
         * 1.不能转换布尔类型
         * 2.不能转换为不相干的类型
         * 3.把高容量转换为低容量会截取
         * 4.有可能出现溢出或者精度问题
         */
        char a = 'a';
        int b = a + 1;
        System.out.println(b);
        System.out.println((char)b);
    }
}
public class Demon06 {
    public static void main(String[] args) {
        //操作比较大的数会出现溢出
        //新特性:可用_划分数字
        int money = 10_0000_0000;
        int year = 20;
        System.out.println(money*year);//溢出
        System.out.println((long)money*year);
    }
}

变量,常量,作用域

  1. 变量
    • 变量是可变化的量。
    • java是强类型语言,每个变量都要声明其类型
    • 变量是最基本的存储单元,要素包含变量名,变量类型和作用域
	type varName [= value]{,varName[=value]}
	//数据类型 变量名=值;可以用逗号来声明多个同类型变量
  • 注意事项:
    变量可以是基本类型也可以是引用类型;
    标识符必须合法;
    变量声明必须以分号结束
  1. 作用域
    • 类变量
    • 实例变量
    • 局部变量
	public class Demon08 {
    //类变量
    static double salary = 2500;
    //实例变量,从属于对象,不进行初始化会赋值为默认值
    //布尔值默认为false,除了基本类型其余默认值均为NULL
    String name;
    int age;
    //main 方法
    public static void main(String[] args) {
        //局部变量,使用前必须声明和初始化
        int i = 0;
        System.out.println(i);

        Demon08 demon08 = new Demon08();
        System.out.println(demon08.name);
        System.out.println(demon08.age);

        System.out.println(salary);//无需new对象
    }
    //其他方法
    public void add(){

    }
}
  1. 常量
    • 常量化:final修饰,一旦初始化就不能再改变
    • 常量就是特殊的变量,设置值以后就不允许改变
    • 常量名一般使用大写字母
	public class Demon09 {
    static final double PI = 3.14;
    final static double PI1 = 3.14;
    public static void main(String[] args) {
        System.out.println(PI);
    }
}
  1. 命名原则
    • 方法和变量首字母小写驼峰原则
    • 常量全部大写用下划线分隔
    • 类名首字母大写驼峰原则

运算符

  1. java支持以下运算符
    • 算术运算符:+,-,*,/,++,--
    • 赋值运算符:=
    • 关系运算符:>,<,>=,<=,==,!=,instanceof
    • 逻辑运算符:&&,||,!
    • 位运算符:&,|,^,>>,<<,>>>
    • 条件运算符:? :
    • 扩展赋值运算符:+=,-=,*=,/=
public class Demon01 {
    public static void main(String[] args) {
        //二元运算符
        int a = 10;
        int b = 15;
        int c = 20;
        int d = 25;

        System.out.println(a+b);
        System.out.println(a-b);
        System.out.println(a*b);
        System.out.println(a/(double)b);//注意类型转换
    }
}
public class Demon02 {
    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);//强制转换
        System.out.println(b+c+d);
        System.out.println(c+d);
    }
}
public class Demon03 {
    public static void main(String[] args) {
        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);
        System.out.println(c%a);
    }
}
  1. 自增运算符
    • ++/--
public class Demon04 {
    public static void main(String[] args) {
        // ++,--自增自减运算符
        int a = 3;
        int b = ++a;//先自增再赋值
        int c = a++;//先赋值再自增
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);

        //幂运算 2^3 = 2*2*2 使用一些工具类去实现
        System.out.println(Math.pow(2,3));
    }
}
  1. 逻辑运算符,位运算符和条件运算符
    • 与或非
   public class Demon05 {
    public static void main(String[] args) {
        //逻辑运算符,与或非
        boolean a = true;
        boolean b = false;
        System.out.println("a && b :" + (a&&b));
        System.out.println("a || b :" + (a||b));
        System.out.println("!a :" + (!a));
   
        //短路运算:对于a&&b,如果b为false就不会继续计算
        int c = 5;
        System.out.println((a&&b)&&(c++>5));
        System.out.println(c);
    }
   }
  • 位运算
   public class Demon06 {
    public static void main(String[] args) {
        //位运算:按位运算
        /*
        a = 1001
        b = 1010
        a&b = 1000
        a|b = 1011
        a^b = 0011
         */
        System.out.println(2<<3);
    }
   }
  1. 扩展运算符
public class Demon07 {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        a += b;
        System.out.println(a);
        a -= b;
        System.out.println(a);
        //字符串连接符
        //只要有一个是string就会转换为string
        System.out.println(""+a+b);//1020
        System.out.println(a+b+"");//30
        //字符串连接符号有优先级
    }
}
  1. 三元运算符
public class Demon08 {
    public static void main(String[] args) {
        // x? y: z
        int score = 80;
        String res = score < 60 ? "不及格" : "及格";
        System.out.println(res);
    }
}

运算符有优先级,通过加括号来实现。

包机制

  1. 为了更好组织类,Java提供了包机制,用于区别类名的命名空间。
  2. 包语句的语法为:
    package pkg1[. pkg2[. pkg3...]]
  3. 一般使用公司域名倒置作为包名
  4. 为了能够使用一个包的成员,我们需要在Java程序中导入该报,import
  5. 尽量不要让名字重复

Doc文档

  1. javadoc命令是用来生成自己的API文档
  2. 参数信息
package operator;

/**
 * @author Jhon Smith
 * @version 1.0
 * @since 1.8
 * @param
 * @throws
 * @return
 */
public class Demon09 {

    String name;
    public String test(String name) throws Exception{
        return name;
    }
}
  1. 生成文档
    javadoc -encoding utf-8 charset utf-8 xx.java
posted @ 2021-10-12 23:36  黑衣の甘铃儿  阅读(40)  评论(0)    收藏  举报