1,晨写
1,列举Object类中的常用方法
public boolean equals(Object antherObj)
//底层原码 this==antherObj
public String toString()//打印对应的字符串形式
public Object clone() throws CloneNotSupportedExption//创建对象副本
//前提该对象必须实现接口"Cloneable"
public int hashCode() //哈希码值
//返回int类型范围的值
2,String类的特点是什么
String是不可变的,一旦被创建,其值不可改变
创建字符串格式
String s ="xxx";(推荐)
String s = new "xxx";
String作为特殊的引用类型,他作为形式参数,它的概念不会影响实际参数
3列举String类的转换功能有哪些?(4个)
public char[] toCharArray()//将字符串转换为数组
public byte[] getBytes()//使用平台默认字符集对字符串进行编码
public static String valeOf()//将指定的类型转换为String
public String toUppercase()//将字符串转换为大写
public String toLowrcase()//将字符串转换为小写
...
4,列举String类的获取功能
//截取功能
//从指定位置接渠道endInde -1处结束
public String substring(int beginIndex,int endIndex)
//从指定位置截取到结束
public String substring(int beginIndex)
//拆分
//将一个字符串通过指定分隔号拆分成字符串数组
public String[] spile(String regex)
//拼接
//使用当前字符串和指定的字符串拼接成一个新的字符串;在末尾拼接
public String concat(String antherString)
//获取字符串长度
public int length()
//面试题
数组中有没有length(),String类中length(),在集合中有没有length()?
数组中length属性
String类中有length(),获取字符串长度
集合中没有,size():获取集合中元素数
5,String类中两个字符串按照字典顺序如何比较的?
//1,获取两个字符串对应的字符数组长度
//2,获取最小值Math.min(长度1,长度2);
//3,将两个字符串使用字符数组表示
//4,定义统计变量
//5,循环 条件 k<长度最小值
//6,不断通过循环条件取出数组元素,如果都一致,条件不满足
如果条件满足;如果某一个字符不一致,直接返回return,第一个字符数组某元素,第二个字符数组某元素,
// 7,return 长度相减
//8,第一个字母就不同
return ASCLL码值相减
2,StringBuffer字符缓冲区(容器)
线程安全的字符串缓冲区,支持可变的字符序列!
构造方法:
//无参构造,空的字符序列,初始容量16字符
public StringBuffer()
// 将字符串构造成StringBuffer类型,容量为字符串长度+初始容量
public StringBuffer(String str)
//创建一个字符串缓冲区,指定容量
public StringBuffer(int capacity)
//获取字符串缓冲区长度------length()
//获取字符串缓冲区容量-------capacity()
StringBuffer添加删除功能
//(末尾追加到字符串缓冲区,)返回值是字符串缓冲区本身
public StringBuffer append(任意java类型)
//在指定位置处的序列前面插入新的序列
public StringBuffer insert(int offer,任意java类型)
//在指定位置删除指定字符,返回值是字符串缓冲区本身
public StringBuffer deleteCharAt(int index)
//从指定位置start,到end-1处的字符序列,返回字符串缓冲区本身
public StringBuffer delete(int start,int end)
StringBuffer特有功能
//字符串缓冲区反转功能,将字符串缓冲区的字符序列反转
public StringBuffer reverse()
//使用指定字符串str,替换从start开始,到end-1处进行替换,返回字符串缓冲区本身
public StringBuffer replace(int start,int end,String str)
//从start截取掉,返回字符串
public String subSting(int start)
//从start开始到end-1处截取掉,返回字符串
public String subString(int start,int end)
StringBuffer和StringBuilder的区别
都是字符串缓冲区,都支持可变的字符序列
StringBuffer是线程安全的,多线程的------同步-----执行效率低
StringBuilder是StringBuffer的简易替换是线程不安全的,单线程的-----不同步----执行效率高
String和StringBuffer的相互转换(重点)
package com.qfstringbuffer;
public class Str_StrB {
public static void main(String[] args) {
//String和StringBuffer的相互转换
//String-->StringBuffer
String s = "HelloWorld";
// 1>使用StringBuffer有参构造方法
StringBuffer strb = new StringBuffer(s);
System.out.println(s);
// 2>使用StringBuffer无参构造方法和append方法
StringBuffer strb2 = new StringBuffer();
strb2.append(s);
System.out.println(strb2);
//StringBuffer-->String
// 1>使用toString方法
StringBuffer strb3 = new StringBuffer("HelloJavaEE");
strb3.toString();
System.out.println(strb3);
// 2>用String的构造方法
String str = new String(strb3);
System.out.println(str);
}
}
String和StringBuffer的区别
String特点:
字符串常量,创建后,其值不可更改
作为形式参数,不会影响实际参数(特殊的引用数据类型)
StringBuffer特点:
字符串缓冲区的字符串,线程安全,执行效率低
作为形式参数,会影响实际参数
3,java.util.Date:日期类
4,Interger类以及Character类
Integer i = 整数;----->底层的Integer
Interger类-----内部类---IntergerCache:内部缓存区:缓存池大小 -128~127
Character类提供
判断功能:判断字符大小写,数字字符
四类八种的基本数据类型都会自动提升对应的引用类型 jdk5新特性:自动拆装箱
这些基本数据类型可以和String相互转换---转换桥梁..>基本数据类型对应的包装类型
Interger的方法
构造方法
//创建Interger实例,里面包含int的值
public Interger(int value)
//将数字字符串转换为Interger实例,可能会出现异常(字符串如果不是数字字符串,就会出现异常)
public Interger(String s) thows NumberFormatException
Interger其他常用方法
//获取int的最大值
public static final int Max_VALUE
//获取int的最小值
public static final int Min_VALUE
...
Character的常用方法
构造方法
//包装含有char类型的值
public Character(char value)
Character其他常用方法
//判断功能
//判断指定的字符是否为数字
public static boolean isDigit(char ch)
//判断指定的字符是否为小写
public static boolean isLowerCase(char ch)
//判断指定的字符是否为大写
public static boolean isUpperCase(char ch)
基本类型和String的相互转换(重点)------>用int举例
package com.qf.integer_04;
/**
* int基本类型和引用String的相互转换 (重点)
*
* String---int: int 变量名 = Integer.parseInt(已知的数字字符串) 推荐
* int---->String: Integer类提供静态方法 String 变量名 = Integer.toString(int类型的值)
*/
public class IntegerDemo4 {
public static void main(String[] args) {
//int---->String
//方式1)比较简单
int i = 100 ;
String s = "" ;
s += i ; //拼接符号 s = s+i
System.out.println(s) ;//"100"
System.out.println("-----------------------------------------") ;
//方式2):int---->Integer----->String
//int-->Integer
//Integer integer = new Integer(i) ; //有参构造方法
Integer integer = Integer.valueOf(i);
//Integer--->String
String str = integer.toString();
System.out.println(str);
System.out.println("----------------------------------------------") ;
//方式3:Integer---->String
//public static String toString(int i)
String str2 = Integer.toString(i);
System.out.println(str2);
System.out.println("---------------------String-->Integer->int----------------------------") ;
//推荐一步执行:
//Integer类提供了静态方法
//public static int parseInt(String s)throws NumberFormatException:要转的字符串必须为数字字符串
//万能方法:String--->long --->Long引用提供静态方法 public static long pasreLong(String str)
//...
String source = "1990" ;
int value = Integer.parseInt(source);
System.out.println(value) ;
System.out.println("------------------------------------------");
//分步执行
//String--->Integer---->int
//Integer(String str)
Integer ii = new Integer(source) ;
//Integer--->int public int intValue() :
int value2 = ii.intValue();
System.out.println(value2) ;
}
}
5,其他常用工具类
6,对象数组
对象数组初始化方式:
对象数组动态初始化:
类 对象数组名称 [] = new 类[长度];
对象数组静态初始化:
类 对象数组名称 [] = new 类[]{实例化对象列表};
package com.demo;
public class Student {
//学生属性
private String name ;
private int age ;
//获取对象信息方法
public String get(){
return "姓名:"+this.name+",年龄:"+this.age;
}
//构造方法
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
//get set方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package com.demo;
public class StudentTest {
public static void main(String[] args) {
//动态初始化定义
mother1();
System.out.println("========================================");
//静态初始化定义
mother2();
}
private static void mother2() {
Student[] stu1 = new Student[]{
new Student("曹操",33),new Student("曹睿",18),new Student("曹植",23),
new Student("许褚",31),new Student("司马懿",38)
};
for (int i = 0; i < stu1.length; i++) {
System.out.println(stu1[i].get());
}
}
private static void mother1() {
Student[] stu = new Student[5];
stu[0] = new Student("张飞",34);
stu[1] = new Student("关羽",35);
stu[2] = new Student("赵子龙",32);
stu[3] = new Student("黄忠",36);
stu[4] = new Student("马超",31);
for (int i = 0; i < stu.length; i++) {
System.out.println(stu[i].get());
}
}
}