1注释,标识符,关键字

注释(commnets)

写给自己或他人看,不会被执行。养成写注释良好习惯,拼写注意规范

IDEA 快捷键 shift +ctrl

单行注释

双斜杆//

  1. public class helloword {
  2. public static void main(String[] args) {
  3. System.out.println("helloword!");//注释不会被执行
  4. }
  5. }

多行注释

/
/

  1. public class helloword {
  2. public static void main(String[] args) {
  3. System.out.println("helloword!");
  4. }/*多行注释*/
  5. }

文档注释

/*

*/

  1. public class helloword {
  2. public static void main(String[] args) {
  3. System.out.println("helloword!");
  4. }
  5. }
  6. /**
  7. *@Description helloword
  8. *@Auther er0s10n
  9. */

ps.彩蛋:

关键字

类名,变量名,方法名

abstract assert boolean break byte
case catch char class const
continue default do double else
enum extends final finally float
for goto if implements import
instanceof int interface long native
new package private protected public
return strictfp short static super
switch synchronized this throw throws
transient try void volatile while
关键字 含义
abstract 表明类或者成员方法具有抽象属性
assert 断言,用来进行程序调试
boolean 基本数据类型之一,声明布尔类型的关键字
break 提前跳出一个块
byte 基本数据类型之一,字节类型
case 用在switch语句之中,表示其中的一个分支
catch 用在异常处理中,用来捕捉异常
char 基本数据类型之一,字符类型
class 声明一个类
const 保留关键字,没有具体含义
continue 回到一个块的开始处
default 默认,例如,用在switch语句中,表明一个默认的分支。Java8 中也作用于声明接口函数的默认实现
do 用在do-while循环结构中
double 基本数据类型之一,双精度浮点数类型
else 用在条件语句中,表明当条件不成立时的分支
enum 枚举
extends 表明一个类型是另一个类型的子类型。对于类,可以是另一个类或者抽象类;对于接口,可以是另一个接口
final 用来说明最终属性,表明一个类不能派生出子类,或者成员方法不能被覆盖,或者成员域的值不能被改变,用来定义常量
finally 用于处理异常情况,用来声明一个基本肯定会被执行到的语句块
float 基本数据类型之一,单精度浮点数类型
for 一种循环结构的引导词
goto 保留关键字,没有具体含义
if 条件语句的引导词
implements 表明一个类实现了给定的接口
import 表明要访问指定的类或包
instanceof 用来测试一个对象是否是指定类型的实例对象
int 基本数据类型之一,整数类型
interface 接口
long 基本数据类型之一,长整数类型
native 用来声明一个方法是由与计算机相关的语言(如C/C++/FORTRAN语言)实现的
new 用来创建新实例对象
package 包
private 一种访问控制方式:私用模式
protected 一种访问控制方式:保护模式
public 一种访问控制方式:共用模式
return 从成员方法中返回数据
short 基本数据类型之一,短整数类型
static 表明具有静态属性
strictfp 用来声明FP_strict(单精度或双精度浮点数)表达式遵循IEEE 754 754)算术规范
super 表明当前对象的父类型的引用或者父类型的构造方法
switch 分支语句结构的引导词
synchronized 表明一段代码需要同步执行
this 指向当前实例对象的引用
throw 抛出一个异常
throws 声明在当前定义的成员方法中所有需要抛出的异常
transient 声明不用序列化的成员域
try 尝试一个可能抛出异常的程序块
void 声明当前成员方法没有返回值
volatile 表明两个或者多个变量必须同步地发生变化
while 用在循环结构中

标识符

  • 所有标识符以字母(A-Z或a-z),美元符号($),或者下划线(_)开始
  • 首字母以后可以用数字
  • 不能以关键字作为变量名,方法名
  • 标识符大小写敏感
  • 合法字符实例:age ,$salary, _value
  • 非法示例 : 123abc; -salery, # abc
  • 可以用中文,但不建议
  • 不能使用拼音

2数据类型

强类型语言

要求变量使用要严格符合规定,所有变量先命名后才能使用

弱类型语言

弱类型比如 js

  1. public class helloword {
  2. public static void main(String[] args) {
  3. String a="helloword";
  4. System.out.println(a);
  5. }
  6. }

变量:可以变化的量

  • Java是强类型语言,每个变量必须声明类型。
  • Java变量是Java程序中最基本的存储单元,其要素包括变量名,变量类型和作用域
    type varName [=value] [{,varName[=value]}];
    //数据类型 变量名=值;

Java数据类型分为两大类

  • 基本类型(primitive type)
  • 引用型(reference type)
    Java基础语法 - 图1

Java基础语法 - 图2

基本类型

public class helloword {
    public static void main(String[] args) {
        //八大数据类型
        //整数
        int num1=10;//最常用
        byte num2=20;
        short num3=30;
        long num4=1000L; //long后要加L
        //小数
        float num5=50.1F; //float后要加F
        double num6=3.1415926;
        //字符型
        char name="A";
        //字符串,string不是关键字,是类
        String namea="陈子昱";
        //boolean
        boolean flag=true;



    }
}

整数拓展

public class helloword {
    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);
    }
}

浮点数拓展

浮点数是有限的,计算机无法精确计算,只能在一定长度截取近似值

public class helloword {
    public static void main(String[] args) {
        float f=0.1f;
        double d=1.0/10;
        System.out.println(f==d);//false
        System.out.println(f);//0.1
        System.out.println(d);//0.1
        //===================================
        float d1=23223335551615165165511233322f;
        float d2=d1+1;
        System.out.println(d1==d2);//true

    }
}

应该避免浮点数之间的比较

银行使用BigDecimal

银行怎么表示? 钱不能有误差

BigDeciaml 数学工具类

字符拓展

[字符集](https://baike.baidu.com/item/%E5%AD%97%E7%AC%A6%E9%9B%86)中的[字符](https://baike.baidu.com/item/%E5%AD%97%E7%AC%A6/4768913)[编码](https://baike.baidu.com/item/%E7%BC%96%E7%A0%81)为指定[集合](https://baike.baidu.com/item/%E9%9B%86%E5%90%88)中某一[对象](https://baike.baidu.com/item/%E5%AF%B9%E8%B1%A1/17158)(例如:[比特](https://baike.baidu.com/item/%E6%AF%94%E7%89%B9/3431582)模式、[自然数](https://baike.baidu.com/item/%E8%87%AA%E7%84%B6%E6%95%B0/385394)[序列](https://baike.baidu.com/item/%E5%BA%8F%E5%88%97/1302588)、8位组或者[电脉冲](https://baike.baidu.com/item/%E7%94%B5%E8%84%89%E5%86%B2/10706490)),以便[文本](https://baike.baidu.com/item/%E6%96%87%E6%9C%AC/5443630)在[计算机](https://baike.baidu.com/item/%E8%AE%A1%E7%AE%97%E6%9C%BA/140338)中存储和通过[通信](https://baike.baidu.com/item/%E9%80%9A%E4%BF%A1)[网络](https://baike.baidu.com/item/%E7%BD%91%E7%BB%9C)的传递

字符编码

eg.

public class helloword {
    public static void main(String[] args) {
        char c1='a';
        char c2='中';
        char c3='\u0061'//unicode码
        System.out.println((int)c1);//97
        System.out.println((int)c2);//20013
        System.out.println(char3);//a

    }//强制转换
}

字符本质还是数字

Unicode编码 2个字节 0-65536

ASCII 编码

Java基础语法 - 图3

特殊字符

转义字符 意义 ASCII码值(十进制)
\a 响铃(BEL) 007
\b 退格(BS) ,将当前位置移到前一列 008
\f 换页(FF),将当前位置移到下页开头 012
\n 换行(LF) ,将当前位置移到下一行开头 010
\r 回车(CR) ,将当前位置移到本行开头 013
\t 水平制表(HT) (跳到下一个TAB位置) 009
\v 垂直制表(VT) 011
\ 代表一个反斜线字符’’’ 092
‘ 代表一个单引号(撇号)字符 039
“ 代表一个双引号字符 034
? 代表一个问号 063
\0 空字符(NUL) 000
\ddd 1到3位八进制数所代表的任意字符 三位八进制
\xhh 十六进制所代表的任意字符 十六进制

内存拓展

String sa=new String(original:"helloword");
String sb=new String(original:"helloword");
System.out.println(sa==sb);//false  内存地址不同

boolean 扩展

boolean flag=true;
if (flag==true){}//新手
if (flag){}//老手  
//两句都相同 less is more 代码要精简易读

3类型转换

java是强类型语言,所以要进行有些运算时,需要用到类型转换。

运算中,不同类型的数据先转为同一类型,然后进行运算。

eg. (类型)变量名

低———————————————————>高

  • byte,shout,char—->int—>long—>float—>double
  • 不能转化 boolen性
  • 高容量转低容量要强制转换,且有精度,内存溢出问题

强制类型转换

大容量转小容量要强制转换

public class helloword {
    public static void main(String[] args) {
        int i=128;
        byte b=(byte)i;
        System.out.println(i);//128
        System.out.println(b);//-128//溢出

        System.out.println((int)23.7);//23//精度丢失
        System.out.println((int)-45.89f)

    }
}

自动类型转换

低到高则自动转换

public class helloword {
    public static void main(String[] args) {
        char c='a';
        int d=c+1;
        System.out.println(d);
    }
}

溢出问题

操作大数字时要注意溢出问题

long 中带l时防止看成1 应该用大写L

public class helloword {
    public static void main(String[] args) {
        int money=1000000000;
        int years=20;
        int total1=years*money;
        long total2=years*money;
        System.out.println(total);//-1474836480
        System.out.println(total2);//-1474836480
        //======================================
        long total3=money*((long)years);
        System.out.println(total3);//20000000000
    }
}

4变量,常量

变量

  • 变量:可以变化的量! ,一段内存空间
  • Java是强类型语言,每个变量必须声明类型
  • java变量是程序中最基本的存储单元,要素包括变量名,变量类型和基本作用域
type varName [=value] [{,varName[=value]}];
//数据类型  变量名=值; 可以用逗号隔开来声明多个同类型变量

注意事项

  • 每个变量都有基本类型,类型可以是基本类型,也可以是引用类型
  • 变量名必须是合法标识符
  • 变量声明是一条完整语句,因此每一个声明必须以分号结束
public class helloword {
    public static void main(String[] args) {
        int a,b,c;
        int d=1,e=2,f=3;//程序可读性
        String name="chenziyu";
        char x='x';
        double pi=3.14;
    }
}

作用域

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

局部变量只在方法中

public class demo {
    //属性:变量

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

    //类变量staic  ;类变量从属于类
    static double salary=2500;

    //main 方法
    public static void main(String[] args) {
        //局部变量,必须声明和初始化
        int i=10;
        System.out.println(i);

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

        //类变量
        System.out.println(salary);
    }
    //其他方法
    public void add(){

    }
}

常量

  • 常量(constant):初始化(initialize)之后不会再改变的值
  • 所谓常量可以理解为一种特殊变量,它的值被设定后,在程序运行中不允许被改变
  • 常量一般用大写字符
final 常量名=值;
final double Pi=3.14;

命名规范

  • 所有变量,方法,类名:见名知意
  • 类成员变量:首字母小写和驼峰原则:motherSalary 除了第一个单词以外,后面单词首字母大写
  • 局部变量:首字母小写和驼峰原则
  • 常量:大写字母和下划线 MAX_VALUE
  • 类名 :首字母大写和驼峰原则: Man, GoodMan
  • 方法名:首字母小写和驼峰原则:run(),runRun()

5运算符

基本运算符

  • 算数运算符:+, - ,* , / , % , ++ ,—
  • 赋值运算符 =
  • 关系运算符 > , < , >= ,<= ,== ,!=,instanceof
  • 逻辑运算符 :&& , ||, !
  • 位运算符 & ,| ,^ , ~ ,>> ,<< ,>>>
  • 条件运算符 ? :
  • 扩展赋值运算符 : += ,-= ,*= , /=
public class demo2 {
    public static void main(String[] args) {
        //二元运算符
        int a=10;
        int b=10;
        long c=123895849298292499L;
        short d=10;
        byte e=8;

        System.out.println(a+b);
        System.out.println(a-b);
        System.out.println(a*b);
        System.out.println(a/(double)b);
        System.out.println(e+b+c+d);
        System.out.println(c+d+e);
        System.out.println(d+e);
/*
20
0
100
1.0
123895849298292527
123895849298292517
18
*/
    }


}

关系运算符

public class demo2 {
    public static void main(String[] args) {

        int a=10;
        int b=20;
        int c=11;
        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);
    }

}

自增运算符,初识math类

一元运算符

// ++  --
public class demo2 {
    public static void main(String[] args) {
        int a=3;
        int b=a++; //执行代码后,先给b赋值,再自增
        //a=a+1
        System.out.println(a);

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

        //幂运算 math类包含工程计算的函数
        double pow=Math.pow(2,3);
        System.out.println(pow);
    }


}

逻辑运算符,位运算符,条件运算符

逻辑运算符

public class demo2 {
    public static void main(String[] args) {
    //与 (and) 或(or  非(not)
        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));


    }


}

位运算符

public class demo2 {
    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 怎么运算最快  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
         */
        System.out.println(2<<3);


    }
}

条件运算符

x ? y : z

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

    }
}

扩展赋值运算符

public class demo2 {
    public static void main(String[] args) {
        int a=20;
        int b=10;
        a+=b;//a=a+b
        a-=b;//a=a-b
        System.out.println(a);
        //字符串连接符
        System.out.println(a+b+"");//进行加法  30
        System.out.println(""+a+b);//进行拼接  2010

    }
}

运算符优先级

优先级 运算符 结合性
1 ()、[]、{} 从左向右
2 !、+、-、~、++、— 从右向左
3 *、/、% 从左向右
4 +、- 从左向右
5 «、»、>>> 从左向右
6 <、<=、>、>=、instanceof 从左向右
7 ==、!= 从左向右
8 & 从左向右
9 ^ 从左向右
10 | 从左向右
11 && 从左向右
12 || 从左向右
13 ?: 从右向左
14 =、+=、-=、*=、/=、&=、|=、^=、~=、«=、»=、>>>= 从右向左

6常用类

7IO

8集合

9包机制,JavaDoc

包机制

包机制(这玩意就是文件夹)

  • 为了更好的组织类,java提供了包机制,用于区别类名的命名空间。
  • 包语句格式为:java package pkg1[.pkg2[.pkg3...]];

  • 一般用公司域名倒置作为包名 ;top.chenziyu.ctf

  • 为了能使用某一个包的成员,我们需要在Java程序中明确导入该包。使用import 语句完成该功能```java import package1[.package2…].(classname|*);

    ```java
    package com.chennziyu.ctf;
    //包路径
    import java.util.Date;//导入日期包
    import com.chennziyu.blogs.blogs; //导入自己的包
    import com.baidu.*; //导入全部包
    public class demo {
      public static void main(String[] args) {
    
          System.out.println();
      }
    }
    

javaDoc

package com.chennziyu.ctf;

/**
 * @author 陈子昱
 * @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;

    }
}
javadoc -encodeing UTF-8 -charset UTF-8 doc.java
//文件夹下生成 index.html 自动生成文档

idea生成java doc文档

补充ascii

ASCII码表具体如下所示 [1] :

Bin (二进制) Oct (八进制) Dec (十进制) Hex (十六进制) 缩写/字符 解释
0000 0000 00 0 0x00 NUL(null) 空字符
0000 0001 01 1 0x01 SOH(start of headline) 标题开始
0000 0010 02 2 0x02 STX (start of text) 正文开始
0000 0011 03 3 0x03 ETX (end of text) 正文结束
0000 0100 04 4 0x04 EOT (end of transmission) 传输结束
0000 0101 05 5 0x05 ENQ (enquiry) 请求
0000 0110 06 6 0x06 ACK (acknowledge) 收到通知
0000 0111 07 7 0x07 BEL (bell) 响铃
0000 1000 010 8 0x08 BS (backspace) 退格
0000 1001 011 9 0x09 HT (horizontal tab) 水平制表符
0000 1010 012 10 0x0A LF (NL line feed, new line) 换行键
0000 1011 013 11 0x0B VT (vertical tab) 垂直制表符
0000 1100 014 12 0x0C FF (NP form feed, new page) 换页键
0000 1101 015 13 0x0D CR (carriage return) 回车键
0000 1110 016 14 0x0E SO (shift out) 不用切换
0000 1111 017 15 0x0F SI (shift in) 启用切换
0001 0000 020 16 0x10 DLE (data link escape) 数据链路转义
0001 0001 021 17 0x11 DC1 (device control 1) 设备控制1
0001 0010 022 18 0x12 DC2 (device control 2) 设备控制2
0001 0011 023 19 0x13 DC3 (device control 3) 设备控制3
0001 0100 024 20 0x14 DC4 (device control 4) 设备控制4
0001 0101 025 21 0x15 NAK (negative acknowledge) 拒绝接收
0001 0110 026 22 0x16 SYN (synchronous idle) 同步空闲
0001 0111 027 23 0x17 ETB (end of trans. block) 结束传输块
0001 1000 030 24 0x18 CAN (cancel) 取消
0001 1001 031 25 0x19 EM (end of medium) 媒介结束
0001 1010 032 26 0x1A SUB (substitute) 代替
0001 1011 033 27 0x1B ESC (escape) 换码(溢出)
0001 1100 034 28 0x1C FS (file separator) 文件分隔符
0001 1101 035 29 0x1D GS (group separator) 分组符
0001 1110 036 30 0x1E RS (record separator) 记录分隔符
0001 1111 037 31 0x1F US (unit separator) 单元分隔符
0010 0000 040 32 0x20 (space) 空格
0010 0001 041 33 0x21 ! 叹号
0010 0010 042 34 0x22 “ 双引号
0010 0011 043 35 0x23 # 井号
0010 0100 044 36 0x24 $ 美元符
0010 0101 045 37 0x25 % 百分号
0010 0110 046 38 0x26 & 和号
0010 0111 047 39 0x27 ‘ 闭单引号
0010 1000 050 40 0x28 ( 开括号
0010 1001 051 41 0x29 ) 闭括号
0010 1010 052 42 0x2A * 星号
0010 1011 053 43 0x2B + 加号
0010 1100 054 44 0x2C , 逗号
0010 1101 055 45 0x2D - 减号/破折号
0010 1110 056 46 0x2E . 句号
0010 1111 057 47 0x2F / 斜杠
0011 0000 060 48 0x30 0 字符0
0011 0001 061 49 0x31 1 字符1
0011 0010 062 50 0x32 2 字符2
0011 0011 063 51 0x33 3 字符3
0011 0100 064 52 0x34 4 字符4
0011 0101 065 53 0x35 5 字符5
0011 0110 066 54 0x36 6 字符6
0011 0111 067 55 0x37 7 字符7
0011 1000 070 56 0x38 8 字符8
0011 1001 071 57 0x39 9 字符9
0011 1010 072 58 0x3A : 冒号
0011 1011 073 59 0x3B ; 分号
0011 1100 074 60 0x3C < 小于
0011 1101 075 61 0x3D = 等号
0011 1110 076 62 0x3E > 大于
0011 1111 077 63 0x3F ? 问号
0100 0000 0100 64 0x40 @ 电子邮件符号
0100 0001 0101 65 0x41 A 大写字母A
0100 0010 0102 66 0x42 B 大写字母B
0100 0011 0103 67 0x43 C 大写字母C
0100 0100 0104 68 0x44 D 大写字母D
0100 0101 0105 69 0x45 E 大写字母E
0100 0110 0106 70 0x46 F 大写字母F
0100 0111 0107 71 0x47 G 大写字母G
0100 1000 0110 72 0x48 H 大写字母H
0100 1001 0111 73 0x49 I 大写字母I
01001010 0112 74 0x4A J 大写字母J
0100 1011 0113 75 0x4B K 大写字母K
0100 1100 0114 76 0x4C L 大写字母L
0100 1101 0115 77 0x4D M 大写字母M
0100 1110 0116 78 0x4E N 大写字母N
0100 1111 0117 79 0x4F O 大写字母O
0101 0000 0120 80 0x50 P 大写字母P
0101 0001 0121 81 0x51 Q 大写字母Q
0101 0010 0122 82 0x52 R 大写字母R
0101 0011 0123 83 0x53 S 大写字母S
0101 0100 0124 84 0x54 T 大写字母T
0101 0101 0125 85 0x55 U 大写字母U
0101 0110 0126 86 0x56 V 大写字母V
0101 0111 0127 87 0x57 W 大写字母W
0101 1000 0130 88 0x58 X 大写字母X
0101 1001 0131 89 0x59 Y 大写字母Y
0101 1010 0132 90 0x5A Z 大写字母Z
0101 1011 0133 91 0x5B [ 开方括号
0101 1100 0134 92 0x5C \ 反斜杠
0101 1101 0135 93 0x5D ] 闭方括号
0101 1110 0136 94 0x5E ^ 脱字符
0101 1111 0137 95 0x5F _ 下划线
0110 0000 0140 96 0x60 ` 开单引号
0110 0001 0141 97 0x61 a 小写字母a
0110 0010 0142 98 0x62 b 小写字母b
0110 0011 0143 99 0x63 c 小写字母c
0110 0100 0144 100 0x64 d 小写字母d
0110 0101 0145 101 0x65 e 小写字母e
0110 0110 0146 102 0x66 f 小写字母f
0110 0111 0147 103 0x67 g 小写字母g
0110 1000 0150 104 0x68 h 小写字母h
0110 1001 0151 105 0x69 i 小写字母i
0110 1010 0152 106 0x6A j 小写字母j
0110 1011 0153 107 0x6B k 小写字母k
0110 1100 0154 108 0x6C l 小写字母l
0110 1101 0155 109 0x6D m 小写字母m
0110 1110 0156 110 0x6E n 小写字母n
0110 1111 0157 111 0x6F o 小写字母o
0111 0000 0160 112 0x70 p 小写字母p
0111 0001 0161 113 0x71 q 小写字母q
0111 0010 0162 114 0x72 r 小写字母r
0111 0011 0163 115 0x73 s 小写字母s
0111 0100 0164 116 0x74 t 小写字母t
0111 0101 0165 117 0x75 u 小写字母u
0111 0110 0166 118 0x76 v 小写字母v
0111 0111 0167 119 0x77 w 小写字母w
0111 1000 0170 120 0x78 x 小写字母x
0111 1001 0171 121 0x79 y 小写字母y
0111 1010 0172 122 0x7A z 小写字母z
0111 1011 0173 123 0x7B { 开花括号
0111 1100 0174 124 0x7C | 垂线
0111 1101 0175 125 0x7D } 闭花括号
0111 1110 0176 126 0x7E ~ 波浪号
0111 1111 0177 127 0x7F DEL (delete) 删除