Object class
java.lang.object
lang
The package does not need to be guided, it can be used directly
Is the parent of all classes
java.util.Objects
JDK7 adds an Object tool class
It provides some methods to manipulate objects. It consists of some static practical methods. These methods are null-save (null pointer safe) or null-tolerant (tolerant of null pointers), used to calculate objects hashcode
and return objects String representation, compare two objects.
When comparing two objects, the Object equals
method is prone to throw a null pointer exception.
null.equals
null cannot call method
public boolean equals(Object obj) {
return (this == obj);
}
The equals method in the Objects class optimizes this problem.
public static boolean equals(Object a, Object b) {
return (a == b) || (a != null && a.equals(b));
}
toString()
default
Do not rewrite, return address value
toString
The method returns the string representation of the object. In fact, the content of the string is the type of the object + @ + memory address value.
Rewrite
@Override
public String toString(){
return "";
}
equals()
###default
Object ==
address comparison of operators is performed by default in the Object class . As long as they are not the same object, the result must be false.
Rewrite
equals under the lang package
@Override
public boolean equals(object o){
if(this == o)
return ture;
if(o==null||getclass()!=o.getclass())
return false;
Person person =(person) o;
return age == person.age &&Object.equals(name,person.name);
}
Date class
java.util.Date
The class represents a specific moment, accurate to the millisecond.
Standard reference time 00:00:00 January 1, 1970 (UK)
public long getTime()
Convert the date object into the corresponding time millisecond value.
DateFormat class
Time/date formatting subclassAbstract class
java.text.SimpleDateFormat
public SimpleDateFormat(String pattern)
Identification letter (case sensitive) | meaning |
---|---|
Y | year |
M | month |
d | day |
H | Time |
m | Minute |
s | second |
The commonly used methods of the DateFormat class are:
public String format(Date date)
: Format the Date object as a string.public Date parse(String source)
: Parse the string into a Date object.
Calendar class
java.util.Calendar
It is a calendar class, which appears after Date, replacing many Date methods. This class encapsulates all possible time information as static member variables for easy access. The calendar class is convenient for obtaining various time attributes.
method of obtaining
Calendar is an abstract class. Due to language sensitivity, the Calendar class is not created directly when creating objects, but is created through static methods.Return subclass object,as follows:
Calendar static method
public static Calendar getInstance()
: Get a calendar using the default time zone and locale
Common method
According to the API documentation of the Calendar class, the commonly used methods are:
public int get(int field)
: Returns the value of a given calendar field.public void set(int field, int value)
: Set the given calendar field to the given value.public abstract void add(int field, int amount)
: According to the rules of the calendar, add or subtract a specified amount of time for a given calendar field.public Date getTime()
: Returns a Date object representing the time value of this Calendar (the millisecond offset from the epoch to the present).
Many member constants are provided in the Calendar class, which represent a given calendar field:
Field value | meaning |
---|---|
YEAR | year |
MONTH | Month (start from 0, can be used with +1) |
DAY_OF_MONTH | Day of the month (what day) |
HOUR | Hour (12-hour clock) |
HOUR_OF_DAY | Hour (24 hour clock) |
MINUTE | Minute |
SECOND | second |
DAY_OF_WEEK | Day of the week (day of the week, Sunday is 1, and -1 can be used) |
Tips:
The Western Week starts on Sunday, and China on Monday.
In the Calendar class, the month is represented by 0-11 representing January to December.
The date is related to the size, the later the time, the greater the time.
System class
java.lang.System
A large number of static methods are provided in the class to obtain system-related information or system-level operations. In the API documentation of the System class, the commonly used methods are:
public static long currentTimeMillis()
: Returns the current time in milliseconds. Equivalent to Date's getTime()public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
: Copy the specified data in the array to another array.
##Array Copy (arraycopy)
The copy action of the array is system level,High performance. The System.arraycopy method has 5 parameters, the meanings are:
Parameter number | parameter name | Parameter Type | Parameter meaning |
---|---|---|---|
1 | src | Object | Source array |
2 | srcPos | int | Source array index start position |
3 | dest | Object | Target array |
4 | destPos | int | The starting position of the target array index |
5 | length | int | Number of copied elements |
StringBuilder class
Difference with String
Common method
There are 2 commonly used methods in StringBuilder:
public StringBuilder append(...)
: Add the string form of any type of data,And return the current object itself。public String toString()
: Convert the current StringBuilder object to String object.Has been rewrittenObject's toString();
public class Demo02StringBuilder {
public static void main(String[] args) {
//创建对象
StringBuilder builder = new StringBuilder();
//public StringBuilder append(任意类型)
StringBuilder builder2 = builder.append("hello");
//对比一下
System.out.println("builder:"+builder);
System.out.println("builder2:"+builder2);
System.out.println(builder == builder2); //true
// 可以添加 任何类型
builder.append("hello");
builder.append("world");
builder.append(true);
builder.append(100);
// 在我们开发中,会遇到调用一个方法后,返回一个对象的情况。然后使用返回的对象继续调用方法。
// 这种时候,我们就可以把代码现在一起,如append方法一样,代码如下
//链式编程
builder.append("hello").append("world").append(true).append(100);
System.out.println("builder:"+builder);
}
}
回一个对象的情况。然后使用返回的对象继续调用方法。
// 这种时候,我们就可以把代码现在一起,如append方法一样,代码如下
//链式编程
builder.append("hello").append("world").append(true).append(100);
System.out.println("builder:"+builder);
}
}