Day02

跟随狂神学Java Day2

注释

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

关键词与标识符

关键字(Keyword)

是编程语言预先定义好的、具有特定含义和功能的保留词汇,也称为 “保留字”。

标识符:

1.定义:表名、类名、变量名等等名称符号就叫标识符

abstract boolean break byte case
catch char const class continue
default do double else extends
final finally float for goto
if implements import instanceof int
private long native new package
static protected public return short
throw strictfp super switch this
volatile throws transient try void
interface while synchronized

2.特点:

首字母必须是以字母、$、_ 开始的

之后可以是字母、$、_ 、数字

字母大小写敏感

不能以标识符为名


数据类型

  • 强类型语言
    • 要求变量的使用要严格符合规定,所有变量都必须先定义后才能使用。
  • 弱类型语言
  • 数据类型基本分类
    • 基本类型
      • 数值类型
        • 整数类型
          • byte占1字节,范围:-128 - 127
          • short占2字节,范围: -2^15 - 2^15-1
          • int占4字节,范围: -2^31 - 2^31 - 1
          • long占8字节,范围: -2^63 - 2^63-1
        • 浮点类型
          • float占4字节
          • double占8字节
        • 字符类型:char占2字节
      • boolean类型:占1位其值只有tue和fase两个。
    • 引用类型
      • 接口
      • 数组

Java属于强类型语言

八大基本类型

public class Demo01 {
    public static void main(String[] args) {
       //八大基本数据类型
        //整数
        int num1=10;
        byte num2=20;
        short num3=30;
        long num4=40L;//long类型后面用L来区分
        //浮点型
        float num5=50.1F;//float类型要在数字后面加F
        double num6=60.11723378742672;
        //字符类
        char ch1='A';
        //字符串
        String name="John";
        //字符串,string不是关键字,而是类
        //布尔值
        boolean bool1=true;
        boolean bool2=false;
    }
}

public class Demo02 {
    public static void main(String[] args) {
        int i = 128;
        byte b = (byte)i;   // 内存溢出
        double b1 = i;

        // 强制转换     (类型)变量名     高--低
        // 自动转换     低--高

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

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

        System.out.println("++++++++++++++++++++++++++++++++++++");

        System.out.println((int)123.7);
        System.out.println((int)-45.89f);

        System.out.println("=======================");
        char a = 'a';
        int c = a+1;

        System.out.println(c);  // 98
        System.out.println((char)c);    // b

    }
}

拓展:

public class Demo03 {
    public static void main(String[] args) {
        //整数拓展: 进制 二进制0b 十进制日常 八进制0 十六进制0x
        int i=10;
        int i2=010;//八进制0
        int i3=0x10;//十六进制0x
        System.out.println(i);
        System.out.println(i2);
        System.out.println(i3);
        System.out.println("--------------");
        //浮点数拓展
        //float 有限 离散的 ,所以对于银行业务不能用浮点类型来完成,可以使用类的功能
        float f = 0.1f; // 0.1
        double d = 1.0/10;  // 0.1

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

        float d1 = 2425444564215654564f;
        float d2 = d1 + 1;

        System.out.println(d1==d2); // 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   2字节    0 - 65536   Excel

        char c3 = '\u0061';

        System.out.println(c3); // a
        System.out.println("--------------");
        //转义字符
        // \t 制表符
        // \n 换行符
        System.out.println("hello\tworld");
        System.out.println("hello\nworld");
        System.out.println("--------------");
        //布尔值拓展
        boolean flag = true;

        if(flag==true){}    // 新手
        if(flag){}      // 老油条

        // 代码要精简易读

    }
}

变量与常量

变量:

  • 变量:可以变化的量。

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

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

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

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

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

    public class Demo04 {
        static int allClicks =0; //类变量
        String str="hello world";//实例变量
        public  void method(){
            int i;//局部变量 
        }
    
        public static void main(String[] args) {
            /*
            int a,b,c;对于规范一般不这么写定义
            应该是int a;
            int b;
            int c;
    
             */
            String name="wahaha";
            System.out.println(name);
            Demo04 str=new Demo04();
            System.out.println(str.str);
        }
    }
    
    

    变量的命名规范

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

    • 类成员变量:首字母小和驼峰原则:monthSalary;

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

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

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

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


常量:

public class Demo08 {
    
    // 修饰符,不存在先后顺序
    static final double PI = 3.14;
    final static double PI = 3.14;

    public static void main(String[] args) {
        System.out.println(PI);
    }
}

常量修饰符:final

基本运算符

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

  • 赋值运算符: =

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

  • 逻辑运算符:&&,‖,!

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

  • 条件运算符: ?:

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

package operation;

public class Demo1 {
    public static void main(String[] args) {
        //二元运算
        int a=10;
        int b=20;
        System.out.println(a+b);
        System.out.println(a-b);
        System.out.println(a*b);
        System.out.println(a/b);
        //除法运算失败,要整数除整数得变成浮点型
        System.out.println(a/(double)b);
    }
}

package operation;

public class Demo2 {
    public static void main(String[] args) {
        long a=1234567890L;
        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);
        System.out.println((double)c+d);
    }
}

package operation;

public class Demo03 {
    public static void main(String[] args) {
        int a =10;
        int b =20;
        int c =21;
        System.out.println(c%a);
        System.out.println(a>b);
        System.out.println(a<b);
        System.out.println(a==b);
        System.out.println(a!=b);
    }
}

一元运算、math函数

package operation;

public class Demo04 {
    public static void main(String[] args) {
        //一元运算 ++ --
        int a = 3;
        int b = a++; //执行完这行代码后,先把a值赋值给b,再自增
        System.out.println(b);
        System.out.println(a);
        int c = ++a;//这个是先字增,再赋值
        System.out.println(c);
        System.out.println(a);
        //幂运算 2^3 在Java中会大量使用math函数
        double pow =Math.pow(2,3);
        System.out.println(pow);
    }
}

逻辑运算、位运算

package operation;

public class Demo05 {
    public static void main(String[] args) {
        //逻辑运算符
        //and(&&) or(||) 取反(!)
        boolean a = true;
        boolean b = false;
        System.out.println("a&&b=" + (a&&b));
        System.out.println("a||b=" + (a||b));
        System.out.println("!(a&&b)=" + !(a&&b) );
        //短路运算
        int c=5;
        boolean d = (c<4)&&(c++<4);//在运算c<4时已经返回false结束进程了
        System.out.println(d);
        System.out.println(c);

    }
}

package operation;

public class Demo06 {
    public static void main(String[] args) {
        /*
        A=0011 1100
        B=0000 1101
        --------------
        A&B=0000 1100 全1出1,否则为0
        A&B=0011 1101 有1出1,全0出0
        A^B=0011 0001 异出1,同出0
        ~B=1111 0010 取反
        面试题如何快速计算2*8=16
        << *2
        >> /2
         */
        System.out.println(2<<3);
    }
}

三元运算 x?y:z

package operation;

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

javadoc

  • javadoc命令是用来生成自己API文档的。
  • 参数信息
    • @ author作者名
    • @ version版本号
    • @ since指明需要最早使用的jdk版本
    • @ paran参数名
    • @ return返回值情况
    • @ throws异常抛出情况
package operation;

/**
 * @author xiaobai
 * @version 1.0
 * since 1.8
 */
public class JavaDoc {
    String name;

    /**
     * 
     * @param name
     * @return
     * @throws Exception
     */
    public String test(String name) throws Exception{
        return name;
    }
    public static void main(String[] args) {

    }
}

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

posted @ 2025-11-02 20:48  爱小白的小黑  阅读(4)  评论(0)    收藏  举报