0x001 标识符和关键字
标识符
- 标识符由字母、下划线、美元符号和数字组成。
- 第一个字符不能是数字
- 不能是关键字
- 不能是true、false、null
注意:JAVA区分大小写
关键字
abstract assert boolean break byte case catch char class constcontinue 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
注意:java使用的是Unicode字符集,所以汉字作为标识符也是可以的,但是并不推荐这种方式。
public class Main {
public static void 中文函数(int 参数A,int 参数B){
System.out.println("参数A:"+参数A+" 参数B:"+参数B);
}
public static void main(String[] args) {
中文函数(1,2);
Scanner scanner=new Scanner(System.in);
scanner.next();
}
}
基本数据类型
8种基本数据类型
- 逻辑类型:boolean
- 整数类型:byte、short、int、double
- 字符类型:char
- 浮点类型:float、double
逻辑类型
- 常量:true、false
- 变量:boolean male=true 占1位
整数类型
类型 | 位数(字节) | 常量 | 变量 | 范围 |
---|---|---|---|---|
byte | 1 | 12 | byte x=12 | -27~27-1 |
short | 2 | 12 | short x=12 | -215~215-1 |
int | 4 | 12 | int x=12 | -231~231-1 |
long | 5 | 12 | long x=12L | -263~263-1 |
注意:java是没有无符号类型的
字符类型
位数:2字节
常量: 'A'、'B'
变量: char ch='A'
-
可以使用Unicode转义来表示字符
public static void unicodeTrancelate(){ char chinaWord='好'; char you='\u4F60'; System.out.println("好:"+(int)chinaWord); System.out.println("you:"+(char)you); } //输出结果 好:22909 you:你
浮点类型
|类型|位数(字节)|常量|变量|有效数字|
|:---:|:---:|:---:|:---:|:---:|:---:|
|float|4|12.0f|float x=12.0f|8|
|double|8|12.0|short x=12.0|16|
注意需要指明数据类型,默认是double,+f是float。
0x002类型转换
精度从低到高:
byte short char int long float double
-
低转高:自动转换
int a=10; float b=a; //输出结果 b=50.0f
-
高转低:强制转换(损失精度)
float a=10.1f; int b=a; //输出结果 b=50
0x003 输入输出数据
输入
输入使用的是Scanner
Scanner scanner=new Scanner(System.in);
boolean isRight=scanner.nextBoolean();//输入一个字符串
int a=scanner.nextInt();//输入一个int
float b=scanner.nextFloat();//输入一个float
String c=scanner.next();//输入一个字符串
输出使用的是
System.out.println("a:"+a);
println表示输出后回车,无需回车可以使用print函数。
格式化输出:
System.out.printf("格式化控制部分",表达式a,表达式b,表达式c);
示例:
System.out.printf("%d,$f",a,b);
- %d:输出int型数据
- %c:输出char型数据
- %f:输出浮点型型数据,小数部分最多保留6位
- %s:输出字符串型数据
- %md:输出int型数据占m列
- %m.nf:输出浮点型型数据占m列,小数保留n位置
0x004数组
声明:
- 一维数组:
- 数组的元素类型 数组名[];
- 数组的元素类型[] 数组名;
- 二维数组:
数组的元素类型 数组名[][];
-
数组的元素类型[][] 数组名;
例如:int boy[];
char[][] cat;
一次声明多个
char[] a,b;==>char a[],b[];
int[] a,b[];==>char a[],b[][];
分配元素
数组名=new 数组元素类型[数组元素个数];
例如:
boy=new int[4];
或者在声明的时候分配
int[] boy=new int[4];
使用int型指定数组大小:
int size=10;
intboy[]=new int[size];
初始化:
int boy[]=new int[4];
boy[0]=1;
boy[1]=2;
boy[2]=3;
boy[3]=4;
或者
int boy[]={1,2,3,4};
System.out.print(boy[1]);
数组的长度
boy.length;
注意:
数组的索引从0开始,结束与n-1;
-
如果访问越界,将会抛出ArrayIndexOutOfBoundsException:
代码:int a[] ={1,2,3}; System.out.print(a[10]);
结果:
- 数组是引用型变量,如果两个数组拥有相同引用,他们就会有相同元素
-
代码:
int a[]={1,2,3,4};
int b[]={100,200,300};
System.out.println("前:");
System.out.print("a:");for (int i:a){ System.out.print(i+","); } System.out.println(); System.out.print("b"); for (int i:b){ System.out.print(i+","); } System.out.println(); System.out.println("后:"); b=a; System.out.print("a:"); for (int i:a){ System.out.print(i+","); } System.out.println(); System.out.print("b:"); for (int i:b){ System.out.print(i+","); }
-
结果:
前: a:1,2,3,4, b100,200,300, 后: a:1,2,3,4, b:1,2,3,4,
-
0x005 运算符、表达式和语句
算术运算
- +、-、*、/、%
自增自减
x++、x-- 自增自减,==>x=x+1,x=x-1
-
--x、++x 自增自减,==>x=x+1,x=x-1
差异:x++ 是先使用再加;++x是先加再使用
栗子:x=1; (x++)*2;//执行逻辑:x*2;x=x+1; (++x)*2;//执行逻辑:x=x+1;x*2;
算术混合运算精度
- 从低到高:
byte short char int long float double - 运算规则:
- 如果有双精度浮点数(double),按双精度运算,5.0/2+10=12.5,12.5是double
- 如果最高单精度浮点数(float),按单精度运算,5.0f/2+10=12.5,12.5是double
- 如果最高long,则按long计算,12L+100+'a'=209,209是long
- 如果最高精度低于int,则按int,(byte)10+'a'=107,107是int
关系运算符
、<、>=、<=、==、!=
逻辑运算符和逻辑表达式
- &&、||、!
位运算符
- &、|、~、^
instanceof运算符
-判断对象是否属于某一个类
0x006 逻辑语句
if条件语句
//单条件单分支
if(表达式){
语句
}
//单条件双分支
if(表达式){
语句
}else{
语句
}
//多条件多分支
if(表达式){
语句
}else if(条件){
语句
}
switch语句
switch(条件)
{
case 常量值1:
语句
break;
case 常量值2:
语句
break;
case 常量值2:
语句
break;
default:
}
for循环
for(表达式1;表达式2;表达式3){
语句
}
while循环
while(表达式){
语句
}
do-while循环
do{
语句
}while(表达式);
break和continue
- break:用来跳出循环
-continue:用来跳出一次循环