API:Application Programming Interface:应用程序接口文档
java.lang.Object类:所有的类父类(包括自定义的类)
面试题:
在Java中获取一个类的字节码文件对象的方式有几种?
有三种:
1)public final Class getClass():获取正在运行的类; (返回字节码文件对象)
2)在Java中任意类型的class属性 举例: Class c = 类名.class
3)Class类提供的一个静态方法
public static Class forName(String classPathName) :参数为当前类的全限定名称
String类:代表字符串,是一个常量,创建之后,其值不能更改!
构造方法:
public String():创建字符串对象
public String(String original):将指定的字符串传进去,构造一个字符串对象
public String(char[] value):将字符串数组---构造成String
public String(char[] value,int offset,int count):将一部分字符数组转换成String
public String(byte[] bytes)将字节数组---构造成String
public String(byte[] bytes, int offset,int length):将一部分字节数组---构造成String
如果存在类型转换问题:
已知char[]--->String public String(char[] value)
已知byte[]--->String public String(byte[] bytes)
如果仅仅表示定义一个String对象---推荐的格式
String 变量名 = "字符串常量" ;
字符串常量相加:
先相加,然后看结果,是否在常量池中,如果有,直接返回值地址;否则开空间
字符变量相加:
先在常量池开辟空间,然后在操作;
1.String类中常用的获取功能:
public char charAt(int index)
获取指定索引值对应的字符
public int length() :
字符串特有功能:获取字符串长度
public String concat(String str):
拼接功能 将指定的字符串拼接到该字符串末尾,获取新的字符串
public int indexOf(String str)
返回指定字符串第一次出现索引值
public int lastIndexOf(String str):
返回指定子字符串最后一次出现的索引值
public String[] split(String regex):
字符串拆分功能
截取功能
public String substring(int beginIndex,int endIndex)
从指定位置开始,截取到endIndex-1处结束,包前不包后
public String substring(int beginIndex)
从指定位置开始默认截取到末尾!
2.String类的常用的转换功能
public byte[] getBytes():
将字符串转换成字节数组 (编码过程:将能看懂的东西--->变成看不懂的!)
(使用平台默认字符集)
//String s = "中国" ;
//byte[] bytes = s.getBytes() ;
public char[] toCharArray() :
将字符串转换成字符数组 (使用居多)
//char[] chs = ss.toCharArray();
public static String valueOf(常见的基本类型int/long/double/float/boolean/Object):
String的万能方法:将上面的常用类型--转换成String
//String result = String.valueOf(i);
public String toUpperCase():
将指定字符串转换成大写
public String toLowerCase():
将指定字符串转换成小写
3.String类判断功能
public boolean constains(String str):
判断大串中是否包指定的子字符串
public boolean equals(Object anObject)):
比较两个字符串内容是否相同
public boolean equalsIgnoreCase(String anotherString)):
忽略大小写比较
public boolean startsWith(String prefix):
字符串是否以指定的前缀开头
public boolean endsWith(String str):
字符串是否以指定的后缀结尾
4.其他功能
public String replace(char oldChar,char newChar)
替换功能:使用指定的新的字符替换以前的字符
public String trim():
删除前后两端空格
应用场景:trim():
使用io(intput输入 Output输出)流进行 读写复制(文件内容在网络中传输)
文件内容前后有一些空字符出现,获取到文件内容之后,先去trim()去除两端空格
public StringBuffer():无参构造方法,里面空的字符序列,初始容量16个字符(默认容量足够大) -
public StringBuffer(String str):将字符串构造为StringBuffer类型,容量是大小:当前的里面的字符串长度+16
前两个构造方法使用居多!
public StringBuffer(int capacity):创建一个字符串缓冲区,指定容量大小
获取字符串缓冲区的长度 ---int length()
获取字符串缓冲区的容量---int capacity()
2.StringBuffer的添加/删除的功能
1)添加
public StringBuffer append(任意java类型)
可以将任意类型的元素添加(在末尾追加)到字符串缓冲区中,返回值是字符串缓冲区本身
public StringBuffer insert(int offset,任意java类型)
在指定位置处的序列前面插入新的序列
2)删除
public StringBuffer deleteCharAt(int index)
在指定位置处删除指定的字符,返回字符串缓冲区本身
public StringBuffer delete(int start,int end)
从指定位置start处开始到end-1处的字符序列删除,返回值字符串缓冲区本身
开发中使用居多:追加功能以及删除 deletCharAt(int index)使用居多
3.StringBuffer的特有功能
public StringBuffer reverse() :
字符串缓冲区反转功能,将字符串缓冲区中所有的字符序列反转!
4.StringBuffer其他功能
替换功能
public StringBuffer replace(int start,int end,String str)
使用指定的字符串str从指定位置开始到end-1处进行替换,返回该字符串缓冲区本身
五、Integer和Character
1.Integer
1)构造方法
public Integer(int value):创建一个Integer实例,里面包含的int类型的值
public Integer(String s) throws NumberFormatException:
将数字字符串转换成Integer的实例 可能会出现数字格式化异常(字符串如果不是数字字符串,就会出现这个异常)
2.Character
char类型的包装类类型:Character
构造方法:
public Character(char value):包含char类型的值
成员方法:
判断功能:
public static boolean isDigit(char ch):判断当前这个字符是否为数字
public static boolean isLowerCase(char ch):判断当前这个字符是否小写
public static boolean isUpperCase(char ch):判断当前这个字符是否大写
类型转换重点
1.String和int的相互转换
int--->String
1)使用字符串拼接符号 ""+任何类型数据= "新的字符串"
2)int--->Integer---->String
int i = 100 ;
Integer integer = new Integer(i) ;
String str = integer.toString() ;
3)Integer的静态方法 public static String toString(int i)
String str2 = Integer.toString(i) ;
String--->int(使用的比较频繁)
1)String---->Integer---->int
String source = "10" ;
Integer integer2 = new Integer(source) ;
int value = integer2.intValue() ;
2)Integer提供的静态方法 public static int parseInt(String s)
int value2 = Integer.parseInt(source);
2.StringBuffer和String的相互转换
String--->StringBuffer
1)通过StringBuffer无参构造方法 + 结合 append(String str)
String s = "helloworld" ;
StringBuffer buffer = new StringBuffer() ;
buffer.append(s) ;
2)直接通过StringBuffer的有参构造方法
StringBuffer buffer2 = new StringBuffer(s) ;
StringBuffer---->String
1)通过StringBuffer的toString()
StringBuffer sb = new StringBuffer("JavaEE") ;
String str = sb.toString() ;
2)通过String的构造方法 String(StringBuffer/StringBuilder buffer)
String str2 = new String(sb) ;
3.Date和String的相互转换
将Date---->String:格式化
public final String format(Date date)
* SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
* String dateTime = sdf.format(new Date());
将String日期---->Date:解析
public Date parse(String source)throws ParseException
* String str = "1999-02-05"
* SimplDateFormat sdg = new SimpleDateFormat("yyyy-MM-dd")
* Date dateTime = sdf.parse(str)
二、其他类
1.System类
java.lang.System:不能实例化:提供了静态字段(静态变量)
public static final PrintStream out :标准 打印输出流
public static final PrintStream err 标准错误输出流
public static final InputStream in 标准输入流
成员方法
public static long currentTimeMillis()
计算当前系统时间毫秒值 (很少单独用) 用来计算当前某个程序的时间复杂度(运行效率)
public static void gc()
收到开启垃圾回收器 ---会调用finalize(),回收没有更多引用的对象
public static void exit(int status): 参数为0:终止jvm
public static void arraycopy(
Object src, //原对象
int srcPos, //原对象中的某个位置
Object dest, //目标对象
int destPos, //目标对象的某个位置
int length //指定的长度
)
2.Random类
java.util.Random:伪随机数生成器 可以产生随机数 比Math.random()麻烦
两个构造方法:
public Random() :无参构造方法(推荐)
public Random(long seed):有参构造,创建一个指定long类型的变量的随机数生成器
两个成员方法:
public int nextInt(): 产生伪随机数在,取值是int类型的范围内
public int nextInt(int n):产生0-n之间的随机数(推荐)
3.Date类
java.util.Date:表单特定的日期时间,还允许格式化和解析日期字符串
构造方法:
public Date():创建当前日期对象:获取当前系统日期时间
public Date(long date):里面传入long类型的时间毫秒值,获取的表示从标准基准时间(称为“时代”)即1970年1月1日00:00:00 GMT起的指定毫秒数
成员方法:
public long getTime():将Date对象转换成long类型时间毫秒值
public void setTime(long time):设置日期时间,参数时间毫秒值
转化
java.util.Date重点如何和String日期文本相互转换!
他们之间要进行转换需要使用类DateFormat
DateFormat是日期/时间格式化子类的抽象类,它以语言无关的方式格式化和分析日期或时间
它具体的子类:SimpleDateFormat是一个具体的类,允许格式化(日期文本),解析(文本日期)
构造方法:
public SimpleDateFormat(String pattern):参数是日期模式 "yyyy-MM-dd"
pattern的组成:
y:表示年份中某个值----->yyyy 表示年 "1996/2023/..."
M:表示年中的月份------>MM 表示月:七月-->"07" 十月 -->"10"
d:表示月中的日期值----->dd 表示 "01","11","31"..
成员方法
将Date---->String:格式化
public final String format(Date date)
* SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
* String dateTime = sdf.format(new Date());
将String日期---->Date:解析
public Date parse(String source)throws ParseException
* String str = "1999-02-05"
* SimplDateFormat sdg = new SimpleDateFormat("yyyy-MM-dd")
* Date dateTime = sdf.parse(str)
4.Calendar类
java.util.Calendar:日历类(抽象类)
提供了获取年,年中的月,月中的日期,小时,小时分钟数,分钟中的秒数都可以获取到
如果有一个类是抽象类,可以直接new子类,或者是它提供了一些静态方法---->
完成了一些事情,创建子类对象,返回值是当前抽象类的本身
成员方法:
public static Calendar getInstance() 创建日历对象
public int get(int field):通过日历对象获取日历中的字段值
参数:Calendar中提供的一些静态字段
public static final int YEAR:年份
public static final int DAY_OF_MONTH/public static final int DATE:月中的日期
public static final int MONTH:月份
public abstract void add(int field,int amount):给指定的日历字段值添加或者减去时间偏移量
5.BigDecimal类
BigDecimal这个类是对小数可以进行精确计算的!
构造方法:
public BigDecimal(String val):将String类型数据构造成BigDecimal
将double数据---使用"小数字符串"
成员方法:
public BigDecimal add(BigDecimal augend):求和
public BigDecimal subtract(BigDecimal subtrahend):相减
public BigDecimal divide(BigDecimal divisor):小数除法
public BigDecimal divide(BigDecimal divisor,int roundingMode):除法,第二个参数舍入模式
BigDecimal提供的模式 public static final int ROUND_HALF_UP:四舍五入
...
public BigDecimal divide(BigDecimal divisor,
int scale,//保留几位小数
int roundingMode) //舍入模式
public BigDecimal multiply(BigDecimal multiplicand):乘法
LinkedList的特有方法:
public void addFirst(E e) :在链表开头插入元素
public void addLast(E e) :在链表末尾插入元素
public E removeFirst(): 删除链表开头的第一个元素并返回该元素
public E removeLast()从此列表中删除并返回最后一个元素。
public E getFirst():返回链表头元素
public E getLast():返回链表链尾元素
5.Vector集合
Vector:底层数组,查询快,增删慢,线程安全的,执行效率低!
Vector集合的特有功能:
遍历方式:Collection的Object[] toArray()
Collection的Iterator iterator()
List接口提供的 size()+get(int index)
List接口提供的ListIterator listiterator()
特有遍历方式
public Enumeration<E> elements()--->相当于集合的迭代器
Enumeration接口:枚举组件
boolean hasMoreElements()---->相当于boolean hasNext()
E nextElement() ---->相当于E next()
public E elementAt(int index)--->相当于List的get(int index)+size方法相结合
增强for
添加:
public void addElement(E obj)--->相当于List的add()
public E elementAt(int index)--->相当于List的get(int index)