Java基础
进制转换问题
- 十进制小数转换为N进制,小数乘以N,取整即可
- 十六进制小数高位到低位,依次对应-1此方位,-2次方位,-3次方位…
Math API
Math.round(): 四舍五入的原理是在参数上加0.5然后进行下取整
* Math.round(11.5) = 12
* Math.round(-11.5) = -11
Math.floor():根据“floor”的字面意思“地板”去理解
* Math.floor(11.46)=Math.floor(11.68)=Math.floor(11.5)=11
* Math.floor(-11.46)=Math.floor(-11.68)=Math.floor(-11.5)=-12
Math.ceil():根据“ceil”的字面意思“天花板”去理解
* Math.ceil(11.46)=Math.ceil(11.68)=Math.ceil(11.5)=12
* Math.ceil(-11.46)=Math.ceil(-11.68)=Math.ceil(-11.5)=-11
Math.pow()
Math.pow(arg0, arg1)
其中arg0代表底数,arg1代表指数次幂。
Math.pow(2,10) = 1023
接口默认修饰符
Java的interface中,成员变量的默认修饰符为:
public static final
所以我们在interface中定义成员变量的时候,可以
1:public static final String name = “张三”;
2:String name = “张三”;
方法的默认修饰符是:
public abstract
即:公共抽象的,在接口中定义方法时候,也有两种方式
1:public abstract List getUserNames(Long companyId);
2:List getUserNames(Long companyId);
List.remove()
List.remove(Object o)
直接移除对象,从index=0开始扫描,移除满足要求的第一个对象
List.remove(int index)
根据索引移除相应的对象
Thread类的方法
类方法的重载与重写
重写
- 子类和父类出现了同名的函数,称作为方法重写
- 方法重写的原因
父类的功能无法满足子类的需求
- 方法重写时注意的问题
1.参数列表问题:方法名与形参列表必须一致(=)
2.修饰符问题:子类的权限修饰符必须要大于或者等于父类的权限修饰符(>=)
3.返回值问题:子类的返回值类型必须要小于或者等于父类的返回值类型(<=)
4.抛出异常问题 子类抛出的异常类型要小于或等于父类抛出的异常类型(<=)
重载
- 在一个类中存在两个或者两个以上的同名函数,称作为方法重载。
- 方法重载的要求
1.函数名要一致
2.形参列表不一致(形参的个数或形参的类型不一致)
3.与返回值类型无关
抽象类和接口的区别
抽象类
- 抽象类是用来捕捉子类的通用特性,不能被实例化
接口
- 接口是抽象方法的集合,不能被实例化
区别
参数 | 抽象类 | 接口 |
---|---|---|
修饰符 | public,protected,default | public |
构造器 | 有或无 | 无 |
实现 | extends 子类不为抽象类时需要实现所有抽象方法 | implements |
main方法 | 拥有main方法,且能运行 | 无main方法 |
速度 | 比接口快 | 比抽象类慢 |
添加新方法 | 往抽象类中添加新的方法,给它提供默认实现,不需要改变子类现在的代码 | 往接口中添加方法,必须改变实现该接口的类 |
java8新特性 | - | 在java8中,在接口中添加方法时,使用default标识符,并完成该方法的默认实现,此时不需要改变实现该接口的类 |
循环中标签的使用
continue与标签结合
outer1:
for(int i =0;i<4;i++){
System.out.println("begin to itrate. "+i);
for(int j =0;j<2;j++){
if(i==2){
continue outer1;
}
System.out.println("now the value of j is:"+j);
}
System.out.println("******************");
}
结果
begin to itrate. 0
now the value of j is:0
now the value of j is:1
******************
begin to itrate. 1
now the value of j is:0
now the value of j is:1
******************
begin to itrate. 2
begin to itrate. 3
now the value of j is:0
now the value of j is:1
******************
当i=2时,执行continue outter1语句,会回到外循环最开始的位置,不会执行continue outter1之后的语句,然后i=3,继续进入外循环
break与标签结合
outer2:
for(int i =0;i<4;i++){
System.out.println("begin to itrate. "+i);
for(int j =0;j<2;j++){
if(i==2){
break outer2;
}
System.out.println("now the value of j is:"+j);
} System.out.println("******************");
}
结果
begin to itrate. 0
now the value of j is:0
now the value of j is:1
******************
begin to itrate. 1
now the value of j is:0
now the value of j is:1
******************
begin to itrate. 2
当i=2时,执行break outter2语句,会跳出外循环并结束循环
Servlet生命周期
二叉树遍历序列求解
根据先序和中序求后序遍历序列
根据中序和后序求先序遍历序列
wait()与sleep()的区别调用时机以及对象锁
SQL语句回炉
升序降序
- order by … desc/asc
- desc : descend
- asc: ascend(默认)
SELECT * FROM table_name ORDER BY field ASC
SELECT * FROM table_name ORDER BY field DESC
将file1.txt中内容复制到file2.txt中
使用FileReader与FileWriter
public static void copy(String sourceFile,String targetFile){
File source = new File(sourceFile);
File target = new File(targetFile);
try{
FileReader fr = new FileReader(source);//读文件
FileWriter fw = new FileWriter(target);//写文件
BufferedReader bufr = new BufferedReader(fr); //缓存
String str = null;
while ((str = bufr.readLine()) != null){
fw.write(str);
//在Windows操作系统中,fr.write("\n"); 是不行的,需要 fr.write("\r\n"); 即回车换行
fw.write("\r\n");
}
bufr.close();
fw.close();
fr.close();
}catch(IOException e){
e.printStackTrace();
}finally {
System.out.print("最终执行我");
}
}
使用InputStream与OutputStream
public static void copyByIOStream(String sourceFile,String targetFile){
try{
//在InputStreamReader()/OutputStreamWriter()参数中加入字符编码gbk,解决中文乱码问题
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File(sourceFile)),"gbk"));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(targetFile)),"gbk"));
String str = null;
while((str=reader.readLine())!=null){
System.out.println(str);
writer.write(str);
writer.newLine();
}
writer.close();
reader.close();
}catch (FileNotFoundException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
finally {
System.out.println("finally");
}
}
文件与流的基本认识
构造方法注意事项
子类的构造方法总是先调用父类的构造方法,如果子类的构造方法没有明显地指明使用父类的哪个构造方法,子类就调用父类不带参数的构造方法。如果父类没有无参的构造函数,所以子类需要在自己的构造函数中显示的调用父类的构造函数。
将包含多个空格的字符串转换为字符串数组
public String[] split(String regex) {
return split(regex, 0);
}
//示例
String str = "shu tian meng tu";
String[] strings = str.split(" ");
for (String _str : strings
) {
System.out.println(_str);
}
//结果
shu
tian
meng
tu
BCD码
BCD码是用二进制来表示十进制的一种编码,通过四位二进制数来表示十进制数中的0-9。例如,0000表示0,0001表示1,0010表示2,…1001表示9
BCD码分为:
- 压缩BCD码:一个字节表示两个0-9的十进制数,高八位和低八位分别表示一个0-9的十进制数
- 非压缩BCD码:一个字节表示一个0-9的十进制数,高八位始终为0000
//压缩BCD码
10010001 -> 91
//非压缩BCD码
00001001 -> 9
多态
package Wangyi;
class Base
{
public void method()
{
System.out.println("Base");
}
}
class Son extends Base
{
public void method()
{
System.out.println("Son");
}
public void methodB()
{
System.out.println("SonB");
}
}
public class Test01
{
public static void main(String[] args)
{
Base base = new Son();
base.method();
base.methodB();
}
}
结果:编译出现错误
规律:编译看左边,运行看右边