今天完成的事情:
* 了解java基础语法
一基础语法
1代码注释
* 单行 //
* 多行 /* ...*/
* 文档 /** ... */
2标记符
标记类名,变量名,方法名,数组等的名字
构成:字母,下划线,美元符号和数字
* 首字母不为数字
* 不用保留字
* 区分大小写
命名规范
类名:大驼峰
方法名,变量名:小驼峰
常量:都大写
3变量
声明:
int x;
赋值:
int y =1;
x=1;
4常量
特点:
只能被赋值一次,使用final关键字
使用:
final double PI=3.14
5数据类型
* 整数
* 字符
* 布尔
整数:
* int x = 1;
* byte y = -4;
* short a,-b;
* long n = 12345 * 54321
浮点:
float:float f1 = 13.23f;
double:double d1 = 3.5d,d2=5.6;
字符:
char: char ch = 'a',
转义字符:
\ddd 八进制;\uxxxx十六进制;
\r回车;\n换行;\b退格;\f换页
布尔:
true and false
6数据类型转换
低精度-》高精度:不会溢出,隐式转换,
高精度-》低精度:精度丢失或转换失败,显式转换 如:int a = (int) 2.345
### 7运算符
赋值:=
算术:+ - * /
自增和自减:
第一次不加,返回 a++, a--
第一次加后,返回 ++a, --a
关系运算符:== > < 等
逻辑运算符:&& || !
位运算符:& | ~ ^ << >> >>>
复合赋值运算符
三元运算符:statement ? value1:vlaue2;
()提高优先级
运算符优先级
二流程选择
1条件语句
if (condition) {
statement
};
注意:比较时防止漏写= ,所以使用 if (trueCondition)这种方式
if (condition) {
statement1
} else {
statement2
};
switch分支语句
switch (conditionValue) {
case condition1: statement1; [break;]
...
default: statementN;
};
2循环语句
1)while (condition) {
statement
};
2)do {
statement
}
while (condition);
3)for (condition1,c2,c3) {
statement with x
};
4)foreach
for (循环变量x,遍历对象obj){
statement
};
3跳转语句
1)break
2)break Loop
3)continue
三数组
1 一维数组
1)声明:int arr[]; double[] dou;
2)分配内存空间:arr = new int[5];int month[] = new int[12];
3)赋值:int a[] = {1,2,3};int b[] = new int[]{4,5,6};
int c[] = new int[3]; c[0] = 8;
4)方法:获取长度:arr.length;
2二维数组
1)声明:int tdarr1[][];char[][] tdarr2;
2)分配内存:
分配行列:int a[][]; a = new int[2][4];
分配行:int b[][]; b = new int[2][];
3)赋值:
int tdarr1[][] = {{1,2},{3,4}}
int tdarr2[][] = new int[][]{{1,2},{3,4}}
int tdarr3[][] = new int[2][2];
tdarr3[0] = new int[]{1,2}
tdarrf3[0][1] = 3
3不规则数组
int a[][] = new int[2][];
a[0] = new int[4];
a[1] = new int[5];
### 4数组的操作
1)遍历:for循环
2)填充和批量替换:Arrays.fill(arr,value);
五字符串
### String类,创建字符串变量
1创建字符串
1)创建:
String a ='a';
String b1,b2;
b1 = 'b1';
2)构造方法实例化:
String a = new String('str');
3)利用字符数组实例化:
char[] charArray = {'h','e','l','l','o'};
String a = new String(charArray);
4)使用字符数组部分创建字符串对象
char[] charArray = {'h','e','l','l','o'};
String a = new String(charArray,3,2);
2字符串的连接
1)+ 和+=
2)与数字连,自动调用toString()
3提取字符串
1)str.length()
2) str.charAt(index) 获取指定位置
3) a.indexOf(substr) 字符串索引位置
4)str.startswith(prefix);str.endswith(prefix);
5) str.toCharArray();字符转为字符数组
6) str.contains(string);是否包含指定内容
4字符串的操作
1)截取:str.substring
2)替换:str.replace(oldstr,newstr);
3) 分割:str.split(regex);
4) 大小写转换:toLowerCase;
5) 去空白: str.trim();
6) 字符是否相等:str.equals(str2)
### 可变字符串 StringBuffer类
1)创建: StringBuffer sbf = new StringBuffer();
2) append:
sbf.append(obj)
3) 将index的字符改为ch:setCharAtr(index,ch)
4)str插入到offset位置上:insert(offset,str)
5)移除该序列范围内子序 delete(start, end)
6)其他类似String
* length
* charAt
* indexOf
* substring
* replace
7)StringBuffer操作自身对象,而String赋值一次,内容改变生成新对象
明天计划的事情
* java面向对象
* 常用类
* swing程序设计
遇到的问题
无
收获:
了解了java基本语法,做了一些练习。
评论