方法重写(补充)
发生重写的情况:
- 有继承关系
- 方法名同名
- 参数列表要一模一样(顺序,个数,类型)
- 返回值类型为引用型时,子类的返回值类型小于等于父类的返回值类型基本类型时,要一模一样
- 子类的修饰符权限要大于等于父类的修饰符权限(父类的修饰符不能是private)
- 子类抛出的异常小于等于父类抛出的异常(任何一个方法都会对外抛出运行时异常)
public class Test01 {
public static void main(String[] args) {
}
}
class F{
F fun(F s) {
System.out.println(1/0);
return null;
}
}
class S extends F{
@Override
public S fun(F s)throws NullPointerException{
return null;
}
}
String类
字符串:一组字符序列(不可变的串)
创建String对象的方式:
package com.mage.string;
import java.io.UnsupportedEncodingException;
public class Test01 {
public static void main(String[] args) throws UnsupportedEncodingException {
String str1=new String();
System.out.println(str1);
//通过双引号直接创建对象
String str2="hello";
//通过字符串常量赋值
String str3="";
//按照字节数组创建字符串对象
byte[] buf=new byte[] {65,66,67,68,69};
String str4=new String(buf);
System.out.println(str4);
str4=new String(buf, 2, 2);
System.out.println(str4);
str4=new String(buf,2,2,"UTF-8");
System.out.println(str4);
//按照字符数组创建字符串对象
char[] ch=new char[] {'你','好','漂','亮'};
str4=new String(ch);
System.out.println(str4);
str4=new String(ch,0,2);
System.out.println(str4);
//按照代码点创建字符串对象
int[] ins=new int[] {88,89,90,99};
str4=new String(ins,0,4);
System.out.println(str4);
//按照字符串创建对象
String str5=new String("abc");
System.out.println(str5);
}
}
String中常见的方法:
charAt(int index):返回指定索引位置的字符。
codePointAt(index):返回指定索引位置是的元素的代码点
compareTo(String):比较两个字符串的大小
concat(String):拼接作用
copyValueOf(char[]):拼接成新的字符串
getBytes():获取字符串的字节数组
getChars():获取字符串的字符数组
indexOf():查找元素的位置,返回负数代表未找到
isEmpty():查看数组是否为空
replace():替换
splite():截取
substring():截取
toUpperCase():转为大写
toLowerCase():转为小写
public class Test02 {
public static void main(String[] args) {
// 创建一个String对象
String str = "ilovejavaandlaoxue";
//charAt(int index);
char ch = str.charAt(3);
System.out.println("返回当前索引位置上的指定字符:"+ch);
//codePointAt(index)
int num = str.codePointAt(0);
System.out.println("返回指定索引位置上元素的代码点:"+num);
//compareTo(String)
String cStr = "ilovejavaandlaoxu";
num = str.compareTo(cStr);
System.out.println("比较两个字符串的大小:"+num);
//compareToIgronCase(String)
cStr = "ILOVEJAVAANDLAOXUE";
num = str.compareToIgnoreCase(cStr);
System.out.println("比较两个字符串的大小(忽略大小写):"+num);
//concat(String)
cStr = "andgirl";
str = str.concat(cStr);
System.out.println("拼接之后的结果是:"+str);
//copyValueOf(char[])
char[] chs = new char[] {'i','l','o','v','e'};
str = String.copyValueOf(chs);
System.out.println("创建新的字符串:"+str);
str = String.copyValueOf(chs,1,4);
System.out.println("创建新的字符串:"+str);
str = "憨憨.avi";
boolean flag = str.endsWith(".avi");
System.out.println("str是否是.avi格式:"+flag);
//getBytes
byte[] buf = str.getBytes();
System.out.println("获取字符串的字节数组:"+Arrays.toString(buf));
//getChars();
chs = new char[10];
// 从str的2索引处开始 到str的第5个索引结束(不包含) 复制到chs字符数组中 从第1个位置开始
str.getChars(2,6, chs, 1);
System.out.println("获取字符串的字符数组:"+Arrays.toString(chs));
//indexOf 返回负数代表未找到
str = "hehe.avi.png";
int index = str.indexOf(".");
System.out.println(".在string中第一次出现的位置是:"+index);
index = str.indexOf(100);
System.out.println("e在string中第一次出现的位置是:"+index);
index = str.indexOf(".",5);
System.out.println(".在string中第5个索引位置开始计算,第一次出现的位置是:"+index);
index = str.lastIndexOf(".");
System.out.println(".在string中最后一次出现的位置是:"+index);
//isEmpty()
System.out.println("查看数组是否为null:"+str.isEmpty());
//repleace
str = "hehe.avi.png";
str = str.replace(".", "+");
System.out.println("替换之后的结果:"+str);
//splite:
str = "login?unme=zs&pwd=zs";
String[] strs = str.split("\\?");
for(String string:strs) {
System.out.println(string);
}
System.out.println("======");
String parms = strs[1];
strs = parms.split("&");
for(String string:strs) {
String[] ss = string.split("=");
System.out.println(ss[1]);
}
//subString
str = "login?unme=zs&pwd=zs";
str = str.substring(4);// 从4索引开始截取到结束
System.out.println("截取字符串:"+str);
str = str.substring(2, 7);//从2索引开始截取 到索引为7结束不包含7
System.out.println("截取字符串:"+str);
//大小写转换
str = "login?unme=zs&pwd=zs";
str = str.toUpperCase();
System.out.println("转为大写:"+str);
str = str.toLowerCase();
System.out.println("转为小写:"+str);
//通过静态方法创建String对象
String str1 = String.valueOf(123);
System.out.println(str1);
//对象转字符串 其实就是调用当前对象的toString方法
User u = new User(10);
String str2 = String.valueOf(u);
System.out.println(str2);
}
}
StringBuffer和StringBuilder的区别和联系:
- StringBuffer和StringBuilder都是继承AbstractStringBuilder
- StringBuffer的效率低于StringBuilder
- StringBuffer安全性高于StringBuilder
- 一般情况使用StringBuilder
包装类
- 8个包装类都是由8个基本数据类型演变的
byte->Byte
short->Short
char ->Character
int ->Integer
long ->Long
float->Float
double->Double
boolean->Boolean
-
JDK12 Integer中的所有构造器都过时,通过valueOf方法获取int对应得Integer对象
-
String–>int 用Integer.parseInt()方法
-
int–>String 用String.valueOf()
-
JDK1.5之后支持自动拆装箱:
- 装箱:调用了Integer.valueOf()方法
- 拆箱:对象.intValue()
-
tips:自动装箱时,会先判断当前的值是否在缓冲区[-128,127]中,如果在区间,直接从缓冲区中获取对应的Integer对象;如果不在,会重写创建一个新的Integer对象
public class Test02 {
public static void main(String[] args) {
Integer in1 = 123;
int num = in1;
Integer in2 = 88;
Integer in3 = 88;
System.out.println(in2==in3);
in2 = 127;
in3 = 127;
System.out.println(in2==in3);
}
}
- 字面值创建Boolean对象时 只能指定true和false 并且创建的对象也是对应的true和false
- 字符串创建Boolean对象是 只要字符是true/false 无所谓大小写 得到对应的Boolean对象就是与之对应的true和false
- 其它所有字符串赋值都是false
public class Test03 {
public static void main(String[] args) {
Boolean f1 = new Boolean(true);
System.out.println(f1);
f1 = new Boolean(false);
System.out.println(f1);
f1 = new Boolean("1");
System.out.println(f1);
f1 = new Boolean("0");
System.out.println(f1);
f1 = new Boolean("TRue");
System.out.println(f1);
f1 = new Boolean("False");
System.out.println(f1);
f1 = new Boolean("123");
System.out.println(f1);
}
}
Date类
空构造器创建的是当前系统时间对象
public class Test02 {
public static void main(String[] args) {
//创建时间对象
Date date = new Date();
//创SimpleDateFormat对象
SimpleDateFormat sd = new SimpleDateFormat();
//设置一下输出的内容格式
sd.applyPattern("yyyy年MM月dd日 HH时mm分ss秒 是一年中的第D天 W w");
//调用格式化方法格式化日期
String str = sd.format(date);
System.out.println(str);
//创建对象并且指定格式化的内容
SimpleDateFormat ss = new SimpleDateFormat("yy/MM/dd hh:mm:ss");
System.out.println(ss.format(date));
}
}