常用类
内部类object类object类常用方法包装类String类BigDecimalCalendarDateSystem
内部类
编译后可以产生独立的字节码文件 能够直接访问外部类的私有成员而不破坏封装 为外部类提供必要的功能组件
成员内部类
在类的内部定义,与实例变量 方法同级别的类外部类的实例部分,创建内部类的对象时,必须依赖外部类对象 静态内部类
级别与外部类相同,使用方法相同 局部内部类
定义在外部类的方法中只供当前方法使用级别与局部变量相同 匿名内部类
没有类名的局部内部类必须继承一个父类或接口在创建对象的同时创建一个局部内部类只能在当时使用一次
object类
默认继承object所有类都与之有关
getClass()
判断两个引用中的实际存储对象类型是否一致
Student s1
= new Student();
Student s2
= new Student();
Class
class1 = s1
.getClass();
Class
class2 = s2
.getClass();
if(class1
== class2
){
sout("相同类型")
}
hashcode()
public int hashCode(){}根据对象的内存地址字符串或值返回该对象的哈希码值
Student s1
= new Student();
Student s2
= new Student();
Class
class1 = s1
.getClass();
Class
class2 = s2
.getClass();
sout("s1.hashCode");
sout("s2.hashCode");
Student s3
= s2
;
toString()
public String toString(){}返回该对象的字符串表示可以根据需求重写覆盖该方法 用return String来输出字符串
equals()
public boolean equals(Object obj){}默认实现为(this – obj),比较两个对象的地址是否相同可以重写为比较对象内容是否相同
finalize()
对象被判定为垃圾时 jvm会调用此方法来标记回收对象,进入回收队列System.gc() 手动垃圾回收,用于通知jvm进行回收
包装类
基本数据类型所对应的引用数据类型[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YOKDIUf8-1603354129905)(基本数据类型.png)]对象形式的基本数据类型,其中包含了一些方法例如 **Value() toString()Object类可以统一所有数据,包装类的默认值为null
类型转换与装箱,拆箱
装箱:对基本数据类型进行对象创建 转为引用类型
Integer a
= new Integer(int a
);
int age
= 3;
Integer integer1
= age
;
拆箱:指创建的数据类型对象使用 **Value()方法调用返回值
Integer integer2
= new Integer(12);
int b
= integer2
.intValue();
int age
= integer2
;
基本类型转为字符串 例如 int n1 = 100;
使用+,String s1 = n1+"";使用Integer中的toString(). String s2 = Integer.toString(n1)
字符串转为基本类型 例如 String str = “150”;
使用Integer.parsexxx(); int a = Integer.parseInt(str);
boolean 字符串转为基本类型 “true”==>true 其他全是false boolean a = Boolean.parseBoolean()
Integer缓冲区
自动装箱使用的是Integer.valueOf();没有新建对象缓冲区中有已经创建的-128到127之间的对象,会直接进行调用地址,所以赋值的变量地址相同不在以上范围内的数字大小才会进行new Integer操作,产生新的对象节省内存的消耗
String类
字符串是常量,String s = “Hello”; 字面值存储在字符串池中,当改变字符串的值会在池中新建一个字符串对象,创建新值会检查池中是否已存在此值,存在则直接调用,相同字符串地址相同String s = new String(“Java Best”); 此方法会在堆和池中同时创建对象,栈指向堆指向池,实际只有池中存在字符串,相同字符串堆地址不相同
常用方法
1. public int length()
输出字符数 int
2. public cahr charAt()
返回指定某个位置的字符 范围 0 - (length-1)
3. public boolean contains()
判断是否包含某个字符串
public void strTest() {
String a
= new String("Java牛逼");
System
.out
.println(a
.length());
System
.out
.println(a
.charAt(a
.length() - 1));
System
.out
.println(a
.contains("Java"));
System
.out
.println(a
.contains("php"));
}
6
逼
true
false
4. public char[] toCharArray()
将字符串转换为数组
5. public int indexOf(String str)
查找str首次出现的第一个字符下标,如不存在返回-1
6. public int lastIndexOf(String str)
查找str最后一次出现的第一个下标,如果不存在返回-1
public void strtest2(){
String a
= new String("StringTest");
char[] b
= a
.toCharArray();
for (char c
: b
) {
System
.out
.println(c
);
}
}
public void strtest3(){
String str
= new String("javaisbestisworst");
System
.out
.println(str
.indexOf("is"));
System
.out
.println(str
.indexOf("isi"));
System
.out
.println(str
.lastIndexOf("is"));
System
.out
.println(str
.lastIndexOf("isi"));
}
7. public String trim()
去除字符串前后的空格
8. public String toUpperCase()/toLowerCase
将字符串的大小写转换
9. public boolean endWith()
判断是否以某些字符结尾或开始
public void strtest4(){
String str
= new String(" JavaIsBest ");
System
.out
.println(str
);
System
.out
.println(str
.trim());
System
.out
.println(str
.toUpperCase());
System
.out
.println(str
.toLowerCase());
System
.out
.println(str
.endsWith("t"));
System
.out
.println(str
.endsWith(" "));
System
.out
.println(str
.startsWith(" "));
System
.out
.println(str
.startsWith("Ja"));
}
10. public String replace(char old, char new);
将字符串中的字符进行替换 不存在则不做操作
11. public String[] sprit()
对字符串进行拆分
public void strtest5(){
String str
= new String("Java may best,my son");
System
.out
.println(str
.replace("Java","Python"));
System
.out
.println(str
.replace("Python","C#"));
String
[] result
= str
.split(" ");
String
[] rlst
= str
.split("[ ,]");
for (String s
: result
) {
System
.out
.println(s
);
}
for (String s
: rlst
) {
System
.out
.println(s
);
}
}
12. public bollean equal()
判断字符串是否相等
13. public int compareTo()
从第一位字符开始比较大小并返回当前位置的差值。如果前面字符相同字符串长的大,返回字符数差值
public void practice(){
String str
= "this is a text";
String
[] result
= str
.split(" ");
for (String s
: result
) {
System
.out
.println(s
);
}
System
.out
.println(str
.replace("text", "practice"));
System
.out
.println(str
.replace("a text", "an easy text"));
System
.out
.println("===================");
String f
= new String();
for (String s
: result
) {
char first
= s
.charAt(0);
char a
= Character
.toUpperCase(first
);
System
.out
.println(s
.replaceFirst(Character
.toString(first
),Character
.toString(a
)));
f
+= s
.replaceFirst(Character
.toString(first
),Character
.toString(a
));
f
= f
+" ";
}
f
= f
.trim();
System
.out
.println(f
);
char[] d
= {'a','b','c','e','d'};
System
.out
.println(Arrays
.toString(d
));
}
可变字符串
StringBuffer JDK 1.0提供,运行效率慢,线程安全,用于多线程 在缓冲区中预先开辟空间,直接进行操作StringBuilder JDK 5.0提供,运行效率快,线程不安全,多用于单线程 与Buffer相似
append() 向可变字符串中追加内容insert() 向字符串中插入内容replace() 替换字符串中的内容,可指定位置但无旧字符替换delete() 删除指定下标位置之间的字符串
public class Demo02 {
public void string(){
StringBuffer a
= new StringBuffer();
a
.append(1);
a
.append("javasb");
System
.out
.println(a
);
a
.insert(0, "First");
System
.out
.println(a
);
a
.insert(1, "Second");
System
.out
.println(a
);
a
.replace(1, 7, "Hello");
System
.out
.println(a
);
StringBuilder b
= new StringBuilder();
a
.delete(0, 1);
System
.out
.println(a
);
}
BigDecimal类
double类型存储的是近似值,并非精确数值 无法适用于精度高的项目,例如银行,此时使用BigDecimal位于Java.math,用于计算精确浮点数
add()加法subtract()减法multiply()乘法divide()除法
public void testdouble(){
double a
= 1.0;
double b
= 0.9;
System
.out
.println(a
- b
);
BigDecimal c
= new BigDecimal("1.0");
BigDecimal d
= new BigDecimal("0.9");
System
.out
.println("a:" + c
+" "+ "b:" + d
);
System
.out
.println("a+b= " + c
.add(d
));
System
.out
.println("a-b= " + c
.subtract(d
));
BigDecimal e
= new BigDecimal("0.3")
.multiply(new BigDecimal("0.4"));
System
.out
.println("0.3 * 0.4 = " + e
);
BigDecimal f
= new BigDecimal("1.0")
.divide(new BigDecimal("0.3"),3,BigDecimal
.ROUND_HALF_UP
);
System
.out
.println("1.0 / 0.3 = " + f
);
}
Date类
表示特定的瞬间,精确到毫秒大部分方法已经被Calendar类所取代
getTime() 获取现在的系统时间after() befor() 判断时间是否在某时间之前或之后,返回BooleancompareTo() 判断时间大于某时间,返回1,0,-1equals() 判断时间是否相等,返回Boolean
Calendar
JDK1.1 之后大部分取代Date类构造方法:protected Calendar(),导致无法直接创建对象
static Calendar getInstence() 使用默认时区和区域获取日历void set(int year, int month, int date, int hourofday, int minute, int second) 设置日历的年月日时分秒int get() 返回给定日历字段的值,比如年月日void setTime(Date date) 用给定的Date设置此日历的时间Date getTime() 返回一个Date表示此日历的时间void add(int filed, int amount) 按照日历的规则,给指定字段添加或减少时间量long getTimeMillies() 毫秒为单位返回该日历的时间值
public void calendar(){
Calendar calendar
= Calendar
.getInstance();
System
.out
.println(calendar
.getTime().toLocaleString());
System
.out
.println(calendar
.getTimeInMillis());
System
.out
.println(calendar
.get(Calendar
.YEAR
));
System
.out
.println("MONTH" + calendar
.get(Calendar
.MONTH
)+1);
System
.out
.println("DATE" + calendar
.get(Calendar
.DATE
));
System
.out
.println("HOUR_OF_DAY" + calendar
.get(Calendar
.HOUR_OF_DAY
));
System
.out
.println("HOUR" + calendar
.get(Calendar
.HOUR
));
System
.out
.println("SECOND" + calendar
.get(Calendar
.SECOND
));
Calendar time
= Calendar
.getInstance();
time
.set(Calendar
.YEAR
,1999);
System
.out
.println(time
.getTime().toLocaleString());
time
.add(Calendar
.YEAR
,1);
System
.out
.println(time
.getTime().toLocaleString());
System
.out
.println(time
.getActualMaximum(Calendar
.DAY_OF_MONTH
));
}
SimpleDateFormat类
是一个以与语言环境有关的方式来格式化和解析日期的具体类进行格式化(日期 -> 文本)(文本 -> 日期)常用的时间模式字母 [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PPBM0syp-1603354129908)(时间模式.png)]
时间格式化为字符串
SimpleDateFormat time
= new SimpleDateFormat("yyyy/MM/dd");
Date date
= new Date();
String str
= time
.format(date
);
System
.out
.println(str
);
字符串转换为文字
SimpleDateFormat time
= new SimpleDateFormat("yyyy/MM/dd HH:mm:ss-SS");
Date date2
= time
.parse("1999/02/02 2:2:2-1");
System
.out
.println(time
.format(date2
));
System类
主要用于获取系统的属性数据和其他操作,构造方法私有 [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NV3bd6fq-1603354129910)(system.png)]
static void arraycopy() 复制数组
int[] arrsrc
= {2, 23, 3, 31, 45, 34, 123, 14};
int[] arrdest
= {3, 53, 1, 42, 14, 24, 14, 15, 45};
System
.arraycopy(arrsrc
,1,arrdest
,1,arrsrc
.length
-1);
System
.out
.println(Arrays
.toString(arrsrc
));
System
.out
.println(Arrays
.toString(arrdest
));
out:
[2, 23, 3, 31, 45, 34, 123, 14]
[3, 23, 3, 31, 45, 34, 123, 14, 45]
static void currentTimeMillis() 获取当前系统时间返回毫秒值
1970年1月1日到现在经过的毫秒数 System
.out
.println(System
.currentTimeMillis());
static void gc() 建议JVM进行垃圾回收
public class Student {
private String name
;
private int age
;
public Student(String name
, int age
) {
super();
this.name
= name
;
this.age
= age
;
}
public Student() {
}
public int getAge() {
return age
;
}
@Override
protected void finalize() throws Throwable
{
System
.out
.println("回收了"+name
+" "+age
);
}
}
public class Application {
public static void main(String
[] args
) {
Student s1
= new Student("aaa",3);
new Student("bbb",4);
new Student("ccc",5);
System
.out
.println(s1
.getAge());
System
.gc();
}
}
output:
3
回收了ccc
5
回收了bbb
4
static void exit(int status) 退出JVM,参数0表示正常退出,非0表示异常退出