1.注释
ctrl+/:单行注释
ctrl+shift+/:多行注释
2.字面量
| 字面量 | 写法 |
|---|---|
| 整数 | 写法一致(666) |
| 小数 | 写法一致(2.12) |
| 字符 | 必须使用单引号,有且只有一个字符('A') |
| 字符串 | 必须使用双引号,内容可有可无(”你好”,” “) |
| 布尔值 | true,false |
| 空值 | null(特殊值) |
特殊字符:
——换行——相当于tab
System.out.println(' ');//相当于空出两行 System.out.println(" ");//单引号和双引号读行
3.变量
公式:数据类型 变量名称=数据
int month=12;//注意:中间有空格
注意事项:1.变量要先声明,才能使用
2.什么类型的变量,只能储存什么类型的数据
3.变量存在访问范围({……}或者从定义到 }为止),同一个范围内,多个变量的名字不能一样
4.变量定义时可以不给赋初始值,但是在使用的时候变量里必须有值
int month=12; int month-12;//第二行报错 {int month=23} System.out.println(month);//12
4.关键词
public,class,double,int等
关键词不能用来作为:类名,变量名
5.标识符
主要组成:数字,字母,下划线_,美元符号
不能以数字开头,不能用关键词作为名字,并且区分大小写
标识符的建议规范:
方法/变量名:用英文,有意义,第一个字母小写(studyNumber)
类名称:全英文,有意义,首字母大写(HelloWorld)
6.变量详解
二进制:只有0,1,按照逢2进1的方式表示数据
6/2=3/2=1…1(6的二进制:110) 13/2=6…1 /2=3/2=1….1(13的二进制:1101)
7.字符
1.计算
字符'A'——65
字符'a'——97
字符'0'——48
int de='a'; System.out.println(de);//97
2.直接输出
System.out.println('a');//a
8.二,八,十六进制在程序中的写法
| 进制 | 写法 |
|---|---|
| 二进制 | 以0B或者0b开头 |
| 八进制(最小数是0,最大数是7,每三位二进制作为一单元) | 以0开头 |
| 十六进制(最小数是0,最大数是15,每四位二进制作为一单元,0123456789ABCDEF) | 以0X或者0x开头 |
常见换算结果:
1B=8b;1KB=1024b;1MB=1024KB;1GB=1024MB;1TB=1024GB;
9.数据类型
1.基本数据类型:
| 数据类型 | 字节数 | 数据范围 |
|---|---|---|
| 整数型 byte | 1 | -128~127 |
| 整数型 short | 2 | -32768~32767 |
| 整数型 int(默认) | 4 | |
| 整数型 long | 8 | 默认是long类型,在数据后面加上l/L |
| 小数型 float | 4 | 默认是float类型,在数据后面加上f/F |
| 小数型 double(默认) | 8 | |
| 字符型 char | 2 | |
| 布尔型 boolean | 1 |
long des=23322l; System.out.println(des); float des2=22389f; System.out.println(des2); char des3='a'; System.out.println(des3); boolean des4=true; System.out.println(des4);
2.引用数据类型(自定义数据类型):
| 数据类型 | 符号 |
|---|---|
| 类 | Class |
| 接口 | Interface |
| 数组 | Array |
| 空 | NULL |
| 集合 | Collection |
| 字符串 | String |
//字符串String
String name="张三";
System.out.println(name);//张三
char c = name.charAt(1);
System.out.println(c);//查询字符
for (int i = 0; i < name.length(); i++) {
char char1=name.charAt(i);
System.out.println(char1);
}//遍历字符串
//集合Collection
Collection<String>collection1=new ArrayList<>();
collection1.add("张三");
for (String s : collection1) {
System.out.println(s);
}
//数组Array
int[]arr2={1,2,3,4,5};
System.out.println(Arrays.toString(arr2));//返回数组内容
//接口Interface
public interface HelloWorld {
String name="张三";
void helloWorld();
}
10.数据类型转换
1.自动类型转换
类型范围小的变量,可以直接赋值给类型范围大的变量
byte>short/char>int>long>float>double
byte b=12; int di=b; System.out.println(di);
2.强制类型转换
强制类型转换可能造成数据丢失
浮点型强转成整数型,直接丢掉小数部分,保留整数部分返回
double dew=12.3; int sod= (int) dew;//快捷键:alt+回车键 System.out.println(sod);//12
3.表达式的自动转换
在表达式中,小范围类型的变量,会自动转换成表达式中较大范围的类型,再参与计算
byte,short,char>int>long>float>double
表达式的最终结果类型由表达式中最高类型决定
在表达式中,byte,short,char是直接转换成int类型运算
byte dedo=23; int ff=34; int s=dedo+ff; System.out.println(s); byte doe=23; short deos=42; int dess=doe+deos; System.out.println(dess); byte nihao=12; byte hao=30; int ws=nihao+hao; System.out.println(ws);
11.运算符
1.除/,取余%
除:在Java中两个整数相除结果还是整数
int ni2=10; int ni3=3; int ni4=ni2/ni3; System.out.println(ni4);//3
取余:正负取决于被除数
int r=-6; int p=5; System.out.println(r%p);//-1 int r1=6; int p1=5; System.out.println(r1%p1);//1
2.自增++,自减–
“+”符号与字符串运算的时候是用作连接符的,其结果依然是一个字符串(“ithema”+a+”a”结果:“ithemaaa”)
单独使用自增,自减:“++a”与“a++”无区别
不单独使用自增,自减:1.“++a”放在变量之前,先加后用,先对变量+1,-1,再拿变量的量运算;2.“a++”放在变量之后,先用后加,先拿变量的量运算,再对变量+1,-1
int q=1;
q++;
++q;
System.out.println(q);//3
int w=3;
int x=w++;
System.out.println("W的值:"+w+",X的值:"+x);//w=4,x=3
int w1=3;
int x1=++w1;
System.out.println("W1的值:"+w1+",X1的值:"+x1);//w1=4,x1=4
3.扩展赋值运算符
a+=b等效于a=(a的类型)(a+b)
a/=b等效于a=(a的类型)(a/b)
a*=b等效于a=(a的类型)(ab)
运用场景:运用于抢红包,发红包,转账,收账
byte cv=12; int o=23; cv+=o;//cv=(byte)cv+o System.out.println(cv);//强制转换
4.赋值运算符
“=”:赋值,从右往左看
int l=23; int df=l; System.out.println(df);//23
5.关系运算符
结果:true/false
| 运算符 | 作用 |
|---|---|
| <= | 小于等于(a<=b判断a是否小于等于b)只要满足小于或者等于则true |
| >= | 大于等于(a>=b判断a是否大于等于b) 只要满足大于或者等于则true |
| == | 等于(a==b判断a是否等于b) |
| != | 不等于(a!=b判断a是否不等于b) |
6.逻辑运算符
| 运算符 | 作用 |
|---|---|
| & | 多个条件必须是true,结果才是true |
| | | 多个条件只要一个为true,结果就为true |
| ! | 相反(!(2>3)=true,!(3>2)=false) |
| ^ | 多个条件结果相同则false,多个条件结果不同则true((2>3)^(3>2)=true,(2>3)^(1>3)=false) |
| && | 左边为false,右边则不执行,多个条件必须是true,结果才是true((2>10)&&(3>2)=false) |
| || | 左边是true,右边则不执行,多个条件只要一个为true,结果就为true((2>1)||(3<5)=true) |
7.三元运算符
格式:条件表达式?值1:值2;
首先计算关系表达式的值,如果值是true,返回值1,如果值是false,返回值2
| 优先级 | 运算符 | 优先级 | 运算符 |
|---|---|---|---|
| 1 | () | 6 | | |
| 2 | */% | 7 | && |
| 3 | +- | 8 | || |
| 4 | & | 9 | ?: |
| 5 | ^ |


