一.String
1.String的特性
- String类:代表字符串。java程序中的所有字符串字面值(如“abc”)都作为此类的实例实现。
- String是一个fianl类,代表不可变得字符序列,不可被继承。简称 :不可 变的特性
体现:1.党对字符串重新赋值时,需要重写指定内存区域赋值,不能使用原有的value进行赋值。
2.当对现有的字符进行连接操作时,也需要重新指定内存区域赋值,不能使用原有的value进行赋值。
3.当调用String的replace()方法修改指定字符或字符串时,也需要重新指定内存区域赋值,不能使用原有的value进行赋值。
- 字符串是常量,用双引号引起来表示。它们的值在创建后不能更改。
- String对象的字符内容是存储在一个字符数组value[ ] 中的
- String 实现了Serializable 接口:表示字符串是支持序列化的。
实现了Comparable 接口:表示String可以比较大小
- 通过字面量的方式(区别于new)给一个字符串赋值,此时的字符串声明在字符串常量池中。
- 字符串常量池中是不会存储相同内容的字符串的。
public void test1(){
String s1 = "abc"; //字面量
String s2 = "abc";
s1 = "hello";
System.out.println(s1); //hell0
System.out.println(s2); //abc
String s3 ="abc";
s3 +="def";
System.out.println(s3); //abcdef
System.out.println(s2);
System.out.println("===============");
String s4 = "abc";
String s5=s4.replace('a','m');//x.replace('a','b')方法表示把x中的a换为b
System.out.println(s4);
System.out.println(s5);
2.常用方法:
public int length() : 返回字符串的长度
public char charAt(int index):根据下标获取字符
public boolean containers(String str) : 判断当前字符串中是否包含str
public char[ ] toCharArray( ) :将字符串转换成数组
public int indexOf(String str):查找str首次出现的下标,存在,则返回该下标;若不存在,则返回-1
public int lastIndexOf(String str):查找字符串在当前字符串中最后一次出现的下标索引
public String trim( ) :去掉字符串前后的空格
public String toUpperCase( ) :将小写转成大写
public String toUpperCase( ) :将大写转成小写
public boolean endWith(String str) : 判断字符串是否以str结尾
public boolean startsWith(String str) : 判断字符串是否以str开头
public String replace(char oldChar,char new Char) :将旧字符替换成新字符
public String[ ] split(String str ) :根据str做拆分
import java.util.Arrays;
public class S_String {
public static void main(String[] args) {
String name="hello";
name="zhangsan";
String name2="zhangsan";
//演示字符串的另一种创建方式,new String();
String str =new String("java");
String str2 = new String("java");
System.out.println(str==str2);
System.out.println(str.equals(str2));
System.out.println("=====字符串方法的使用1=====");
//字符串方法的使用
//1.length();返回字符串的长度
//2.charAt(int index); 返回某个位置的字符
//3.contains(String str);判断是否包含某个子字符串
String content="java是一个好的编程语言,java真香!";
System.out.println(content.length());
System.out.println(content.charAt(content.length()-1));//最后一个位置的字符
System.out.println(content.contains("java"));
System.out.println(content.contains("好"));
System.out.println("=====字符串方法的使用2=====");
//字符串方法的使用
//4.toCharArray( );返回字符串对应道德数组
//5.indexOf( ); 返回子字符串首次出现的位置
//6.lastIndexOf( );返回字符串最后一次出现的位置
System.out.println(Arrays.toString(content.toCharArray()));
System.out.println(content.charAt(content.length()-1));//最后一个位置的字符
System.out.println(content.indexOf("java"));
System.out.println(content.indexOf("java",4));//从第四个字符开始往后数
System.out.println(content.lastIndexOf("java"));
System.out.println("=====字符串方法的使用3=====");
//字符串方法的使用
//7.trim( );去掉字符串前后的空格
//8.toUpperCase( ); 将小写转成大写 toLowerCase();将大写转成小写
//9.endWith(str);判断字符串是否以str结尾 startsWith(str);判断字符串是否以str开头
String content2=" hello world ABC ";
System.out.println(content2.trim());
System.out.println(content2.toUpperCase());
System.out.println(content2.toLowerCase());
String content3="hello,java";
System.out.println(content3.endsWith("java"));
System.out.println(content3.startsWith("hello"));
System.out.println("=====字符串方法的使用4=====");
//字符串方法的使用
//10.replace(char old,char new );将旧字符替换成新字符
//11.split(String str) :根据str做拆分
String content4="java我能熟练运用";
System.out.println(content4.replace("java","php"));
String say="java is the best programing language,java xiang";
String[] arr =say.split("[ ,]");
System.out.println(arr.length);
for(String string : arr){
System.out.println(string);
}
//补充两个方法equals、compareTo( );比较大小
System.out.println("=======补充========");
String s1="hello";
String s2="HELLO";
System.out.println(s1.equalsIgnoreCase(s2));
String s3="abc";//97
String s4="xyz";//120
System.out.println(s3.compareTo(s4)); //-23 比较首字符的ASC码
String s5="abc";
String s6="xyztyh";
System.out.println(s5.compareTo(s6)); //-3 比较字符串长度
}
}
二、包装类
1.基本规则:
- 基本数据类型所对应的引用类型
- Object类可统一所有数据,包装类的默认是null
基本数据类型 | 包装类型 |
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
boolean | Boolean |
char | Character |
import java.sql.SQLOutput;
public class Student {
@Override
public String toString() {
return "Student{" + "neme='" + name + '\'' + ", age=" + age + '}';
}
public static void main(String[] args) {
System.out.println("==============1.getClass================");
//1. getClass 方法
Student a1 = new Student("aaa",8);
Student a2 = new Student("bbb",10);
//判断s1和s2是不是同一个类型
Class Class1 = a1.getClass();
Class Class2 = a2.getClass();
if(Class1==Class2){
System.out.println("a1和a2是同一个类型的");
}else{
System.out.println("a1和a2不是同一个类型的");
}
System.out.println("==============2.hashCode================");
//2. hashCode 方法
System.out.println(a1.hashCode());
System.out.println(a2.hashCode());
Student a3=a1;
System.out.println(a3.hashCode());
System.out.println("==============3.toString================");
//3. toString 方法
System.out.println(a1.toString());
System.out.println(a2.toString());
System.out.println("==============4.equals================");
//4. equals 方法
System.out.println(a1.equals(a2));
Student a4 =new Student("小明",17);
Student a5 =new Student("小明",17);
System.out.println(a4.equals(a5));
}
private String name;
private int age;
public Student(){
}
public Student(String name,int age) {
this.name = name;
this.age = age;
}
public String getNeme() {
return name;
}
public void setNeme(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public boolean equals(Object obj) {
//1.判断两个对象是否是同一引用
if(this==obj){
return true;
}
//2.判断obj是否null
if(obj==null){
return false;
}
/*3.判断是否是同一类型
if(this.getClass()==obj.getClass()){
}
*/
//intanceof 判断对象是否是某种类型
if(obj instanceof Student){
//4.强制类型转换
Student s=(Student) obj;
//5.比较属性
if(this.name.equals(s.getNeme())&&this.age==s.getAge()){
return true;
}
}
return false;
}
}
2.类型转换与装箱、拆箱
8种包装类提供不同类型间的转换方式:
- Number父类中提供的6个共性方法
- parseXXX( )静态方法
- valueOf( )静态方法
注意:需保证类型兼容,否则抛出NumberFormatException异常
public class BaoZhuang {
public static void main(String[] args) {
//类型转换:装箱,基本类型转为引用类型的过程
//基本类型
int num=18;
//使用Integer类创建对象
Integer integer1=new Integer(num);
Integer integer2= Integer.valueOf(num);
System.out.println("=====装箱======");
System.out.println(integer1);
System.out.println(integer2);
//类型转型:拆箱,引用类型转成基本类型
Integer integer3=new Integer(100);
int num2=integer3.intValue();
System.out.println("=====拆箱======");
System.out.println(integer3);
//JDK1.5之后,提供自动装箱
int age=30;
//自动装箱
Integer integer4=age;
System.out.println("=====自动装箱======");
System.out.println(integer4);
//自动拆箱
int age2=integer4;
System.out.println("=====自动拆箱======");
System.out.println(age2);
System.out.println("===========基本类型和字符串之间的转换============");
//基本类型和字符串之间的转换
//1.基本类型转成字符串
int n1=15;
//1.1 使用+号
String s1 =n1+"";
//1.2 使用Integer中的toString()方法
String s2=Integer.toString(n1);
String s3=Integer.toString(n1,16); //返回n1的16进制
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
System.out.println("=================");
//2.字符串转成基本类型
String str="150";
//使用Integer。parseXXX();
int n2=Integer.parseInt(str);
System.out.println(n2);
//Boolean字符串形式转成基本类型, "ture"--->true 非“true"----->false
String str2="false";
boolean b1=Boolean.parseBoolean(str2);
System.out.println(b1);
}
}
3.整数缓冲区(面试题)
Java预先创建了256个常用的整数包装类型对象
在实际应用当中,对已创建的对象进行复用
public class demo1 {
public static void main(String[] args) {
//面试题
Integer integer1 = new Integer(100);
Integer integer2 = new Integer(100);
System.out.println(integer1==integer2); //两者地址不相同,结果为false
Integer integer3=100; //自动装箱(在-128~127之间,直接赋值,未在则使用new Integer())
Integer integer4=100;
System.out.println(integer3==integer4); //true
Integer integer5=200; //自动装箱
Integer integer6=200;
System.out.println(integer3==integer4); //false
}
}
三、内部类
内部类:成员内部类 、静态内部类、局部内部类、匿名内部类
1、内部类:
1.定义:在一个类的内部再定义一个完整的类
2.特点:编译之后可生成独立的字节码文件
内部类可以直接访问外部类的私有成员,而不破坏封装性
可为外部类提供必要的内部功能组件
public class demo01 { //外部类
private String name; //在外部类里定义一个私有变量name
public void out() { //在外部类里定义的一个方法
System.out.println("这是外部类的方法");
}
class Inner { //内部类
public void in() { //在内部类里定义的一个方法
System.out.println("这是内部类的方法");
System.out.println(name); //访问外部类的私有变量name
}
}
public static void main(String[] args) {
demo01 a = new demo01();
a.out();
demo01.Inner b = a.new Inner(); //通过这个外部类来实例化内部类
b.in();
}
}
成员内部类:
- 在类的内部定义,与实例变量、实例方法同级别的类。
- 外部类的一个实例部分,创建内部类对象时,必须依赖外部类对象
- 当外部类、内部类存在重名属性时,会优先访问内部类属性。
- 若要输出外部类属性,则需要使用 外部类名.this.属性名
- 成员内部类不能定义static(静态类),但可以定义static final(静态常量)
eg: private static final String name = "王五";
//外部类
public class demo02 {
//实例变量
private String name = "张三";
private int age = 20;
//内部类
class Inner {
private String address = "北京";
private String phone = "110";
private String name="李四";
//内部类定义一个方法
public void show() {
//打印外部类的属性
System.out.println(demo02.this.name);
System.out.println(age);
System.out.println("=================");
//打印内部类中的属性
System.out.println(name);
System.out.println(address);
System.out.println(phone);
System.out.println("=================");
//属性和外部类数属性名字相同,优先输出内部类,若要输出外部类属性,则需要使用 外部类名.this.属性名
System.out.println(demo02.this.name);
System.out.println(name);
}
}
public static void main(String[] args) {
//1,创建一个外部类对象
demo02 a1 = new demo02();
//2.创建内部对象
demo02.Inner a2 = a1.new Inner();
//1.2可以合在一起一步到位
demo02.Inner a3 = new demo02().new Inner();
a2.show();
}
}
静态内部类:
- 不依赖外部类对象,可直接创建或通过类名访问,可声明静态成员。
- 静态内部类和外部类的级别相同
- 调用外部类的属性:1.先创建外部类对象
2.调用外部类对象的属性
- 调用静态内部类的属性和方法:用类名来调用
格式: 静态内部类类名 . 属性
public class demo03 {
private String name="xxx";
private int age = 18;
//静态内部类:级别和外部类相同
static class Inner{
private String address="上海";
private String phone ="111";
//静态成员
private static int count =10000;
public void show(){
//调用外部类的属性
//1.先创建外部类对象
//2.调用外部类对象的属性
demo03 a1 = new demo03();
System.out.println(a1.name); // 分步写
System.out.println(new demo03().age); //一步写
System.out.println("===========");
//调用静态内部类的属性和方法
System.out.println(address);
System.out.println(phone);
System.out.println("===========");
//调用静态内部类的静态属性(用类名来调用)
System.out.println(Inner.count);
}
}
public static void main(String[] args) {
//静态内部可以直接被创建,不必依赖外部类,级别和外部类一样
demo03.Inner aaa = new demo03.Inner();
//调用方法
aaa.show();
}
}
局部内部类 :
- 定义在外部类方法中,作用范围和创建对象范围仅限于当前方法。
- 局部内部类访问外部类当前方法中的局部变量时,因无法保障变量的生命周期和自己相同,变量必须修饰为final
- 只有局部内部类调用了这个局部变量,这个变量才会会变成常量
- 局部内部类,不可以加任何的访问修饰符
- 限制类的使用
public class demo04 {
public static void main(String[] args) {
demo04 a1 = new demo04();
a1.show();
}
private String name = "张三";
private int age=35;
public void show(){
//定义局部变量
String address = "深圳";
//局部内部类:注意不能加任何访问修饰符
class Inner{
private String phone ="111";
private String email = "453456789876@qq.com";
//静态成员
private final static int count =123;
public void show2(){
//访问外部类的属性
System.out.println(demo04.this.name);
System.out.println(demo04.this.age);
System.out.println("==============");
//访问内部类的属性
System.out.println(this.phone);
System.out.println(this.email);
System.out.println("==============");
//访问局部变量:jdk1.7要求,变量必须是常量final ,jdk1.8会自动添加final
//只有局部内部类调用了这个局部变量,这个变量才会会变成常量
System.out.println(address);
}
}
//创建局部内部类对象
Inner a2 = new Inner();
a2.show2();
}
}
匿名内部类:
- 没有类名的局部内部类(一切特征都与局部内部类相同)
- 必须继承一个父类或实现一个接口
- 定义类、实现类、创建对象的语法合并,只能创建一个该类的对象
- 优点:减少代码量
- 缺点:可读性较差
public class TestUsb {
public static void main(String[] args) {
//创建接口类型的变量
USB a1 = new Mouse();
a1.service();
/*
//局部内部类
class fan implements USB{
@Override
public void service() {
System.out.println("连接电脑成功,风扇开始工作了...");
}
}
//使用局部内部类创建对象
USB a2 = new fan();
a2.service();
*/
//使用匿名内部类优化(相当于创建于一个局部内部类)
USB a2 = new USB() {
@Override
public void service() {
System.out.println("连接电脑成功,风扇开始工作了...");
}
};
a2.service();
}
}
//实现接口,要写下接口的所有方法
public class Mouse implements USB {
@Override
public void service() {
System.out.println("连接电脑成功,鼠标开始工作了...");
}
}
package Oop.ChangYong.demo05;
//接口
public interface USB {
//服务
void service();
}
四、Object类
1.规则
- 超类、基类、所有类的直接或间接父类,位于继承树的最顶层。
- 任何类,如没有书写extends显示继承某个类,都默认直接继承Object类,否则为间接继承。
- Object类中所定义的方法,是所有对象都具备的方法
- Object类型可以存储任何对象。
2.作用
1.作为参数,可接受任何对象
2.作为返回值,可返回任何对象
3.getClass( )方法:
- public final Class<?> getClass( ) { }
- 返回引用中存储的实际对象类型
- 应用:通常用于判断两个引用中实际存储对象类型是否一致
4.hashCode( ) 方法
- public int hashCode( ) { }
- 返回该对象的哈希值
- 哈希值根据对象的地址或字符串或数字使用hash算法计算出来的 int 类型的数值。
- 一般情况下相同对象返回相同哈希码
5.toString( )方法
- public String toString( ) { }
- 返回该对象的字符串表示(表现形式)
- 可以根据程序需求覆盖该方法,如:展示对象各个属性值
6.equals( )方法:判断两个对象是否相等
- public boolean equals(Object obj){}
- 默认实现(this==obj),比较两个对象地址是否相同
- 可进行覆盖,比较两个对象的内容是否相同
7.equals( ) 方法覆盖步骤:
1.比较两个引用是否指向同一个对象
2.判断obj是否为null
3.判断两个引用指向的实际对象类型是否一致
4.强制类型转换
5.依次比较各个属性值是否相同
8.finalize()方法
- 当对象被判定为垃圾对象时,由JVM自动调用此方法,用以标记垃圾对象,进入回收队列
- 垃圾对象:没有有效引用指向对象时,为垃圾对象
- 垃圾回收:由GC销毁垃圾对象,释放数据存储空间
- 自动回收机制:JVM的内存耗尽,一次性回收所有垃圾对象
- 手动回收机制:使用System.gc( );通知JVM执行垃圾回收
import java.sql.SQLOutput;
public class Student {
@Override
public String toString() {
return "Student{" + "neme='" + name + '\'' + ", age=" + age + '}';
}
public static void main(String[] args) {
System.out.println("==============1.getClass================");
//1. getClass 方法
Student a1 = new Student("aaa",8);
Student a2 = new Student("bbb",10);
//判断s1和s2是不是同一个类型
Class Class1 = a1.getClass();
Class Class2 = a2.getClass();
if(Class1==Class2){
System.out.println("a1和a2是同一个类型的");
}else{
System.out.println("a1和a2不是同一个类型的");
}
System.out.println("==============2.hashCode================");
//2. hashCode 方法
System.out.println(a1.hashCode());
System.out.println(a2.hashCode());
Student a3=a1;
System.out.println(a3.hashCode());
System.out.println("==============3.toString================");
//3. toString 方法
System.out.println(a1.toString());
System.out.println(a2.toString());
System.out.println("==============4.equals================");
//4. equals 方法
System.out.println(a1.equals(a2));
Student a4 =new Student("小明",17);
Student a5 =new Student("小明",17);
System.out.println(a4.equals(a5));
}
private String name;
private int age;
public Student(){
}
public Student(String name,int age) {
this.name = name;
this.age = age;
}
public String getNeme() {
return name;
}
public void setNeme(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public boolean equals(Object obj) {
//1.判断两个对象是否是同一引用
if(this==obj){
return true;
}
//2.判断obj是否null
if(obj==null){
return false;
}
/*3.判断是否是同一类型
if(this.getClass()==obj.getClass()){
}
*/
//intanceof 判断对象是否是某种类型
if(obj instanceof Student){
//4.强制类型转换
Student s=(Student) obj;
//5.比较属性
if(this.name.equals(s.getNeme())&&this.age==s.getAge()){
return true;
}
}
return false;
}
}
五、可变字符串
StringBuffer:可变长字符串,JDK1.0提供,运行效率慢、线程安全。
StringBuilder:可变长字符串,JDK5.0提供,运行效率快、线程不安全。
/*
StringBuffer 和 StringBulider 的使用
和String区别(1)效率比String高 (2)比String节省内存
*/
public class S_String2 {
public static void main(String[] args) {
StringBuffer sb=new StringBuffer();
//1.append();追加
sb.append("java世界第一");
System.out.println(sb.toString());
sb.append("java真香");
System.out.println(sb.toString());
sb.append("java不错");
System.out.println(sb.toString());
//2. insert( );添加
sb.insert(0,"我在最前面");
System.out.println(sb.toString());
//3. replace();替换
sb.replace(0,5,"hello");
System.out.println(sb.toString());
//4. delete();删除
sb.delete(0,5);
System.out.println(sb.toString());
//清空
sb.delete(0,sb.length());
System.out.println(sb.length());
}
}
六、BigDecima
位置:java.math包中
作用:精确计算浮点数
创建方式:BigDdecimal bd =new BigDecimal("1.0");
import java.math.BigDecimal;
import java.sql.SQLOutput;
public class BigDecimal_1 {
public static void main(String[] args) {
double d1=1.0;
double d2=0.9;
System.out.println(d1-d2);
//面试题
double result =(1.4-0.5)/0.9;
System.out.println(result);
//BigDecimal,大的浮点数精确计算
BigDecimal bd1 = new BigDecimal("1.0");
BigDecimal bd2 = new BigDecimal("0.9");
//subtract:减法
BigDecimal r1=bd1.subtract(bd2);
System.out.println(r1);
//add:加法
BigDecimal r2=bd1.add(bd2);
System.out.println(r2);
//multiply:乘法
BigDecimal r3=bd1.multiply(bd2);
System.out.println(r3);
//divide:除法
BigDecimal r4=new BigDecimal("1.4").subtract(new BigDecimal("0.5")).divide(new BigDecimal("0.9"));
System.out.println(r4);
}
}
七、Date():
import java.util.Date;
public class Date_1 {
public static void main(String[] args) {
//1.创建Date对象
//今天
Date date1 = new Date();
System.out.println(date1.toString());
System.out.println(date1.toLocaleString());
//昨天
Date date2 = new Date(date1.getTime()-(60*60*24*1000));
System.out.println(date2.toLocaleString());
//2.方法 after before
boolean b1=date1.after(date2);
System.out.println(b1);
boolean b2=date1.before(date2);
System.out.println(b2);
//比较 compareTo()
int d=date1.compareTo(date2);
System.out.println(d);
//比较是否相等equals();
boolean b3=date1.equals(date2);
System.out.println(b3);
}
}
八、Calendar
- Calendar提供了获取或设置各种日历字段的方法
- 构造方法 protected Calendar():由于修饰符是protected,所以无法直接创建对象
- 其他方法:
方法名 | 说明 |
static Calendar getlnstance( ) | 使用默认时区和区域获取日历 |
void set(int year,int mouth,int date,int hourofday, int minute,int second) | 设置日历的年、月、日、时、分、秒 |
int get (int field) | 返回给定日历字段的值。字段比如年、月、日等 |
void setTime(Date date) | 返回一个Date表示此日历的时间,Date-Calendar |
Date getTime() | 返回一个Date表示此日历的时间,Calendar-Date |
void add(int field , int amout) | 按照日历的规则,给指定字段添加或减少时间量 |
long getTimeInMillies() | 毫秒为单位返回该日历的时间值 |
import java.util.Calendar;
public class Calendar_1 {
public static void main(String[] args) {
//1.创建Calendar对象
Calendar calendar=Calendar.getInstance();
System.out.println(calendar.getTime().toLocaleString());
System.out.println(calendar.getTimeInMillis());
//2.获取时间信息
//获取年
int year=calendar.get(Calendar.YEAR);
//获取月(0~11)
int month=calendar.get(Calendar.MONTH);
//获取日
int day=calendar.get(Calendar.DAY_OF_WEEK);//Date
//获取小时
int hour=calendar.get(Calendar.DAY_OF_MONTH); //HOUR 12小时制 HOUR_OF_DAY 24小时制
//获取分钟
int minute=calendar.get(Calendar.MINUTE);
//获取秒
int second=calendar.get(Calendar.SECOND);
System.out.println(year+"年"+(month+1)+"月"+day+"日"+hour+":"+minute+":"+second);
//3.修改时间
Calendar calendar2=Calendar.getInstance();
calendar2.set(Calendar.DAY_OF_MONTH,5);
System.out.println(calendar2.getTime().toLocaleString());
//4.add方法修改时间
calendar2.add(Calendar.HOUR,-1);
System.out.println(calendar2.getTime().toLocaleString());
//5.补充方法
int max= calendar2.getActualMaximum(Calendar.DAY_OF_MONTH);
int min= calendar2.getActualMinimum(Calendar.DAY_OF_MONTH);
System.out.println(max);
System.out.println(min);
}
}
十、SimpleDateFormat
- SimpleDateFormat是一个以与语言环境有关的方式来格式化和解析日期的具体类
- 进行格式化(日期 --->文本)、解析 ( 文本------>日期)
- 常用的时间模式字母
字母 | 日期或时间 | 示例 |
y | 年 | 2010 |
M | 年中月份 | 08 |
d | 月中天数 | 10 |
H | 1天中小时数(0~23) | 22 |
m | 分钟 | 16 |
s | 秒 | 59 |
S | 毫秒 | 367 |
import java.text.SimpleDateFormat;
import java.util.Date;
public class SimpleDateFormat_1 {
public static void main(String[] args) throws Exception{
//1.创建SimpleDateFormat对象 y 年 M 月
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH:mm:ss");
//2.创建Date
Date date=new Date();
//格式化date(把日期转成字符串)
String str =sdf.format(date);
System.out.println(str);
//解析(把字符串转为日期)
Date date2=sdf.parse("1990年05月01日12:56:45");
System.out.println(date2);
}
}
十一、System类
System系统类,主要用于获取系统的属性数据和其他操作,构造方法是私有的
方法名 | 说明 |
static void arraycopy(...) | 复制数组 |
static long currentTimeMillis(); | 获取当前系统时间,返回的是毫秒值 |
static void ga(); | 建议JVM赶快启动垃圾回收器回收垃圾 |
static void exit(int status); | 退出jvm,如果参数是0表示正常退出jvm,非0表示异常退出jvm。 |
import com.sun.xml.internal.messaging.saaj.packaging.mime.util.QDecoderStream;
public class System_1 {
public static void main(String[] args) {
//1.arraycopy:数组的复制
/*
src:源数组
srcPos:从哪个位置开始复制0
dest:目标数组
destPos:目标数组的位置
length:复制的长度
*/
int[] arr={20,28,25,4,3,4,5,654,78,45};
int[] dest=new int [10];
System.arraycopy(arr,0,dest,0,arr.length);
for(int i=0;i< dest.length;i++){
System.out.println(dest[i]);
}
//Arrays.copyOf(original,newLength)
System.out.println(System.currentTimeMillis());
long start=System.currentTimeMillis();
for(int i=-2345678;i<99999999;i++){
for(int j=0;j<9999999;j++){
int result =i+j;
}
}
//2.获取毫秒数
long end=System.currentTimeMillis();
System.out.println("用时:"+(end-start));
Student s1 = new Student("aaa", 19);
Student s2 = new Student("bbb", 7);
Student s3 = new Student("ccc",8);
//3.回收垃圾
System.gc();//告诉垃圾回收期回收
//4.推出jvm
System.exit(0);
System.out.println("程序结束了。。。。");
}
}
class Student{
private String name;
private int age;
public Student(String x, int i) {
}
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;
}
public String toString (){
return "Student [name="+ name + ",age="+age+"]";
}
@Override
protected void finalize() throws Throwable {
System.out.println("回收了"+name+""+" "+age);
}
}