day05(Object,tostring(),equals(),System,Date,SimpleDateFormat,拆装箱,正则表达式)
Object类,
是所应类的父类;
拥有自己的方法:常用的 红颜色标记的为常用的方法
toString()
用法:打印对象的地址值
getClass() 获取当前类的字节码文件
getName() 获取字节码文件的名字
Integer.toHexString 返回指定参数的16进制字符串
hashCode() 返回该对象的哈希码值 public String toString() { Object类中的tostring值 (地址值)
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
eg:01
如果是字符串 则打印的还是 字符串本身
public class toStringDemo {
public static void main(String[] args) {
String s="a";//如果是字符串则打印的是字符串
String s1=new String("a");
System.out.println(s.toString());
System.out.println(s1.toString());
}
}
输出结果
a
a
问题:toString()在这里打的是字符串值本身 而不是地址值
查看源码:
public String toString() {
return this;
}
可知 String类中重写了 toString()方法 返回值为他本身的值
eg:02
public class Student {
private String name;
private int age;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
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 class toStringDemo {
public static void main(String[] args) {
Student s=new Student(null,16);
Student s1=new Student(null,15);
System.out.println(s.toString());
System.out.println(s1.toString());
}
}
输出结果:
day05.Student@4d43691d
day05.Student@4aa168c
原因:
Student 类中没有重写toString()方法 所以实现的是Object类中的toString()方法
在Student类中重写toString();
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
输出结果:
Student [name=null, age=16]
Student [name=null, age=15]
总结 : 如果该类中没有重写toString ()方法 则输出该对象的地址值。
equals()
比较地址值 和"=="是一个作用(继承了父类的equals())
public boolean equals(Object obj) {
return (this == obj);
}
如果相比较对象的内容,需要重写equals()
eg:03
public class StudentTest {
public static void main(String[] args) {
Student s=new Student(null,15);
Student s1=new Student(null,15);
if (s.equals(s1)) {
System.out.println(true);
}else{
System.out.println(false);
}
}
}
(Student 没有重写equals方法)
比较的是地址值 ( s 和 s1 的地址值不一样)
输出结果:
false
Student 重写equals方法 (采用系统自动生成equals())
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
重写后的方法可以看出 比较的是对象中存储的内容
输出结果:
true
System类
特点:1.不能创建对象
2.很工具类相似
成员方法:
//赋值数组
// static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
src
- 源数组。
srcPos
- 源数组中的起始位置。
dest
- 目标数组。
destPos
- 目标数据中的起始位置。
length
- 要复制的数组元素的数量。
public class Demo01 {
public static void main(String[] args) {
int[] arr={1,2,3,4,5};
int[] arr1=new int[6];
System.arraycopy(arr, 0, arr1, 0, 4);
for (int i = 0; i < arr1.length; i++) {//没有赋值的会存在默认值 int在堆中 默认值是0
System.out.print(arr1[i]+" ");
}
}
}
输出结果:
1 2 3 4 0 0
//获取当前系统时间的毫秒值
static long currentTimeMillis()
public class Demo01 {
public static void main(String[] args) {
long l = System.currentTimeMillis();//获取系统时间的毫秒数
long shi=l/1000/60/60;//转化为小时数
long nian=shi/24/365;//转化为年
System.out.println(nian+1970);//因为这个时间是由1970年开始算的 所以加1970
}
}
输出结果:
2017
//终止当前正在运行的 Java 虚拟机。参数用作状态码;根据惯例,非 0 的状态码表示异常终止。
public static void exit(int status)
public class Demo01 {
public static void main(String[] args) {
for (int i = 0; i <100; i++) {
if (i==10) {
System.out.println(i);
System.exit(0);//虚拟机关闭
}
}
}
} 输出结果:
10
//垃圾回收器
public static void gc()
public class Demo01 {
public static void main(String[] args) throws Throwable {
for (int i = 0; i < 280000; i++) {
new Test();
}
}
}
class Test{
@Override
protected void finalize() throws Throwable { System.out.println("垃圾回收器");
}
}
输出结果:
垃圾回收器
垃圾回收器
垃圾回收器
垃圾回收器
日期类
Date
构造方法
Date();//获取当前系统时间的date对象;
Date(long date);//获取指定毫秒值得date对象
public class Demo01 {
public static void main(String[] args) throws Throwable {
Date d=new Date();
System.out.println(d);
long l=System.currentTimeMillis();//获取当前系统时间的毫秒值
Date d1=new Date(l);
System.out.println(d1);
}
}
输出结果:
Sun Aug 27 19:11:14 GMT+08:00 2017
Sun Aug 27 19:11:14 GMT+08:00 2017
这样的格式看起来不是特别友好 所以我们就存在数据格式化:
SimpleDateFormat
/*
* SimpleDateFormat:
* 格式化:
* Date --- String
* 2049-8-26 2049年8月26日
* String format(Date date)
* 解析:
* String --- Date
* "2049-8-26"
* Date parse(String source)
*
* 构造方法:
* SimpleDateFormat() :使用默认的模式进行对象的构建
* SimpleDateFormat(String pattern) :使用的指定的模式进行对象的构建
*
* 注意:Exception in thread "main" java.text.ParseException: Unparseable date: "49年9月26日 下午1:29"
* 解析的字符串,模式必须和构建对象的模式一样
*
*/
格式化 把不是特别友好的时间进行格式化 格式化我们想要的格式 时间 转化为 字符串
public class Demo01 {
public static void main(String[] args) throws Throwable {
SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
//获取系统当前时间的Date对象d
Date d=new Date();
//格式化
String str = format.format(d);
System.out.println(str);
}
}
解析 把字符串格式转化 时间对象
public class Demo01 {
public static void main(String[] args) throws Throwable {
SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String str="2017年08月27日 19:22:23";
Date date = format.parse(str);
System.out.println(date.toLocaleString());
}
}
输出结果:
2017-8-27 19:22:23
1.1.1 Calendar类概述
Calendar是日历类,在Date后出现,替换掉了许多Date的方法。该类将所有可能用到的时间信息封装为静态成员变量,方便获取。
Calendar为抽象类,由于语言敏感性,Calendar类在创建对象时并非直接创建,而是通过静态方法创建,将语言敏感内容处理好,再返回子类对象,
如下:
Calendar类静态方法
Calendar c = Calendar.getInstance(); //返回当前时间
public class Demo01 {
public static void main(String[] args) throws Throwable {
Calendar c=Calendar.getInstance();//获取1970年日历
Date date=new Date();
c.setTime(date);//设置日历为系统年的日历 //查询
int year=c.get(c.YEAR);//获取系统年份
System.out.println(year);
int mouth=c.get(c.MONTH)+1;//因为美国人是从0月开始的 所以加1
System.out.println(mouth);
int day=c.get(c.DAY_OF_MONTH);//获取系统的月份的第几天
System.out.println(day);
int weekday=c.get(c.DAY_OF_WEEK)-1;//获取这个星期的第几天 美国人一个星期的第一天是星期天
if(weekday==0) { //如果是0则 让它返回为7
weekday=7;
}
System.out.println(weekday);
System.out.println("------------");
//修改
c.set(c.YEAR, 2018);//修改年份为2018年
System.out.println(c.get(c.YEAR));
c.set(c.MONTH, 8);
System.out.println(c.get(c.MONTH));
c.set(c.DAY_OF_MONTH, 8);
System.out.println(c.get(c.DAY_OF_MONTH));
System.out.println("------------");
//添加
c.add(c.YEAR, -1);//给年份+(-1)
System.out.println(c.get(c.YEAR));
c.add(c.MONTH, 1);
System.out.println(c.get(c.MONTH));
c.add(c.DAY_OF_MONTH, 1);
System.out.println(c.get(c.DAY_OF_MONTH)); }
}
输出结果:
2017
8
27
7
------------
2018
8
8
------------
2017
9
9
拆装箱,
4种8类 | |
基本数据类型 装 箱 ↓↓ ↑↑ 箱 拆 包装类 |
自动装箱 基本数据类型>>>>引用数据类型
拆箱 引用数据类型>>>>基本数据类型
public class Demo01 {
public static void main(String[] args) throws Throwable {
Integer i=10;//装箱
int s=i;//拆箱
System.out.println(s);
}
}
正则表达式
正则表达式是专门解决字符串规则匹配的工具。
正则表达式也是一个字符串,用来定义匹配规则。
参照帮助文档,在Pattern类中有简单的规则定义,可以结合字符串类的方法使用。
字符
x 字符 x
\\ 反斜线字符
字符类
[abc] a、b 或 c(简单类)
[^abc] 任何字符,除了 a、b 或 c(否定)
[a-zA-Z] a 到 z 或 A 到 Z,两头的字母包括在内(范围
预定义字符类
. 任何字符(与行结束符可能匹配也可能不匹配)
\d 数字:[0-9]
\D 非数字: [^0-9]
\s 空白字符:[ \t\n\x0B\f\r]
\S 非空白字符:[^\s]
\w 单词字符:[a-zA-Z_0-9]
\W 非单词字符:[^\w]
Greedy 数量词
X? X,一次或一次也没有
X* X,零次或多次
X+ X,一次或多次
X{n} X,恰好 n 次
X{n,} X,至少 n 次
X{n,m} X,至少 n 次,但是不超过 m 次
public class Demo01 {
public static void main(String[] args) throws Throwable {
String s="012365489";
/*判断qq号
8-13位
不能以0开头
不能含有字母*/
boolean b = s.matches("[1-9][0-9]{7,12}");
System.out.println(b);
}
}
day05(Object,tostring(),equals(),System,Date,SimpleDateFormat,拆装箱,正则表达式)的更多相关文章
- Java基础 与时间日期相关的类:System -Date -SimpleDateFormat -Calendar类 -解决后缀.000Z 的时区问题
笔记总结: /**与时间相关的类:System_Date_SimpleDateFormat_Calendar类 * 1.system 类下的currentTimeMillis() * 输出从1970年 ...
- java基础1.5版后新特性 自动装箱拆箱 Date SimpleDateFormat Calendar.getInstance()获得一个日历对象 抽象不要生成对象 get set add System.arrayCopy()用于集合等的扩容
8种基本数据类型的8种包装类 byte Byte short Short int Integer long Long float Float double Double char Character ...
- Java连载78-深入自动拆装箱、Date类和SimpleDateFormat格式化
一.深入自动拆装箱 1.直接举例: public class D78_AutomaticUnpackingAndPacking{ public static void main(String[] ar ...
- JavaSE的包装类,自动装箱和自动拆箱 ,字符窜转换,toString(),equals(), hashCode()的区别
一.基本数据类型和包装类 包装类均位于Java.lang包,包装类和基本数据类型的对应关系如下表所示: Primitive-Type Wrapper-Class byte ...
- 03_10_Object类的toString equals等方法
03_10_Object类的toString equals等方法 1. toString方法 Object类中定义有public String toString()方法,其返回值是String类型,描 ...
- .net学习之继承、里氏替换原则LSP、虚方法、多态、抽象类、Equals方法、接口、装箱拆箱、字符串
1.继承(1)创建子类对象的时候,在子类对象中会为子类对象的字段开辟空间,也会为父类的所有字段开辟空间,只不过父类私有的成员访问不到(2)子类从父类继承父类所有的非私有成员,但是父类的所有字段也会创建 ...
- JAVA基础--toString, equals方法
==比较的是地址 equals比较的是内容. 所以要重写object的equals方法. public class TestEquals { public static void main(Strin ...
- Arrays类的运用,二分法,数组的复制,命令行参数的运用,二维数组,Object,equals
/*Arrays jdk中为了便于开发,给开发者提供了Arrays类, 其中包含了很多数组的常用操作.例如快速输出.排序.查找等.*/ import java.util.Arrays; public ...
- Object.toString()打印“地址”的原理
Object.toString()打印"地址"的原理 @(java) 首先,打印的绝不是地址 public native int hashCode(); public boolea ...
随机推荐
- How to Pronounce PROBABLY
How to Pronounce PROBABLY Share Tweet Share Though this is a content word, it’s frequently reduced. ...
- JPEG和Variant的转换
unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, ...
- js实现jquery函数animate动画效果
<script> function animate(obj, json, interval, sp, fn) { clearInterval(obj.timer); function ge ...
- 简单AOP
代码如下 //使用说明 //1,新加接口与类 //2,新加类并实现ICallHandler类: ExecuteHandler //3,新建特性并实现HandlerAttribute和重写其中的Crea ...
- win64+anaconda+xgboost(转)
Windows下安装python版的XGBoost(Anaconda) XGBoost是近年来很受追捧的机器学习算法,由华盛顿大学的陈天奇提出,在国内外的很多大赛中取得很不错的名次, ...
- python,遍历文件的方法
在做验证码识别时,识别时需要和库里的图片对比,找到最接近的那个图片,然后就行到了用与图片一致的字符命名,获取文件的名称,去将图片的名称读出来作为验证码.以下是我通过网上的资料总结的三种文件遍历的方式, ...
- SpringBoot中使用Redis
在SpringBoot中使用Redis,思路如下: 查询时先查Redis缓存,如果缓存中存在信息,就直接从缓存中获取. 如果缓存中没有相关信息,就去数据库中查找,查完顺便将信息存放进缓存里,以便下一次 ...
- 注册google账号 解决国内手机注册失败的问题
1. PC端下载夜神安卓模拟器.安装,启动. 2. 在模拟器里的市场应用里下载qq邮箱. 3. 启动邮箱,选择gmail,注册.后续一切顺利. 这是迄今为止,唯一注册顺利的方法.其他方法,手机验证一关 ...
- Linux 线程调度
1.线程sleep()后,会让出cpu的时间片,交由其他线程进行抢占cpu. 线程之间正常的切换是依靠时间片的. 当主线程没有结束,且其在所占有的时间片内,并没有结束自己的工作,此时,子线程将会抢占c ...
- Swift4 - 动态计算UITableView中tableHeaderView的高度 - 获取子控件高度和宽度
核心 : /// 获取 子控件高度 func sizeHeaderToFit(view:UIView) { view.setNeedsLayout() view.layoutIfNeeded() le ...