您的位置:首页 > 编程语言 > Java开发

(三)、Java复习笔记之常见对象

2017-10-29 21:19 274 查看

31、Object类

Object类概述

类层次结构的根类

所有对象(包括数组)都实现这个类的方法。

构造方法

public Object()

回想面向对象中为什么说:

子类的构造方法默认访问的是父类的无参构造方法

Object类的hashCode()方法

public int hashCode()

返回该对象的哈希码值。默认情况下,该方法会根据对象的地址来计算。

不同对象的,hashCode()一般来说不会相同。但是,同一个对象的hashCode()值肯定相同。

Object类的getClass()方法

public final Class getClass()

返回此 Object 的运行时类。

可以通过Class类中的一个方法,获取对象的真实类的全名称。

public String getName()

Object类的toString()方法

public String toString()

它的值等于:

getClass().getName() + “@” + Integer.toHexString(hashCode())

由于默认情况下的数据对我们来说没有意义,一般建议重写该方法。

Object类的equals()方法

指示其他某个对象是否与此对象“相等”。

默认情况下比较的是对象的引用是否相同。

由于比较对象的引用没有意义,一般建议重写该方法。

32、==号和equals方法的区别

==是一个比较运算符,基本数据类型比较的是值,引用数据类型比较的是地址值

equals方法只能比较引用数据类型,所有对象都会继承Object类中的方法,如果没有重写Object类中的equals方法,比较的是地址值,重写后比较的是对象中的属性

33、String类

String 类代表字符串。字符串是常量,它们的值在创建之后不能更改。

Java 语言提供字符串串联符号(”+”)。字符串串联是通过 StringBuilder(或 StringBuffer)类及其 append 方法实现的。字符串转换是通过 toString 方法实现的。变量名指向了一个新的内存空间。

面试题

//String是字符串常量。常量池中没有这个字符串对象就创建,有就直接用
String s1 = "abc";
String s2 = "abc";
System.out.println(s1 == s2); //true
System.out.println(s1.equals(s2)); //true

//创建了几个对象?2个,一个在常量池中,一个在堆内存中
String s1 = new String("abc");
System.out.println(s1);

//s1记录的是堆内存对象的地址值,s2记录的是常量池中的地址值
String s1 = new String("abc");
String s2 = "abc";
System.out.println(s1 == s2); //false
System.out.println(s1.equals(s2)); //true

//java中的常量优化机制,s1的三个常量直接在常量池中创建一个"abc"常量
String s1 = "a" + "b" + "c";
String s2 = "abc";
System.out.println(s1 == s2); //true
System.out.println(s1.equals(s2)); //true

//s3首先在堆中创建一个StringBuilder,然后创建一个String对象
String s1 = "ab";
String s2 = "abc";
String s3 = s1 + "c";
System.out.println(s3 == s2); //false
System.out.println(s3.equals(s2)); //true


34、String、StringBuffer与StringBuilder之间区别

(1)三者在执行速度方面的比较:StringBuilder > StringBuffer > String

(2)String是一个不可变的字符序列,StringBuffer,StringBuilder是可变的字符序列

HashSet<StringBuilder> hs = new HashSet<>();
StringBuilder sb1 = new StringBuilder("Hello ");
StringBuilder sb2 = new StringBuilder("Hello World");
hs.add(sb1);
hs.add(sb2);
System.out.println(hs); //[Hello , Hello World]

StringBuilder sb3 = sb1; //指向sb1的内存空间
sb3.append("World"); //在sb1的内存空间增加World
System.out.println(hs); //[Hello World, Hello World],破坏了HashSet键值的唯一性


HashSet<String> hs = new HashSet<>();
String s1 = new String("Hello ");
String s2 = new String("Hello World");
hs.add(s1);
hs.add(s2);
System.out.println(hs); //[Hello , Hello World]

String s3 = s1; //s3指向s1的内存空间
s3 += "World"; //s3指向了一个新的内存空间
System.out.println(hs); //[Hello , Hello World]


(3)StringBuffer线程安全、StringBuilder线程不安全

35、Arrays类

Arrays类概述

针对数组进行操作的工具类。

提供了排序,查找等功能。

成员方法

public static String toString(int[] a)

public static void sort(int[] a)

public static int binarySearch(int[] a,int key)

Arrays.sort自定义比较器

public class Test {

public static void main(String[] args) {
Person p[] = {new Person("tom", 20), new Person("tyshawn", 22), new Person("adam", 18)};
Arrays.sort(p, new Comparator<Person>() {
@Override
public int compare(Person o1, Person o2) {
//              return o1.getAge() - o2.getAge();  //升序
return o2.getAge() - o1.getAge();  //降序
}

});

for(int i = 0; i < p.length; i++){
System.out.print(p[i].toString());
}
}
}

class Person {
private String name;
private int age;

public Person(String name, int age) {
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;
}

@Override
public String toString() {
return "[" + name + "," + age + "]";
}
}


36、自动装箱和自动拆箱

自动装箱:把基本类型转换为包装类类型

自动拆箱:把包装类类型转换为基本类型

面试题

Integer i1 = new Integer(97);
Integer i2 = new Integer(97);
System.out.println(i1 == i2); //false
System.out.println(i1.equals(i2)); //true

Integer i3 = new Integer(197);
Integer i4 = new Integer(197);
System.out.println(i3 == i4); //false
System.out.println(i3.equals(i4)); //true

//-128到127是byte的取值范围,如果在这个取值范围内,自动装箱就不会新创建对象,而从常量池中获取,否则会新创建对象
Integer i5 = 97;
Integer i6 = 97;
System.out.println(i5 == i6); //true
System.out.println(i5.equals(i6)); //true

Integer i7 = 197;
Integer i8 = 197;
System.out.println(i7 == i8);//false
System.out.println(i7.equals(i8)); //true


37、正则表达式

字符类

[abc] a、b 或 c(简单类)

[^abc] 任何字符,除了 a、b 或 c(否定)

[a-zA-Z] a到 z 或 A到 Z,两头的字母包括在内(范围)

[0-9] 0到9的字符都包括

预定义字符类

. 任何字符。

\d 数字:[0-9]

\w 单词字符:[a-zA-Z_0-9]

Greedy 数量词

X? X,一次或一次也没有

X* X,零次或多次(包括一次)

X+ X,一次或多次

X{n} X,恰好 n 次

X{n,} X,至少 n 次

X{n,m} X,至少 n 次,但是不超过 m 次

正则表达式的分组功能

捕获组可以通过从左到右计算其开括号来编号。例如,在表达式 ((A)(B(C))) 中,存在四个这样的组:

1 ((A)(B(C)))

2 (A

3 (B(C))

4 (C)

组零始终代表整个表达式。

//叠词快快乐乐
String regex = "(.)\\1(.)\\2"; // \\1代表第一组又出现一次,\\2代表第二组又出现一次
System.out.println("快快乐乐".matches(regex)); //true
System.out.println("高兴兴兴".matches(regex)); //false
System.out.println("高兴高兴".matches(regex)); //false

//叠词 高兴高兴
String regex2 = "(..)\\1";
System.out.println("高兴高兴".matches(regex2)); //true
System.out.println("快快乐乐".matches(regex2)); //false


实例

(1)切割

需求:请按照叠词切割: “sdqqfgkkkhjppppkl”;

String s = "sdqqfgkkkhjppppkl";
String regex = "(.)\\1+"; //+ 代表一次或多次
String[] arr = s.split(regex);

for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
//sd fg hj kl


(2)替换

需求:我我….我…我.要…要要…要学….学学..学.编..编编.编.程.程.程..程

将字符串还原成:“我要学编程”。

String s = "我我....我...我.要...要要...要学....学学..学.编..编编.编.程.程.程..程";
String s2 = s.replaceAll("\\.+", "");
//我我我我要要要要学学学学编编编编程程程程
String s3 = s2.replaceAll("(.)\\1+", "$1"); //$1代表第一组中的内容
System.out.println(s3);
//我要学编程


Pattern类

典型的调用顺序是

Pattern p = Pattern.compile(“a*b”); //将给定的正则表达式编译到模式中。

Matcher m = p.matcher(“aaaaab”); //创建匹配给定输入与此模式的匹配器。

boolean b = m.matches(); //尝试将整个区域与模式匹配

实例:把一个字符串中的手机号码获取出来

String s = "我的手机是18988888888,我曾用过18987654321,还用过18812345678";
String regex = "1[3578]\\d{9}";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(s);
while(m.find()){
System.out.println(m.group());
}
//18988888888
//18987654321
//18812345678


38、Math类

Math类概述

Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。

成员方法

public static int abs(int a) //绝对值

public static double ceil(double a) //上界

public static double floor(double a) //下界

public static int max(int a,int b) //两个值中较大的一个

public static int min(int a,int b) //两个值中较小的一个

public static double pow(double a,double b) //第一个参数的第二个参数次幂的值

public static double random() //返回一个大于等于 0.0 且小于 1.0的随机数

public static int round(float a) //四舍五入

public static double sqrt(double a) //开方

39、Random类

Random类的概述

此类的实例用于生成伪随机数流。如果用相同的种子创建两个 Random 实例,则对每个实例进行相同的方法调用序列,它们将生成并返回相同的数字序列。

//每次运行生成的ab值与上次相同,但a!=b
Random r =  new Random(100);
int a = r.nextInt();
int b = r.nextInt();
System.out.println(a + "," + b);


构造方法

public Random()

public Random(long seed)

成员方法

public int nextInt() //int类型的随机数

public int nextInt(int n) //0到n-1

40、System类

System类的概述

System 类包含一些有用的类字段和方法。它不能被实例化。

成员方法

public static void gc() //运行垃圾回收器

public static void exit(int status) //终止当前正在运行的 Java 虚拟机

public static long currentTimeMillis() //返回以毫秒为单位的当前时间

pubiic static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length) // 从指定源数组中复制一个数组,复制从指定的位置开始,到目标数组的指定位置结束

41、BigInteger类

BigInteger的概述

不可变的任意精度的整数。

构造方法

public BigInteger(String val)

成员方法

public BigInteger add(BigInteger val) // +

public BigInteger subtract(BigInteger val) // -

public BigInteger multiply(BigInteger val) // *

public BigInteger divide(BigInteger val) // /

public BigInteger[] divideAndRemainder(BigInteger val) // /&&%

42、BigDecimal类

BigDecimal的概述

不可变的、任意精度的有符号十进制数。

构造方法

public BigDecimal(String val)

成员方法

public BigDecimal add(BigDecimal augend) // +

public BigDecimal subtract(BigDecimal subtrahend) // -

public BigDecimal multiply(BigDecimal multiplicand) // *

public BigDecimal divide(BigDecimal divisor) // /

43、Date类

Date类的概述

类 Date 表示特定的瞬间,精确到毫秒。

构造方法

public Date()

public Date(long date)

成员方法

public long getTime() // 返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。

public void setTime(long time) // 设置此 Date 对象,以表示 1970 年 1 月 1 日 00:00:00 GMT 以后 time 毫秒的时间点。

44、SimpleDateFormat类

DateFormat类的概述

DateFormat 是日期/时间格式化子类的抽象类,它以与语言无关的方式格式化并解析日期或时间。是抽象类,所以使用其子类SimpleDateFormat

SimpleDateFormat构造方法

public SimpleDateFormat()

public SimpleDateFormat(String pattern)

成员方法

public Date parse(String source) //解析字符串的文本,生成 Date

45、Calendar类

Calendar类的概述

Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。

成员方法

public static Calendar getInstance() //使用默认时区和语言环境获得一个日历。

public int get(int field) //返回给定日历字段的值。

public void add(int field,int amount) //根据日历的规则,为给定的日历字段添加或减去指定的时间量。

public final void set(int year,int month,int date) //设置日历字段 YEAR、MONTH 和 DAY_OF_MONTH 的值。

实例:算一下你来到这个世界多少天?

String birthday = "1996-11-01";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date birth = sdf.parse(birthday); //出生日期

Calendar c = Calendar.getInstance();
String today = c.get(Calendar.YEAR) + "-" + c.get(Calendar.MONTH) + "-" + c.get(Calendar.DAY_OF_MONTH);
Date toda = sdf.parse(today); //今天

long time = toda.getTime() - birth.getTime();
System.out.println(time / 1000 / 60 / 60 / 24);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java