当前位置:网站首页>Object类与常用API
Object类与常用API
2022-08-03 05:09:00 【*super】
一、Object类toString方法
Object类是所有类的基类(即所有类都继承Object类)。
二、Object类equals方法
equals方法重写。
equals比较的是内存中的地址(重写比较内容)。
==比较的是地址。
equals():没有==运行速度快。
三、Date类
import java.util.Date;
/**
* @Author:张金贺
* @Date:2022/6/28 1:42
* @Version 1.0
*/
public class demo07 {
public static void main(String[] args) {
Date date =new Date();//获取当前系统时间
System.out.println(date);
System.out.println(date.getTime());//1656351952001毫秒
//从1970-1-1 8:0:0到当前系统时间的差值 东八区
Date d=new Date(date.getTime());//该毫秒值对应的日期
System.out.println(d);
}
}
四、DateFormat类
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @Author:张金贺
* @Date:2022/6/28 1:50
* @Version 1.0
*/
public class demo08 {
public static void main(String[] args) throws ParseException {
//将当前日期装换成字符串
DateFormat df=new SimpleDateFormat("yyyy-MM-dd");
System.out.println(df.format(new Date()));
//将字符串转换为日期类型
String date ="2000-08-22";
DateFormat df1=new SimpleDateFormat("yyyy-MM-dd");
Date d = df.parse(date);
System.out.println(d);
}
}五、Calendar日历类
import java.util.Calendar;
/**
* @Author:张金贺
* @Date:2022/6/28 1:58
* @Version 1.0
*/
public class demo09 {
public static void main(String[] args) {
Calendar c= Calendar.getInstance();//创建日历对象,当前的所有日期和时间可以获取到
//get方法
System.out.println(c.get(Calendar.YEAR));//获取当前年
System.out.println(c.get(Calendar.MONTH) + 1);//获取当前月
System.out.println(c.get(Calendar.DAY_OF_MONTH));//获取几号
//set方法
c.set(Calendar.YEAR,2018);
System.out.println(c.get(Calendar.YEAR));//获取当前年
//add方法
c.add(Calendar.YEAR,5);
System.out.println(c.get(Calendar.YEAR));
//getTime方法
System.out.println(c.getTime());
}
}
六、System类
1.currentTimeMillis()方法
package demo16;
import java.util.Date;
/**
* @Author:张金贺
* @Date:2022/6/28 2:11
* @Version 1.0
*/
public class test01 {
public static void main(String[] args) {
Long start =System.currentTimeMillis();//获取当前系统时间 单位毫秒 1970-1-1 0:0:0
for (int i = 1; i < 10000; i++) {
System.out.println(i);
}
Long end =System.currentTimeMillis();//获取结束时间 单位毫秒
System.out.println(end-start);
}
}
2.arraycopy()方法

将src数组中前3个元素,复制到dest数组的前3个位置上复制元素前:src数组元素[1,2,3,4,5],dest数组元素[6,7,8,9,10复制元素后:src数组元素[1,2,3,4,5],dest数组元素[1,2,3,9,10]。
package demo16;
/**
* @Author:张金贺
* @Date:2022/6/28 2:17
* @Version 1.0
*/
public class test02 {
public static void main(String[] args) {
int[] src={1,2,3,4,5};
int[] dest={6,7,8,9,10};
System.arraycopy(src,0,dest,0,3);
for (int i: dest) {
System.out.println(i);
}
}
}
七、StringBuilder类(构造方法见JDK)
1.字符串拼接问题
String s = "hello"
s=s+"world" ;
sout(s);
2.常用构造函数
StringBuilder sb= new StringBuilder();//创建了一个字符串容器 初始容量为16个字符
System.out.println(sb);
StringBuilder sb1= new StringBuilder("hello");
System.out.println(sb1);3.常用方法
StringBuilder sb= new StringBuilder();//创建了一个字符串容器 初始容量为16个字符
System.out.println(sb);
sb.append("hello");
System.out.println(sb);public static void main(String[] args) {
StringBuilder sb= new StringBuilder();//创建了一个字符串容器 初始容量为16个字符
System.out.println(sb);
StringBuilder sb1= sb.append("hello");
StringBuilder sb2= sb.append("hello");
sb.append(1);
sb.append(true);
System.out.println(sb);
System.out.println(sb1 == sb2);
System.out.println(sb.toString());
}输出结果:
hellohello1true
true
hellohello1true
八、装箱与拆箱
1.基本类型与引用类型(包装类)

装箱:从基本类型转换为对应的包装类对象。
拆箱:从包装类对象转换为对应的基本类型。
例如:
/**
* @Author:张金贺
* @Date:2022/6/28 2:40
* @Version 1.0
*/
public class demo10 {
public static void main(String[] args) {
Integer i = new Integer(4);//将基本类型4装换成包装类型Integer 装箱
Integer a =Integer.valueOf(5);
int num=i.intValue();//将包装类型转换成基本类型 拆箱
}
}
JDK1.5开始,可以自动装箱与拆箱:
Integer i = 4;//自动装箱。相当于Integer i= Integer.valueOf(4);
i =i + 5;//等号右边:将i对象转成基本数值(自动拆箱) i.intValue()+ 5;
//加法运算完成后,再次装箱,把基本数值转成对象。
2.为什么要有包装类
在某些场合不能使用基本类型必须使用包装类
比如集合能接受的类型为Object,基本类型是无法添加进去的,还有范型也必须使用包装类。
包装类可以为null值,基本类型不能
假设我们要定义一个变量表示分数,如果用基本类型表示的话:int score;默认值为零,如果我想表示分数为空也就是没有参加考试就没法表现了,因为值类型是无法赋空值的,如果使用包装类型Integer score,就可以表示这种情况,因为 Integer的默认值为空。
包装类型是引用类型,里面有一些有用的属性和方法:String num1 ="123";
int num2 = Integer.parseInt(num1);
3.包装类的使用场合
控制器中的参数
如果在控制器中接收一个参数该参数定义成了基本数据类型int,那么传过来的参数如果为空则会报空指针错误,如果定义为其包装类型Integer则不会报错,而是得到一个null值。所以在 RPC方法里面参数和返回值类型都需要用包装类。
pojo类的属性
Pojo类中如果定义成基本类型,数据库中对应的字段为空的时候映射时会出问题。因为基本类型不能赋null。所以在Pojo类中数据类型都需要定义成包装类。
如果是局部变量一般定义成基本类型,因为基本类型存储在栈上,方法执行完毕,栈上的内存空间也随之释放。
边栏推荐
- 设计模式——组合模式、享元模式(Integer缓存)(结构型模式)
- [Harmony OS] [ARK UI] ETS context basic operations
- Super handy drawing tool is recommended
- 内部类、static关键字、final
- How to prepare for the test interface test data
- typescript44-对象之间的类兼容器
- UV decomposition of biotin - PEG2 - azide | CAS: 1192802-98-4 biotin connectors
- 刚上线就狂吸70W粉,新型商业模式“分享购”来了,你知道吗?
- MOSN 反向通道详解
- Installation of Apache DolphinScheduler version 2.0.5 distributed cluster
猜你喜欢

Tributyl-mercaptophosphane "tBuBrettPhos Pd(allyl)" OTf), 1798782-17-8

VR全景展打造专属元宇宙观展空间

Windows 安装PostgreSQL

Build your own web page on the Raspberry Pi (2)

shell script loop statement

IO process thread -> thread -> day5

PotPlayer实现上班摸鱼电视自由

修饰生物素DIAZO-生物素-PEG3-DBCO|重氮-生物素-三聚乙二醇-二苯基环辛炔

测试人员的价值体现在哪里

Kotlin-Flow common encapsulation class: the use of StateFlow
随机推荐
业务表解析-余额系统
Shell之条件语句
2022/08/02 Study Notes (day22) Multithreading
tag单调栈-单调栈预备知识-lt.739. 每日温度
c语言结构体中的冒泡排序
Practical application of WebSocket
三丁基-巯基膦烷「tBuBrettPhos Pd(allyl)」OTf),1798782-17-8
Fluorescent marker peptides FITC/AMC/FAM/Rhodamine TAMRA/Cy3 / Cy5 / Cy7 - Peptide
odps的临时查询能在写sql的时候就给结果一个命名不?
Jmeter 模拟多用户登录的两种方法
【Harmony OS】【ARK UI】轻量级数据存储
刚上线就狂吸70W粉,新型商业模式“分享购”来了,你知道吗?
Exception (abnormal) and Error (error) difference analysis
Harmony OS ets ArkUI 】 【 】 the development basic page layout and data connection
接口测试实战| GET/POST 请求区别详解
链动2+1模式简单,奖励结构丰厚,自主裂变?
Where is the value of testers
Modified BiotinDIAZO-Biotin-PEG3-DBCO|diazo-biotin-tripolyethylene glycol-diphenylcyclooctyne
阿里云对象存储oss私有桶生成链接
【生物素叠氮化物|cas:908007-17-0】价格_厂家