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 ...
随机推荐
- VC中使用ADO操作数据库的方法
源地址:http://blog.csdn.net/xiaobai1593/article/details/7459862 准备工作: (1).引入ADO类 #import "c:\progr ...
- Social media users of the world unite!
Social media users of the world unite!全世界社交媒体用户联合起来!If Plato were alive today, he might well regard ...
- Mysql count+if 函数结合使用
Mysql count+if 函数结合使用 果林椰子 关注 2017.05.18 13:48* 字数 508 阅读 148评论 0喜欢 1 涉及函数 count函数 mysql中count函数用于统计 ...
- 有3D效果的进度条
// The Unofficial Newsletter of Delphi Users - Issue #12 - February 23rd, 1996 unit Percnt3d; (* TPe ...
- 基元线程同步构造之信号量(Semaphore)
信号量(semaphore)不过是由内核维护的 int32变量而已,(说通俗点就是好比一个线程容器里面允许执行的线程数,0计数就是允许执行的0个线程数,1就是允许执行的1个线程数,2就是允许执行的2个 ...
- exception keynote
[exception keynote] Note that the parentheses around this tuple are required, because except ValueEr ...
- mac下将根目录/更改组到普通用户,导致sudo不能用
背景:这是个很愚蠢的故事,我更改了根目录下所有文件的拥有者为普通用户[chown -R xxx / ].结果sudo/su命令都不能用了…… 问题:一旦用sudo命令或su命令就提示: sudo: ...
- 兼容谷歌、火狐、IE7.0以上浏览器div+css实现的带有蒙版的半透明弹窗效果[xyytit]
整个页面变暗的蒙版效果,带有半透明边框的弹窗,用在网站里一定很酷. 最初见与奢饰品购物网站YMALL,后边研究了下,自己做了这个实例. 技术要点:css中几种透明样式的使用.不同的样式在不同的浏览器中 ...
- 【校招面试 之 C/C++】第25题 C++ 智能指针(一)之 auto_ptr
1.智能指针背后的设计思想 我们先来看一个简单的例子: void remodel(std::string & str) { std::string * ps = new std::string ...
- java代码分析及分析工具
一个项目从搭建开始,开发的初期往往思路比较清晰,代码也比较清晰.随着时间的推移,业务越来越复杂.代码也就面临着耦合,冗余,甚至杂乱,到最后谁都不敢碰. 作为一个互联网电子商务网站的业务支撑系统,业务复 ...
